orchestrating 0.1.33 → 0.1.34
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/orch +66 -2
- package/package.json +1 -1
package/bin/orch
CHANGED
|
@@ -243,14 +243,78 @@ if (firstArg === "logout") {
|
|
|
243
243
|
}
|
|
244
244
|
|
|
245
245
|
if (firstArg === "daemon") {
|
|
246
|
-
// Parse daemon flags: orch daemon [--projects <path>]
|
|
247
246
|
const daemonArgs = process.argv.slice(3);
|
|
247
|
+
const pidFile = path.join(os.homedir(), ".orch-daemon.pid");
|
|
248
|
+
|
|
249
|
+
// orch daemon --stop
|
|
250
|
+
if (daemonArgs.includes("--stop")) {
|
|
251
|
+
if (existsSync(pidFile)) {
|
|
252
|
+
const pid = parseInt(readFileSync(pidFile, "utf-8").trim(), 10);
|
|
253
|
+
try {
|
|
254
|
+
process.kill(pid, "SIGTERM");
|
|
255
|
+
unlinkSync(pidFile);
|
|
256
|
+
console.log(`Daemon stopped (pid ${pid}).`);
|
|
257
|
+
} catch {
|
|
258
|
+
unlinkSync(pidFile);
|
|
259
|
+
console.log("Daemon was not running. Cleaned up pid file.");
|
|
260
|
+
}
|
|
261
|
+
} else {
|
|
262
|
+
console.log("No daemon running.");
|
|
263
|
+
}
|
|
264
|
+
process.exit(0);
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
// orch daemon --status
|
|
268
|
+
if (daemonArgs.includes("--status")) {
|
|
269
|
+
if (existsSync(pidFile)) {
|
|
270
|
+
const pid = parseInt(readFileSync(pidFile, "utf-8").trim(), 10);
|
|
271
|
+
try {
|
|
272
|
+
process.kill(pid, 0); // Check if process exists
|
|
273
|
+
console.log(`Daemon running (pid ${pid}).`);
|
|
274
|
+
} catch {
|
|
275
|
+
console.log("Daemon not running (stale pid file).");
|
|
276
|
+
}
|
|
277
|
+
} else {
|
|
278
|
+
console.log("Daemon not running.");
|
|
279
|
+
}
|
|
280
|
+
process.exit(0);
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
// Parse flags
|
|
248
284
|
let projectsDir = null;
|
|
285
|
+
const background = daemonArgs.includes("--background") || daemonArgs.includes("-b");
|
|
249
286
|
for (let di = 0; di < daemonArgs.length; di++) {
|
|
250
287
|
if ((daemonArgs[di] === "--projects" || daemonArgs[di] === "-d") && daemonArgs[di + 1]) {
|
|
251
288
|
projectsDir = daemonArgs[++di];
|
|
252
289
|
}
|
|
253
290
|
}
|
|
291
|
+
|
|
292
|
+
// orch daemon --background — fork and exit
|
|
293
|
+
if (background) {
|
|
294
|
+
const orchPath = fileURLToPath(import.meta.url);
|
|
295
|
+
const fwdArgs = ["daemon"];
|
|
296
|
+
if (projectsDir) fwdArgs.push("--projects", projectsDir);
|
|
297
|
+
const logFile = path.join(os.homedir(), ".orch-daemon.log");
|
|
298
|
+
const out = (await import("fs")).openSync(logFile, "a");
|
|
299
|
+
const child = spawn(process.execPath, [orchPath, ...fwdArgs], {
|
|
300
|
+
stdio: ["ignore", out, out],
|
|
301
|
+
detached: true,
|
|
302
|
+
env: { ...process.env },
|
|
303
|
+
});
|
|
304
|
+
child.unref();
|
|
305
|
+
writeFileSync(pidFile, String(child.pid));
|
|
306
|
+
console.log(`Daemon started in background (pid ${child.pid}).`);
|
|
307
|
+
console.log(`Logs: ${logFile}`);
|
|
308
|
+
console.log(`Stop: orch daemon --stop`);
|
|
309
|
+
process.exit(0);
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
// Foreground — write pid file for --stop/--status
|
|
313
|
+
writeFileSync(pidFile, String(process.pid));
|
|
314
|
+
process.on("exit", () => { try { unlinkSync(pidFile); } catch {} });
|
|
315
|
+
process.on("SIGTERM", () => process.exit(0));
|
|
316
|
+
process.on("SIGINT", () => process.exit(0));
|
|
317
|
+
|
|
254
318
|
await handleDaemon(projectsDir);
|
|
255
319
|
// handleDaemon runs forever (or exits on fatal error)
|
|
256
320
|
}
|
|
@@ -309,7 +373,7 @@ async function handleDaemon(projectsDir) {
|
|
|
309
373
|
|
|
310
374
|
process.stderr.write(`${GREEN}${PREFIX} Listening for remote sessions as "${hostname}"${RESET}\n`);
|
|
311
375
|
process.stderr.write(`${DIM}${PREFIX} Projects: ${scanRoot} (${directories.length} dirs)${RESET}\n`);
|
|
312
|
-
process.stderr.write(`${DIM}${PREFIX} Tip: Use '
|
|
376
|
+
process.stderr.write(`${DIM}${PREFIX} Tip: Use 'orch daemon -b' to run in background${RESET}\n`);
|
|
313
377
|
|
|
314
378
|
let reconnecting = false;
|
|
315
379
|
const recentRequests = new Set();
|