Skip to content

Commit

Permalink
more graceful endings
Browse files Browse the repository at this point in the history
  • Loading branch information
jonluca committed Apr 19, 2023
1 parent 3b63767 commit 7425a14
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 6 deletions.
8 changes: 8 additions & 0 deletions electron-src/data/base-database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ export class BaseDatabase<T> {
this.postSetup = postSetup;
}

terminate = () => {
const db = this.dbWriter;
this.dbWriter = undefined;
if (db) {
return db.destroy();
}
};

isDbInitialized = () => {
return !!this.dbWriter;
};
Expand Down
9 changes: 5 additions & 4 deletions electron-src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ if (!amMainInstance) {
await installExtensions();
}
await dbWorker.startWorker();

nativeTheme.themeSource = "dark";
dbWorker.setupHandlers();
appReady.resolve();
Expand Down Expand Up @@ -139,8 +138,10 @@ if (!amMainInstance) {
appReady.promise.then(() => showApp());
}
});

app.on("window-all-closed", () => {
app.quit();
app.on("will-quit", () => {
logStream.write("Quitting");
dbWorker.stopWorker();
logStream.end();
process.exit(0);
});
}
2 changes: 1 addition & 1 deletion electron-src/window/main-window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const setupNext = async () => {
} catch (e) {
logger.error(`Failed to prepare next: ${e}`);
showErrorAlert("Error", "Failed to prepare next");
app.quit();
app.exit(-1);
return;
}
};
Expand Down
11 changes: 10 additions & 1 deletion electron-src/workers/database-worker.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { spawn, Worker } from "threads";
import type { ModuleThread } from "threads";
import { spawn, Thread, Worker } from "threads";
import type { SQLDatabase } from "../data/database";
import { handleIpc } from "../ipc/ipc";
import db from "../data/database";
Expand Down Expand Up @@ -37,6 +38,14 @@ class DbWorker {
this.embeddingsWorker = await spawn<WorkerType<EmbeddingsDatabase>>(new Worker(embeddingWorkerPath, opts));
};

stopWorker = () => {
this.worker.terminate();
this.embeddingsWorker.terminate();
if (!isDev) {
Thread.terminate(this.worker as ModuleThread<WorkerType<SQLDatabase>>);
Thread.terminate(this.embeddingsWorker as ModuleThread<WorkerType<EmbeddingsDatabase>>);
}
};
setupHandlers() {
for (const property in db) {
const prop = property as keyof WorkerType<SQLDatabase>;
Expand Down

0 comments on commit 7425a14

Please sign in to comment.