Skip to content
This repository has been archived by the owner on Mar 5, 2021. It is now read-only.

Commit

Permalink
Save sessions to disk when exiting and load them back up on startup
Browse files Browse the repository at this point in the history
  • Loading branch information
elisee committed Jul 7, 2016
1 parent f8c6268 commit 791add1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
npm-debug.log
/config.json
/settings.json
/sessions.json

SupCore/**/*.js
SupClient/**/*.js
Expand Down
33 changes: 30 additions & 3 deletions server/commands/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ let buildHttpServer: http.Server;
let languageIds: string[];
let isQuitting = false;

let memoryStore: expressSession.MemoryStore;

function onUncaughtException(err: Error) {
if (hub != null && hub.loadingProjectFolderName != null) {
SupCore.log(`The server crashed while loading project "${hub.loadingProjectFolderName}".\n${(err as any).stack}`);
Expand Down Expand Up @@ -67,10 +69,20 @@ export default function start(serverDataPath: string) {
mainApp = express();

if (typeof config.server.sessionSecret !== "string") throw new Error("serverConfig.sessionSecret is null");

memoryStore = new expressSession.MemoryStore();

try {
const sessionsJSON = fs.readFileSync(`${__dirname}/../../sessions.json`, { encoding: "utf8" });
(memoryStore as any).sessions = JSON.parse(sessionsJSON);
} catch (err) {
// Ignore
}

const sessionSettings = {
name: "supSession",
secret: config.server.sessionSecret,
store: new expressSession.MemoryStore(),
store: memoryStore,
resave: false,
saveUninitialized: false,
cookie: { maxAge: 1000 * 60 * 60 * 24 * 30 }
Expand Down Expand Up @@ -297,8 +309,23 @@ function onExit() {
SupCore.log("Saving all projects...");

hub.saveAll((err: Error) => {
if (err != null) SupCore.log(`Error while exiting:\n${(err as any).stack}`);
else SupCore.log("Exited cleanly.");
let hadError = false;

if (err != null) {
SupCore.log(`Error while exiting:\n${(err as any).stack}`);
hadError = true;
}

SupCore.log("Saving sessions...");
try {
const sessionsJSON = JSON.stringify((memoryStore as any).sessions, null, 2);
fs.writeFileSync(`${__dirname}/../../sessions.json`, sessionsJSON);
} catch (err) {
SupCore.log(`Failed to save sessions:\n${(err as any).stack}`);
hadError = true;
}

if (!hadError) SupCore.log("Exited cleanly.");
process.exit(0);
});
}

0 comments on commit 791add1

Please sign in to comment.