Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Start backend in seperate worker thread #120

Merged
merged 1 commit into from
Sep 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions backend/src/projects/projects.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,7 @@ export class ProjectsService {

async cleanupProject() {
try {
// remove all existing data of previously used project
// TODO(milestone-x): persist data for projects by default?
// Remove all cached data of previously used project
await this.commonService.removeBlockchainData();
} catch (e) {
throw new InternalServerErrorException("Database cleanup failed");
Expand Down
31 changes: 7 additions & 24 deletions frontend/src/targets/electron/main.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,14 @@
import * as path from "path";
import { app, BrowserWindow, shell, dialog } from "electron";
import { createApp, ProcessManagerService } from "@flowser/backend";
import { ProcessManagerService } from "@flowser/backend";
import fixPath from "fix-path";
import { INestApplication } from "@nestjs/common";
import * as worker from "./worker";

fixPath();

const minWidth = 900;
const minHeight = 600;

let backend: INestApplication;

async function startBackend() {
const userDataPath = app.getPath("userData");
const databaseFilePath = path.join(userDataPath, "flowser.sqlite");
if (backend) {
await backend.close();
}
backend = await createApp({
database: {
type: "sqlite",
name: databaseFilePath,
},
common: {
httpServerPort: 6061,
},
});
}

async function createWindow() {
const win = new BrowserWindow({
width: minWidth,
Expand All @@ -54,7 +35,7 @@ async function createWindow() {

async function handleStart() {
try {
await startBackend();
await worker.start();
} catch (error) {
await handleBackendError({
error,
Expand Down Expand Up @@ -87,8 +68,10 @@ app.on("activate", function () {

app.on("will-quit", async function () {
// Make sure to stop all child processes, so that they don't become orphans
const processManagerService = backend.get(ProcessManagerService);
await processManagerService.stopAll();
if (worker.backend) {
const processManagerService = worker.backend.get(ProcessManagerService);
await processManagerService.stopAll();
}
});

type ErrorWithCode = { code: string };
Expand Down
49 changes: 49 additions & 0 deletions frontend/src/targets/electron/worker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { Worker, isMainThread, parentPort, workerData } from "worker_threads";
import path from "path";
import { app } from "electron";
import { createApp } from "@flowser/backend";
import { INestApplication } from "@nestjs/common";

export let backend: INestApplication;

async function startBackend({ userDataPath }: { userDataPath: string }) {
const databaseFilePath = path.join(userDataPath, "flowser.sqlite");
if (backend) {
await backend.close();
}
backend = await createApp({
database: {
type: "sqlite",
name: databaseFilePath,
},
common: {
httpServerPort: 6061,
},
});
}

export let start: () => Promise<unknown>;

if (isMainThread) {
start = async function () {
const userDataPath = app.getPath("userData");
return new Promise((resolve, reject) => {
const worker = new Worker(__filename, {
workerData: {
userDataPath,
},
});
worker.on("message", resolve);
worker.on("error", reject);
worker.on("exit", (code) => {
if (code !== 0)
reject(new Error(`Worker stopped with exit code ${code}`));
});
});
};
} else {
const { userDataPath } = workerData;
startBackend({
userDataPath,
}).then(() => parentPort?.postMessage("Backend started"));
}