Skip to content

Commit 5fb5102

Browse files
feat: Keep a running backup of restore snapshots which is created whenever a new flight is created. Regular snapshots are unaffected.
1 parent 2dabb69 commit 5fb5102

File tree

3 files changed

+54
-15
lines changed

3 files changed

+54
-15
lines changed

server/app.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,11 @@ class Events extends EventEmitter {
200200
const snap = this.trimSnapshot({...this});
201201
store.set(snap, null);
202202
}
203+
saveRestore() {
204+
this.snapshotVersion = this.version;
205+
const snap = this.trimSnapshot({...this});
206+
store.writeRestore(snap);
207+
}
203208
trimSnapshot({
204209
eventsToEmit = null,
205210
newEvents = null,

server/helpers/data-store.js

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
// From jonschlinkert/data-store
22
// Extracted to replace debounce with throttle
33

4-
import { writeFile as _writeFile, readFileSync, existsSync, mkdirSync, unlinkSync } from "fs";
5-
import { homedir } from "os";
6-
import { join, dirname as _dirname, relative, sep } from "path";
7-
import { strictEqual } from "assert";
4+
import {
5+
writeFile as _writeFile,
6+
readFileSync,
7+
readdirSync,
8+
existsSync,
9+
mkdirSync,
10+
unlinkSync,
11+
} from "fs";
12+
import {homedir} from "os";
13+
import path, {join, dirname as _dirname, relative, sep} from "path";
14+
import {strictEqual} from "assert";
815
const XDG_CONFIG_HOME = process.env.XDG_CONFIG_HOME;
916
import throttle from "./throttle";
1017
const flatten = (...args) => [].concat.apply([], args);
@@ -252,7 +259,7 @@ class Store {
252259
* @api public
253260
*/
254261

255-
json(replacer = null, space = this.indent) {
262+
json(data, replacer = null, space = this.indent) {
256263
function stringify(obj, replacer, spaces, cycleReplacer) {
257264
return JSON.stringify(obj, serializer(replacer, cycleReplacer), spaces);
258265
}
@@ -281,7 +288,7 @@ class Store {
281288
return replacer == null ? value : replacer.call(this, key, value);
282289
};
283290
}
284-
return stringify(this.data, replacer, space);
291+
return stringify(data, replacer, space);
285292
}
286293

287294
/**
@@ -317,21 +324,46 @@ class Store {
317324

318325
writeFile() {
319326
mkdir(_dirname(this.path), this.options.mkdir);
320-
const jsonData = this.json();
327+
const jsonData = this.json(this.data);
328+
329+
_writeFile(this.path, jsonData, {mode: 0o0600}, err => {
330+
if (err) {
331+
console.error(err);
332+
}
333+
});
334+
}
335+
336+
writeRestore(data) {
337+
mkdir(_dirname(this.path), this.options.mkdir);
338+
const jsonData = this.json(data);
339+
340+
// Get all of the restore files and keep the oldest 10
341+
const filePath = _dirname(this.path);
342+
readdirSync(filePath)
343+
.filter(f => f.includes("-restore-"))
344+
.sort((a, b) => {
345+
const timestampA = Number(
346+
a.slice(a.indexOf("-restore-") + 9),
347+
a.indexOf(".json"),
348+
);
349+
const timestampB = Number(
350+
b.slice(b.indexOf("-restore-") + 9),
351+
b.indexOf(".json"),
352+
);
353+
354+
return timestampB - timestampA;
355+
})
356+
.slice(10)
357+
.forEach(filename => unlinkSync(join(filePath, filename)));
358+
321359
_writeFile(
322-
this.path.replace(".json", "-restore.json"),
360+
this.path.replace(".json", `-restore-${Date.now()}.json`),
323361
jsonData,
324362
{mode: 0o0600},
325363
err => {
326364
if (err) {
327365
console.error(err);
328366
}
329-
330-
_writeFile(this.path, jsonData, {mode: 0o0600}, err => {
331-
if (err) {
332-
console.error(err);
333-
}
334-
});
335367
},
336368
);
337369
}

server/typeDefs/flight.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,8 @@ const resolver = {
417417
startFlight(rootQuery, {id = uuid.v4(), name, simulators, flightType}) {
418418
const simIds = simulators.map(
419419
(s: {simulatorId: string; missionId?: string; stationSet: string}) => {
420+
// Create a snapshot restore before the flight is created
421+
App.saveRestore();
420422
const template = cloneDeep(
421423
App.simulators.find(sim => sim.id === s.simulatorId),
422424
);
@@ -452,7 +454,7 @@ const resolver = {
452454
pubsub.publish("interfaceUpdate", App.interfaces);
453455

454456
pubsub.publish("flightsUpdate", App.flights);
455-
457+
456458
return id;
457459
},
458460
deleteFlight(rootQuery, {flightId}) {

0 commit comments

Comments
 (0)