Refine CLI service and align project metadata

This commit is contained in:
gitea committed 2026-07-30 19:13:54 +08:00
1 parent 802c32c6bd
commit 2ca45d935e
11 files changed
+285 -184

No files matched your search

+160
View File
@@ -0,0 +1,160 @@
import { spawn, execFileSync } from "node:child_process";
import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import { join } from "node:path";
export type ServiceConfig = {
appRoot: string;
entryFile: string;
stateDir: string;
version?: string;
};
export async function runServiceCommand(action: string | undefined, config: ServiceConfig) {
switch (action) {
case "run":
await runService(config);
return;
case "start":
startService(config);
return;
case "stop":
stopService(config);
return;
case "restart":
stopService(config, { quiet: true });
startService(config);
return;
case "status":
case undefined:
console.log(JSON.stringify(serviceStatus(config), null, 2));
return;
default:
throw new Error("Usage: seal-home service <start|stop|restart|status>");
}
}
export function serviceStatus(config: ServiceConfig) {
const pidFile = getPidFile(config);
const pid = readServicePid(pidFile);
const running = pid ? isSealHomeProcess(pid, config.appRoot) : false;
if (pid && !running) {
rmSync(pidFile, { force: true });
}
return {
running,
pid: running ? pid : undefined,
version: config.version,
appRoot: config.appRoot,
stateDir: config.stateDir,
pidFile,
logFile: getLogFile(config)
};
}
async function runService(config: ServiceConfig) {
const pidFile = getPidFile(config);
const logFile = getLogFile(config);
ensureStateDir(config.stateDir);
writeFileSync(pidFile, `${process.pid}\n`, { mode: 0o600 });
appendLog(logFile, `running pid=${process.pid}`);
const cleanup = () => {
if (readServicePid(pidFile) === process.pid) {
rmSync(pidFile, { force: true });
}
appendLog(logFile, `stopped pid=${process.pid}`);
};
process.on("SIGTERM", () => {
cleanup();
process.exit(0);
});
process.on("SIGINT", () => {
cleanup();
process.exit(0);
});
setInterval(() => {}, 60_000);
await new Promise(() => {});
}
function startService(config: ServiceConfig) {
ensureStateDir(config.stateDir);
const current = serviceStatus(config);
if (current.running) {
console.log(JSON.stringify(current, null, 2));
return;
}
const child = spawn(process.execPath, [config.entryFile, "service", "run"], {
cwd: config.appRoot,
detached: true,
stdio: ["ignore", "ignore", "ignore"],
env: process.env
});
child.unref();
const pidFile = getPidFile(config);
writeFileSync(pidFile, `${child.pid}\n`, { mode: 0o600 });
appendLog(getLogFile(config), `started pid=${child.pid}`);
console.log(JSON.stringify({ started: true, ...serviceStatus(config) }, null, 2));
}
function stopService(config: ServiceConfig, options: { quiet?: boolean } = {}) {
const pidFile = getPidFile(config);
const pid = readServicePid(pidFile);
if (!pid || !isSealHomeProcess(pid, config.appRoot)) {
rmSync(pidFile, { force: true });
if (!options.quiet) console.log(JSON.stringify({ stopped: false, reason: "not running" }, null, 2));
return;
}
process.kill(pid, "SIGTERM");
rmSync(pidFile, { force: true });
appendLog(getLogFile(config), `stop requested pid=${pid}`);
if (!options.quiet) console.log(JSON.stringify({ stopped: true, pid }, null, 2));
}
function isSealHomeProcess(pid: number, appRoot: string) {
if (!isProcessRunning(pid)) return false;
try {
const command = execFileSync("ps", ["-p", String(pid), "-o", "command="], { encoding: "utf8" });
return command.includes(appRoot) && command.includes("service") && command.includes("run");
} catch {
return false;
}
}
function isProcessRunning(pid: number) {
try {
process.kill(pid, 0);
return true;
} catch {
return false;
}
}
function getPidFile(config: ServiceConfig) {
return join(config.stateDir, "service.pid");
}
function getLogFile(config: ServiceConfig) {
return join(config.stateDir, "service.log");
}
function readServicePid(path: string): number | undefined {
if (!existsSync(path)) return undefined;
const pid = Number(readFileSync(path, "utf8").trim());
return Number.isInteger(pid) && pid > 0 ? pid : undefined;
}
function ensureStateDir(stateDir: string) {
mkdirSync(stateDir, { recursive: true, mode: 0o700 });
}
function appendLog(path: string, message: string) {
writeFileSync(path, `${new Date().toISOString()} ${message}\n`, { flag: "a", mode: 0o600 });
}