fix restart delay
This commit is contained in:
parent
b9c5108384
commit
02239da2e2
|
|
@ -48,7 +48,9 @@ if (!process.env.PORT) {
|
|||
|
||||
await preparePrisma();
|
||||
await createLogger();
|
||||
await ensureNotRunning();
|
||||
if (g.mode !== "prod") {
|
||||
await ensureNotRunning();
|
||||
}
|
||||
|
||||
if (g.db) {
|
||||
try {
|
||||
|
|
|
|||
66
pkgs/prod.ts
66
pkgs/prod.ts
|
|
@ -1,41 +1,24 @@
|
|||
import { Subprocess } from "bun";
|
||||
import { $ } from "execa";
|
||||
import exitHook from "exit-hook";
|
||||
import { existsAsync } from "fs-jetpack";
|
||||
import { dir } from "utils/dir";
|
||||
import { checkPort, randomBetween } from "utils/ensure";
|
||||
import { g } from "utils/global";
|
||||
|
||||
g.main = {
|
||||
process: null as null | Subprocess,
|
||||
process: null as null | Worker,
|
||||
restart: {
|
||||
timeout: null as any,
|
||||
},
|
||||
};
|
||||
const main = g.main;
|
||||
let port = 0;
|
||||
|
||||
try {
|
||||
port = parseInt(await Bun.file(".pm_port").text());
|
||||
} catch (e) {
|
||||
while (true) {
|
||||
port = randomBetween(5000, 15000);
|
||||
if (await checkPort(port)) {
|
||||
Bun.write(".pm_port", port.toString());
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
exitHook((signal) => {
|
||||
if (main.process && !main.process.killed) {
|
||||
main.process.kill();
|
||||
if (main.process) {
|
||||
main.process.terminate();
|
||||
}
|
||||
console.log(`Exiting with signal: ${signal}`);
|
||||
});
|
||||
|
||||
console.log("Process Manager running at port:", port);
|
||||
|
||||
if (process.env.DATABASE_URL) {
|
||||
if (
|
||||
!(await existsAsync(dir("node_modules/.prisma"))) &&
|
||||
|
|
@ -44,7 +27,7 @@ if (process.env.DATABASE_URL) {
|
|||
try {
|
||||
await Bun.write(
|
||||
dir("app/db/.env"),
|
||||
`DATABASE_URL=${process.env.DATABASE_URL}`
|
||||
`DATABASE_URL=${process.env.DATABASE_URL}`,
|
||||
);
|
||||
await $({ cwd: dir(`app/db`) })`bun install`;
|
||||
await $({ cwd: dir(`app/db`) })`bun prisma db pull --force`;
|
||||
|
|
@ -57,30 +40,21 @@ if (process.env.DATABASE_URL) {
|
|||
|
||||
const startMain = () => {
|
||||
let mode = "started";
|
||||
if (main.process && !main.process.killed) return;
|
||||
if (main.process && main.process.killed) mode = "restarted";
|
||||
|
||||
main.process = Bun.spawn({
|
||||
cmd: ["bun", "run", "pkgs/index.ts"],
|
||||
cwd: process.cwd(),
|
||||
stdout: "inherit",
|
||||
stderr: "inherit",
|
||||
onExit(subprocess, exitCode, signalCode, error) {
|
||||
clearTimeout(main.restart.timeout);
|
||||
main.restart.timeout = setTimeout(startMain, 500);
|
||||
},
|
||||
});
|
||||
console.log(`Main process`, mode, "pid:", main.process.pid);
|
||||
};
|
||||
startMain();
|
||||
Bun.serve({
|
||||
port,
|
||||
async fetch(request, server) {
|
||||
if (main.process && !main.process.killed) {
|
||||
main.process.kill();
|
||||
await main.process.exited;
|
||||
const worker = new Worker("pkgs/index.ts");
|
||||
worker.onmessage = (event) => {
|
||||
if (event.data === "restart") {
|
||||
const oldprocess = main.process;
|
||||
setTimeout(() => {
|
||||
oldprocess?.postMessage("stop-server");
|
||||
}, 3000);
|
||||
main.process = startMain();
|
||||
}
|
||||
|
||||
return new Response("OK");
|
||||
},
|
||||
});
|
||||
};
|
||||
worker.addEventListener("close", (event) => {
|
||||
console.log("Main worker being closed, thread-id: " + worker.threadId);
|
||||
});
|
||||
console.log(`Main worker`, mode, "thread-id:", worker.threadId);
|
||||
return worker;
|
||||
};
|
||||
main.process = startMain();
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ import { file } from "bun";
|
|||
import { inspectAsync, listAsync } from "fs-jetpack";
|
||||
import { join } from "path";
|
||||
import { createRouter } from "radix3";
|
||||
import { ensureNotRunning } from "utils/ensure";
|
||||
import { prodIndex } from "utils/prod-index";
|
||||
import { dir } from "../utils/dir";
|
||||
import { g } from "../utils/global";
|
||||
|
|
@ -62,6 +63,16 @@ export const createServer = async () => {
|
|||
};
|
||||
};
|
||||
|
||||
if (g.mode === "prod") {
|
||||
addEventListener("message", (e) => {
|
||||
if (e.data === "stop-server") {
|
||||
g.server.stop();
|
||||
process.exit();
|
||||
}
|
||||
});
|
||||
await ensureNotRunning();
|
||||
}
|
||||
|
||||
g.server = Bun.serve({
|
||||
port: g.port,
|
||||
maxRequestBodySize: 1024 * 1024 * 128,
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
import { Server, Subprocess, WebSocketHandler } from "bun";
|
||||
import { Server, WebSocketHandler } from "bun";
|
||||
import { Logger } from "pino";
|
||||
import { RadixRouter } from "radix3";
|
||||
import { PrismaClient } from "../../app/db/db";
|
||||
|
|
@ -48,7 +48,7 @@ export const g = global as unknown as {
|
|||
firebaseInit: boolean;
|
||||
firebase: admin.app.App;
|
||||
main: {
|
||||
process: null | Subprocess;
|
||||
process: null | Worker;
|
||||
restart: {
|
||||
timeout: any;
|
||||
};
|
||||
|
|
@ -88,7 +88,7 @@ export const g = global as unknown as {
|
|||
gz: Record<string, Uint8Array>;
|
||||
};
|
||||
createServer: (
|
||||
arg: PrasiServer & { api: any; db: any }
|
||||
arg: PrasiServer & { api: any; db: any },
|
||||
) => (site_id: string) => Promise<PrasiServer & { api: any; db: any }>;
|
||||
deploy: {
|
||||
init: boolean;
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ import { g } from "./global";
|
|||
export const restartServer = () => {
|
||||
if (g.mode === "dev") {
|
||||
$`bun ${g.mode}`;
|
||||
} else {
|
||||
postMessage("restart");
|
||||
}
|
||||
process.exit(0);
|
||||
};
|
||||
|
|
|
|||
Loading…
Reference in New Issue