fix restart delay
This commit is contained in:
parent
b9c5108384
commit
02239da2e2
|
|
@ -48,7 +48,9 @@ if (!process.env.PORT) {
|
||||||
|
|
||||||
await preparePrisma();
|
await preparePrisma();
|
||||||
await createLogger();
|
await createLogger();
|
||||||
|
if (g.mode !== "prod") {
|
||||||
await ensureNotRunning();
|
await ensureNotRunning();
|
||||||
|
}
|
||||||
|
|
||||||
if (g.db) {
|
if (g.db) {
|
||||||
try {
|
try {
|
||||||
|
|
|
||||||
64
pkgs/prod.ts
64
pkgs/prod.ts
|
|
@ -1,41 +1,24 @@
|
||||||
import { Subprocess } from "bun";
|
|
||||||
import { $ } from "execa";
|
import { $ } from "execa";
|
||||||
import exitHook from "exit-hook";
|
import exitHook from "exit-hook";
|
||||||
import { existsAsync } from "fs-jetpack";
|
import { existsAsync } from "fs-jetpack";
|
||||||
import { dir } from "utils/dir";
|
import { dir } from "utils/dir";
|
||||||
import { checkPort, randomBetween } from "utils/ensure";
|
|
||||||
import { g } from "utils/global";
|
import { g } from "utils/global";
|
||||||
|
|
||||||
g.main = {
|
g.main = {
|
||||||
process: null as null | Subprocess,
|
process: null as null | Worker,
|
||||||
restart: {
|
restart: {
|
||||||
timeout: null as any,
|
timeout: null as any,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
const main = g.main;
|
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) => {
|
exitHook((signal) => {
|
||||||
if (main.process && !main.process.killed) {
|
if (main.process) {
|
||||||
main.process.kill();
|
main.process.terminate();
|
||||||
}
|
}
|
||||||
console.log(`Exiting with signal: ${signal}`);
|
console.log(`Exiting with signal: ${signal}`);
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log("Process Manager running at port:", port);
|
|
||||||
|
|
||||||
if (process.env.DATABASE_URL) {
|
if (process.env.DATABASE_URL) {
|
||||||
if (
|
if (
|
||||||
!(await existsAsync(dir("node_modules/.prisma"))) &&
|
!(await existsAsync(dir("node_modules/.prisma"))) &&
|
||||||
|
|
@ -44,7 +27,7 @@ if (process.env.DATABASE_URL) {
|
||||||
try {
|
try {
|
||||||
await Bun.write(
|
await Bun.write(
|
||||||
dir("app/db/.env"),
|
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 install`;
|
||||||
await $({ cwd: dir(`app/db`) })`bun prisma db pull --force`;
|
await $({ cwd: dir(`app/db`) })`bun prisma db pull --force`;
|
||||||
|
|
@ -57,30 +40,21 @@ if (process.env.DATABASE_URL) {
|
||||||
|
|
||||||
const startMain = () => {
|
const startMain = () => {
|
||||||
let mode = "started";
|
let mode = "started";
|
||||||
if (main.process && !main.process.killed) return;
|
|
||||||
if (main.process && main.process.killed) mode = "restarted";
|
|
||||||
|
|
||||||
main.process = Bun.spawn({
|
const worker = new Worker("pkgs/index.ts");
|
||||||
cmd: ["bun", "run", "pkgs/index.ts"],
|
worker.onmessage = (event) => {
|
||||||
cwd: process.cwd(),
|
if (event.data === "restart") {
|
||||||
stdout: "inherit",
|
const oldprocess = main.process;
|
||||||
stderr: "inherit",
|
setTimeout(() => {
|
||||||
onExit(subprocess, exitCode, signalCode, error) {
|
oldprocess?.postMessage("stop-server");
|
||||||
clearTimeout(main.restart.timeout);
|
}, 3000);
|
||||||
main.restart.timeout = setTimeout(startMain, 500);
|
main.process = startMain();
|
||||||
},
|
|
||||||
});
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
};
|
||||||
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 { inspectAsync, listAsync } from "fs-jetpack";
|
||||||
import { join } from "path";
|
import { join } from "path";
|
||||||
import { createRouter } from "radix3";
|
import { createRouter } from "radix3";
|
||||||
|
import { ensureNotRunning } from "utils/ensure";
|
||||||
import { prodIndex } from "utils/prod-index";
|
import { prodIndex } from "utils/prod-index";
|
||||||
import { dir } from "../utils/dir";
|
import { dir } from "../utils/dir";
|
||||||
import { g } from "../utils/global";
|
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({
|
g.server = Bun.serve({
|
||||||
port: g.port,
|
port: g.port,
|
||||||
maxRequestBodySize: 1024 * 1024 * 128,
|
maxRequestBodySize: 1024 * 1024 * 128,
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
import { Server, Subprocess, WebSocketHandler } from "bun";
|
import { Server, 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,7 +48,7 @@ export const g = global as unknown as {
|
||||||
firebaseInit: boolean;
|
firebaseInit: boolean;
|
||||||
firebase: admin.app.App;
|
firebase: admin.app.App;
|
||||||
main: {
|
main: {
|
||||||
process: null | Subprocess;
|
process: null | Worker;
|
||||||
restart: {
|
restart: {
|
||||||
timeout: any;
|
timeout: any;
|
||||||
};
|
};
|
||||||
|
|
@ -88,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;
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ import { g } from "./global";
|
||||||
export const restartServer = () => {
|
export const restartServer = () => {
|
||||||
if (g.mode === "dev") {
|
if (g.mode === "dev") {
|
||||||
$`bun ${g.mode}`;
|
$`bun ${g.mode}`;
|
||||||
|
} else {
|
||||||
|
postMessage("restart");
|
||||||
}
|
}
|
||||||
process.exit(0);
|
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue