fix subprocess
This commit is contained in:
parent
ff139e3846
commit
599ec7b777
|
|
@ -168,7 +168,7 @@ export const _ = {
|
||||||
const deploys = fs.readdirSync(dir(`/app/web/deploy`));
|
const deploys = fs.readdirSync(dir(`/app/web/deploy`));
|
||||||
|
|
||||||
if (g.mode === "prod") {
|
if (g.mode === "prod") {
|
||||||
postMessage("restart");
|
process?.send?.("restart");
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|
|
||||||
30
pkgs/prod.ts
30
pkgs/prod.ts
|
|
@ -5,7 +5,6 @@ import { dir } from "utils/dir";
|
||||||
import { g } from "utils/global";
|
import { g } from "utils/global";
|
||||||
|
|
||||||
g.main = {
|
g.main = {
|
||||||
old: null,
|
|
||||||
process: null,
|
process: null,
|
||||||
restart: {
|
restart: {
|
||||||
timeout: null as any,
|
timeout: null as any,
|
||||||
|
|
@ -15,7 +14,7 @@ const main = g.main;
|
||||||
|
|
||||||
exitHook((signal) => {
|
exitHook((signal) => {
|
||||||
if (main.process) {
|
if (main.process) {
|
||||||
main.process.terminate();
|
main.process.kill();
|
||||||
}
|
}
|
||||||
console.log(`Exiting with signal: ${signal}`);
|
console.log(`Exiting with signal: ${signal}`);
|
||||||
});
|
});
|
||||||
|
|
@ -40,25 +39,24 @@ if (process.env.DATABASE_URL) {
|
||||||
}
|
}
|
||||||
|
|
||||||
const startMain = () => {
|
const startMain = () => {
|
||||||
let mode = "started";
|
return Bun.spawn({
|
||||||
|
cmd: ["bun", "run", "pkgs/index.ts"],
|
||||||
const worker = new Worker("pkgs/index.ts");
|
cwd: process.cwd(),
|
||||||
worker.onmessage = (event) => {
|
stdout: "inherit",
|
||||||
if (event.data === "restart") {
|
stderr: "inherit",
|
||||||
main.old = main.process;
|
ipc(message, subprocess) {
|
||||||
|
if (message === "restart") {
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
if (main.old) {
|
subprocess.kill();
|
||||||
main.old.terminate();
|
|
||||||
}
|
|
||||||
}, 1000);
|
}, 1000);
|
||||||
main.process = startMain();
|
main.process = startMain();
|
||||||
}
|
}
|
||||||
};
|
},
|
||||||
worker.addEventListener("close", (event) => {
|
onExit(subprocess, exitCode, signalCode, error) {
|
||||||
console.log("Main worker being closed, thread-id: " + worker.threadId);
|
clearTimeout(main.restart.timeout);
|
||||||
|
main.restart.timeout = setTimeout(startMain, 500);
|
||||||
|
},
|
||||||
});
|
});
|
||||||
console.log(`Main worker`, mode, "thread-id:", worker.threadId);
|
|
||||||
return worker;
|
|
||||||
};
|
};
|
||||||
main.process = startMain();
|
main.process = startMain();
|
||||||
setTimeout(() => new Promise(() => 0), 0);
|
setTimeout(() => new Promise(() => 0), 0);
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
import { Server, WebSocketHandler } from "bun";
|
import { Server, Subprocess, WebSocketHandler } from "bun";
|
||||||
import { Logger } from "pino";
|
import { Logger } from "pino";
|
||||||
import { RadixRouter } from "radix3";
|
import { RadixRouter } from "radix3";
|
||||||
import { PrismaClient } from "../../app/db/db";
|
import { PrismaClient } from "../../app/db/db";
|
||||||
|
|
@ -48,8 +48,7 @@ export const g = global as unknown as {
|
||||||
firebaseInit: boolean;
|
firebaseInit: boolean;
|
||||||
firebase: admin.app.App;
|
firebase: admin.app.App;
|
||||||
main: {
|
main: {
|
||||||
old: null | Worker;
|
process: null | Subprocess;
|
||||||
process: null | Worker;
|
|
||||||
restart: {
|
restart: {
|
||||||
timeout: any;
|
timeout: any;
|
||||||
};
|
};
|
||||||
|
|
@ -89,7 +88,7 @@ export const g = global as unknown as {
|
||||||
gz: Record<string, Uint8Array>;
|
gz: Record<string, Uint8Array>;
|
||||||
};
|
};
|
||||||
createServer: (
|
createServer: (
|
||||||
arg: PrasiServer & { api: any; db: any },
|
arg: PrasiServer & { api: any; db: any }
|
||||||
) => (site_id: string) => Promise<PrasiServer & { api: any; db: any }>;
|
) => (site_id: string) => Promise<PrasiServer & { api: any; db: any }>;
|
||||||
deploy: {
|
deploy: {
|
||||||
init: boolean;
|
init: boolean;
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,6 @@ export const restartServer = () => {
|
||||||
if (g.mode === "dev") {
|
if (g.mode === "dev") {
|
||||||
$`bun ${g.mode}`;
|
$`bun ${g.mode}`;
|
||||||
} else {
|
} else {
|
||||||
postMessage("restart");
|
process?.send?.("restart");
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue