feat(cli): Add stats CLI command and daemon stats aggregation; fix process manager & wrapper state handling

This commit is contained in:
2025-08-31 08:06:03 +00:00
parent 1c4ffbb612
commit 6f14033d9b
9 changed files with 166 additions and 7 deletions

View File

@@ -95,6 +95,16 @@ export class ProcessManager extends EventEmitter {
// Check if process with this id already exists
if (this.processes.has(config.id)) {
const existing = this.processes.get(config.id)!;
// If an existing monitor is present but not running, treat this as a fresh start via restart logic
if (!existing.isRunning()) {
this.logger.info(
`Existing monitor found for id '${config.id}' but not running. Restarting it...`,
);
await this.restart(config.id);
return;
}
// Already running surface a meaningful error
throw new ValidationError(
`Process with id '${config.id}' already exists`,
'ERR_DUPLICATE_PROCESS',
@@ -246,7 +256,8 @@ export class ProcessManager extends EventEmitter {
try {
await monitor.stop();
this.updateProcessInfo(id, { status: 'stopped' });
// Ensure status and PID are reflected immediately
this.updateProcessInfo(id, { status: 'stopped', pid: undefined });
this.logger.info(`Successfully stopped process with id '${id}'`);
} catch (error: Error | unknown) {
const processError = new ProcessError(
@@ -430,6 +441,8 @@ export class ProcessManager extends EventEmitter {
const pid = monitor.getPid();
if (pid) {
info.pid = pid;
} else {
info.pid = undefined;
}
// Update uptime if available
@@ -449,9 +462,7 @@ export class ProcessManager extends EventEmitter {
info.restarts = monitor.getRestartCount();
// Update status based on actual running state
if (monitor.isRunning()) {
info.status = 'online';
}
info.status = monitor.isRunning() ? 'online' : 'stopped';
}
}