feat(cli): Add stats CLI command and daemon stats aggregation; fix process manager & wrapper state handling
This commit is contained in:
@@ -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';
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -93,6 +93,9 @@ export class ProcessWrapper extends EventEmitter {
|
||||
this.stdoutRemainder = '';
|
||||
this.stderrRemainder = '';
|
||||
|
||||
// Mark process reference as gone so isRunning() reflects reality
|
||||
this.process = null;
|
||||
|
||||
this.emit('exit', code, signal);
|
||||
});
|
||||
|
||||
@@ -269,6 +272,7 @@ export class ProcessWrapper extends EventEmitter {
|
||||
* Get the process ID if running
|
||||
*/
|
||||
public getPid(): number | null {
|
||||
if (!this.isRunning()) return null;
|
||||
return this.process?.pid || null;
|
||||
}
|
||||
|
||||
@@ -292,7 +296,13 @@ export class ProcessWrapper extends EventEmitter {
|
||||
* Check if the process is currently running
|
||||
*/
|
||||
public isRunning(): boolean {
|
||||
return this.process !== null && typeof this.process.exitCode !== 'number';
|
||||
if (!this.process) return false;
|
||||
// In Node, while the child is running: exitCode === null and signalCode === null/undefined
|
||||
// After it exits: exitCode is a number OR signalCode is a string
|
||||
const anyProc: any = this.process as any;
|
||||
const exitCode = anyProc.exitCode;
|
||||
const signalCode = anyProc.signalCode;
|
||||
return exitCode === null && (signalCode === null || typeof signalCode === 'undefined');
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -10,6 +10,7 @@ import type {
|
||||
DaemonStatusResponse,
|
||||
HeartbeatResponse,
|
||||
} from '../shared/protocol/ipc.types.js';
|
||||
import { LogPersistence } from './logpersistence.js';
|
||||
|
||||
/**
|
||||
* Central daemon server that manages all TSPM processes
|
||||
@@ -170,7 +171,22 @@ export class TspmDaemon {
|
||||
throw new Error(`Process ${id} not found`);
|
||||
}
|
||||
await this.tspmInstance.setDesiredState(id, 'online');
|
||||
await this.tspmInstance.start(config);
|
||||
const existing = this.tspmInstance.processes.get(id);
|
||||
if (existing) {
|
||||
if (existing.isRunning()) {
|
||||
// Already running; return current status/pid
|
||||
const runningInfo = this.tspmInstance.processInfo.get(id);
|
||||
return {
|
||||
processId: id,
|
||||
pid: runningInfo?.pid,
|
||||
status: runningInfo?.status || 'online',
|
||||
};
|
||||
} else {
|
||||
await this.tspmInstance.restart(id);
|
||||
}
|
||||
} else {
|
||||
await this.tspmInstance.start(config);
|
||||
}
|
||||
const processInfo = this.tspmInstance.processInfo.get(id);
|
||||
return {
|
||||
processId: id,
|
||||
@@ -501,6 +517,28 @@ export class TspmDaemon {
|
||||
'daemon:status',
|
||||
async (request: RequestForMethod<'daemon:status'>) => {
|
||||
const memUsage = process.memoryUsage();
|
||||
// Aggregate log stats from monitors
|
||||
let totalLogCount = 0;
|
||||
let totalLogBytes = 0;
|
||||
const perProcess: Array<{ id: ProcessId; count: number; bytes: number }> = [];
|
||||
for (const [id, monitor] of this.tspmInstance.processes.entries()) {
|
||||
try {
|
||||
const logs = monitor.getLogs();
|
||||
const count = logs.length;
|
||||
const bytes = LogPersistence.calculateLogMemorySize(logs);
|
||||
totalLogCount += count;
|
||||
totalLogBytes += bytes;
|
||||
perProcess.push({ id, count, bytes });
|
||||
} catch {}
|
||||
}
|
||||
const pathsInfo = {
|
||||
tspmDir: paths.tspmDir,
|
||||
socketPath: this.socketPath,
|
||||
pidFile: this.daemonPidFile,
|
||||
};
|
||||
const configsInfo = {
|
||||
processConfigs: this.tspmInstance.processConfigs.size,
|
||||
};
|
||||
return {
|
||||
status: 'running',
|
||||
pid: process.pid,
|
||||
@@ -509,6 +547,13 @@ export class TspmDaemon {
|
||||
memoryUsage: memUsage.heapUsed,
|
||||
cpuUsage: process.cpuUsage().user / 1000000, // Convert to seconds
|
||||
version: this.version,
|
||||
logsInMemory: {
|
||||
totalCount: totalLogCount,
|
||||
totalBytes: totalLogBytes,
|
||||
perProcess,
|
||||
},
|
||||
paths: pathsInfo,
|
||||
configs: configsInfo,
|
||||
};
|
||||
},
|
||||
);
|
||||
|
Reference in New Issue
Block a user