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

@@ -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,
};
},
);