Refine CLI service and align project metadata
This commit is contained in:
1 parent
802c32c6bd
commit
2ca45d935e
11 files changed
+285
-184
No files matched your search
+8
-135
@@ -1,7 +1,7 @@
|
||||
#!/usr/bin/env bun
|
||||
|
||||
import { spawn, spawnSync } from "node:child_process";
|
||||
import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
||||
import { spawnSync } from "node:child_process";
|
||||
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
||||
import { homedir } from "node:os";
|
||||
import { dirname, join } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
@@ -14,11 +14,10 @@ import { CorpConfig } from "./core/config/types.js";
|
||||
import { clearCorpTokenCache } from "./core/auth/token-store.js";
|
||||
import { getHoseProvisionalAuthLink } from "./core/auth/hose.js";
|
||||
import { VERSION } from "./version.js";
|
||||
import { runServiceCommand, serviceStatus } from "./cli/service.js";
|
||||
|
||||
const APP_ROOT = dirname(dirname(fileURLToPath(import.meta.url)));
|
||||
const STATE_DIR = join(homedir(), ".config", "seal-home");
|
||||
const SERVICE_PID_FILE = join(STATE_DIR, "service.pid");
|
||||
const SERVICE_LOG_FILE = join(STATE_DIR, "service.log");
|
||||
const CURRENT_CORP_FILE = join(STATE_DIR, "current-corp");
|
||||
const RULE_LIST_FULL_COUNT_LIMIT = 100;
|
||||
const RULE_LIST_FULL_BYTES_LIMIT = 120_000;
|
||||
@@ -67,7 +66,7 @@ async function main() {
|
||||
}
|
||||
|
||||
if (area === "service") {
|
||||
await runServiceCommand(action);
|
||||
await runServiceCommand(action, { appRoot: APP_ROOT, entryFile: fileURLToPath(import.meta.url), stateDir: STATE_DIR, version: VERSION });
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -484,121 +483,15 @@ async function main() {
|
||||
throw new Error(`Unknown command: ${args.command.join(" ")}. Run 'seal-home help' for available commands.`);
|
||||
}
|
||||
|
||||
async function runServiceCommand(action?: string) {
|
||||
switch (action) {
|
||||
case "run":
|
||||
await runService();
|
||||
return;
|
||||
case "start":
|
||||
startService();
|
||||
return;
|
||||
case "stop":
|
||||
stopService();
|
||||
return;
|
||||
case "restart":
|
||||
stopService({ quiet: true });
|
||||
startService();
|
||||
return;
|
||||
case "status":
|
||||
case undefined:
|
||||
printJson(serviceStatus());
|
||||
return;
|
||||
default:
|
||||
throw new Error("Usage: seal-home service <start|stop|restart|status>");
|
||||
}
|
||||
}
|
||||
|
||||
async function runService() {
|
||||
ensureStateDir();
|
||||
writeFileSync(SERVICE_PID_FILE, `${process.pid}\n`);
|
||||
appendLog(`running pid=${process.pid}`);
|
||||
|
||||
const cleanup = () => {
|
||||
if (readServicePid() === process.pid) {
|
||||
rmSync(SERVICE_PID_FILE, { force: true });
|
||||
}
|
||||
appendLog(`stopped pid=${process.pid}`);
|
||||
};
|
||||
|
||||
process.on("SIGTERM", () => {
|
||||
cleanup();
|
||||
process.exit(0);
|
||||
});
|
||||
process.on("SIGINT", () => {
|
||||
cleanup();
|
||||
process.exit(0);
|
||||
});
|
||||
|
||||
setInterval(() => {
|
||||
// Keep the process alive for warmed auth/search caches in this runtime.
|
||||
}, 60_000);
|
||||
|
||||
await new Promise(() => {});
|
||||
}
|
||||
|
||||
function startService() {
|
||||
ensureStateDir();
|
||||
const current = serviceStatus();
|
||||
if (current.running) {
|
||||
printJson(current);
|
||||
return;
|
||||
}
|
||||
|
||||
const child = spawn(process.execPath, [fileURLToPath(import.meta.url), "service", "run"], {
|
||||
cwd: APP_ROOT,
|
||||
detached: true,
|
||||
stdio: ["ignore", "ignore", "ignore"],
|
||||
env: process.env
|
||||
});
|
||||
child.unref();
|
||||
|
||||
writeFileSync(SERVICE_PID_FILE, `${child.pid}\n`);
|
||||
appendLog(`started pid=${child.pid}`);
|
||||
printJson({
|
||||
started: true,
|
||||
...serviceStatus()
|
||||
});
|
||||
}
|
||||
|
||||
function stopService(options: { quiet?: boolean } = {}) {
|
||||
const pid = readServicePid();
|
||||
if (!pid) {
|
||||
if (!options.quiet) printJson({ stopped: false, reason: "not running" });
|
||||
return;
|
||||
}
|
||||
|
||||
if (isProcessRunning(pid)) {
|
||||
process.kill(pid, "SIGTERM");
|
||||
}
|
||||
rmSync(SERVICE_PID_FILE, { force: true });
|
||||
appendLog(`stop requested pid=${pid}`);
|
||||
|
||||
if (!options.quiet) {
|
||||
printJson({ stopped: true, pid });
|
||||
}
|
||||
}
|
||||
|
||||
function serviceStatus() {
|
||||
const pid = readServicePid();
|
||||
return {
|
||||
running: pid ? isProcessRunning(pid) : false,
|
||||
pid,
|
||||
version: VERSION,
|
||||
appRoot: APP_ROOT,
|
||||
stateDir: STATE_DIR,
|
||||
pidFile: SERVICE_PID_FILE,
|
||||
logFile: SERVICE_LOG_FILE
|
||||
};
|
||||
}
|
||||
|
||||
function updateAndRestart() {
|
||||
const wasRunning = serviceStatus().running;
|
||||
if (wasRunning) stopService({ quiet: true });
|
||||
const serviceConfig = { appRoot: APP_ROOT, entryFile: fileURLToPath(import.meta.url), stateDir: STATE_DIR, version: VERSION };
|
||||
const wasRunning = serviceStatus(serviceConfig).running;
|
||||
if (wasRunning) runServiceCommand("stop", serviceConfig);
|
||||
|
||||
runCommand("git", ["-C", APP_ROOT, "pull", "--ff-only"]);
|
||||
runCommand("bun", ["install"], { cwd: APP_ROOT });
|
||||
|
||||
if (wasRunning) startService();
|
||||
if (wasRunning) runServiceCommand("start", serviceConfig);
|
||||
|
||||
printJson({
|
||||
updated: true,
|
||||
@@ -1302,26 +1195,6 @@ function ensureStateDir() {
|
||||
mkdirSync(STATE_DIR, { recursive: true });
|
||||
}
|
||||
|
||||
function readServicePid(): number | undefined {
|
||||
if (!existsSync(SERVICE_PID_FILE)) return undefined;
|
||||
const pid = Number(readFileSync(SERVICE_PID_FILE, "utf-8").trim());
|
||||
return Number.isInteger(pid) && pid > 0 ? pid : undefined;
|
||||
}
|
||||
|
||||
function isProcessRunning(pid: number): boolean {
|
||||
try {
|
||||
process.kill(pid, 0);
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
function appendLog(message: string) {
|
||||
ensureStateDir();
|
||||
writeFileSync(SERVICE_LOG_FILE, `${new Date().toISOString()} ${message}\n`, { flag: "a" });
|
||||
}
|
||||
|
||||
function printJson(value: unknown, options: OutputOptions = {}) {
|
||||
protectLargeOutput(value, options);
|
||||
const json = `${JSON.stringify(value, null, 2)}\n`;
|
||||
|
||||
Reference in new issue
Block a user