From 599ec7b777f3fdae72f72b5b7594598003b9572b Mon Sep 17 00:00:00 2001 From: Rizky Date: Thu, 15 Aug 2024 21:42:34 +0700 Subject: [PATCH] fix subprocess --- pkgs/api/_deploy.ts | 2 +- pkgs/prod.ts | 38 ++++++++++++++++++-------------------- pkgs/utils/global.ts | 7 +++---- pkgs/utils/restart.ts | 2 +- 4 files changed, 23 insertions(+), 26 deletions(-) diff --git a/pkgs/api/_deploy.ts b/pkgs/api/_deploy.ts index 8b55ee9..9460ba2 100644 --- a/pkgs/api/_deploy.ts +++ b/pkgs/api/_deploy.ts @@ -168,7 +168,7 @@ export const _ = { const deploys = fs.readdirSync(dir(`/app/web/deploy`)); if (g.mode === "prod") { - postMessage("restart"); + process?.send?.("restart"); } return { diff --git a/pkgs/prod.ts b/pkgs/prod.ts index c91aba3..2a0d1f5 100644 --- a/pkgs/prod.ts +++ b/pkgs/prod.ts @@ -5,7 +5,6 @@ import { dir } from "utils/dir"; import { g } from "utils/global"; g.main = { - old: null, process: null, restart: { timeout: null as any, @@ -15,7 +14,7 @@ const main = g.main; exitHook((signal) => { if (main.process) { - main.process.terminate(); + main.process.kill(); } console.log(`Exiting with signal: ${signal}`); }); @@ -40,25 +39,24 @@ if (process.env.DATABASE_URL) { } const startMain = () => { - let mode = "started"; - - const worker = new Worker("pkgs/index.ts"); - worker.onmessage = (event) => { - if (event.data === "restart") { - main.old = main.process; - setTimeout(() => { - if (main.old) { - main.old.terminate(); - } - }, 1000); - main.process = startMain(); - } - }; - worker.addEventListener("close", (event) => { - console.log("Main worker being closed, thread-id: " + worker.threadId); + return Bun.spawn({ + cmd: ["bun", "run", "pkgs/index.ts"], + cwd: process.cwd(), + stdout: "inherit", + stderr: "inherit", + ipc(message, subprocess) { + if (message === "restart") { + setTimeout(() => { + subprocess.kill(); + }, 1000); + main.process = startMain(); + } + }, + onExit(subprocess, exitCode, signalCode, error) { + clearTimeout(main.restart.timeout); + main.restart.timeout = setTimeout(startMain, 500); + }, }); - console.log(`Main worker`, mode, "thread-id:", worker.threadId); - return worker; }; main.process = startMain(); setTimeout(() => new Promise(() => 0), 0); diff --git a/pkgs/utils/global.ts b/pkgs/utils/global.ts index 0725494..bc6a27e 100644 --- a/pkgs/utils/global.ts +++ b/pkgs/utils/global.ts @@ -1,4 +1,4 @@ -import { Server, WebSocketHandler } from "bun"; +import { Server, Subprocess, WebSocketHandler } from "bun"; import { Logger } from "pino"; import { RadixRouter } from "radix3"; import { PrismaClient } from "../../app/db/db"; @@ -48,8 +48,7 @@ export const g = global as unknown as { firebaseInit: boolean; firebase: admin.app.App; main: { - old: null | Worker; - process: null | Worker; + process: null | Subprocess; restart: { timeout: any; }; @@ -89,7 +88,7 @@ export const g = global as unknown as { gz: Record; }; createServer: ( - arg: PrasiServer & { api: any; db: any }, + arg: PrasiServer & { api: any; db: any } ) => (site_id: string) => Promise; deploy: { init: boolean; diff --git a/pkgs/utils/restart.ts b/pkgs/utils/restart.ts index f208d10..da34a9b 100644 --- a/pkgs/utils/restart.ts +++ b/pkgs/utils/restart.ts @@ -5,6 +5,6 @@ export const restartServer = () => { if (g.mode === "dev") { $`bun ${g.mode}`; } else { - postMessage("restart"); + process?.send?.("restart"); } };