BREAKING CHANGE(daemon): Refactor daemon and service management: remove IPC auto-spawn, add TspmServiceManager, tighten IPC/client/CLI behavior and tests

This commit is contained in:
2025-08-28 15:52:29 +00:00
parent 8e3cfb624b
commit e73f4acd63
38 changed files with 810 additions and 580 deletions

View File

@@ -7,7 +7,7 @@ import { formatMemory } from '../../helpers/memory.js';
export function registerDaemonCommand(smartcli: plugins.smartcli.Smartcli) {
const cliLogger = new Logger('CLI');
smartcli.addCommand('daemon').subscribe({
next: async (argvArg: CliArguments) => {
const subCommand = argvArg._[1];
@@ -27,7 +27,7 @@ export function registerDaemonCommand(smartcli: plugins.smartcli.Smartcli) {
}
console.log('Starting TSPM daemon manually...');
// Import spawn to start daemon process
const { spawn } = await import('child_process');
const daemonScript = plugins.path.join(
@@ -45,23 +45,25 @@ export function registerDaemonCommand(smartcli: plugins.smartcli.Smartcli) {
TSPM_DAEMON_MODE: 'true',
},
});
// Detach the daemon so it continues running after CLI exits
daemonProcess.unref();
console.log(`Started daemon with PID: ${daemonProcess.pid}`);
// Wait for daemon to be ready
await new Promise(resolve => setTimeout(resolve, 2000));
await new Promise((resolve) => setTimeout(resolve, 2000));
const newStatus = await tspmIpcClient.getDaemonStatus();
if (newStatus) {
console.log('✓ TSPM daemon started successfully');
console.log(` PID: ${newStatus.pid}`);
console.log('\nNote: This daemon will run until you stop it or logout.');
console.log(
'\nNote: This daemon will run until you stop it or logout.',
);
console.log('For automatic startup, use "tspm enable" instead.');
}
// Disconnect from the daemon after starting
await tspmIpcClient.disconnect();
} catch (error) {
@@ -69,7 +71,7 @@ export function registerDaemonCommand(smartcli: plugins.smartcli.Smartcli) {
process.exit(1);
}
break;
case 'start-service':
// This is called by systemd - start the daemon directly
console.log('Starting TSPM daemon for systemd service...');
@@ -82,7 +84,7 @@ export function registerDaemonCommand(smartcli: plugins.smartcli.Smartcli) {
console.log('Stopping TSPM daemon...');
await tspmIpcClient.stopDaemon(true);
console.log('✓ TSPM daemon stopped successfully');
// Disconnect from the daemon after stopping
await tspmIpcClient.disconnect();
} catch (error) {
@@ -112,7 +114,7 @@ export function registerDaemonCommand(smartcli: plugins.smartcli.Smartcli) {
`Memory: ${formatMemory(status.memoryUsage || 0)}`,
);
console.log(`CPU: ${status.cpuUsage?.toFixed(1) || 0}s`);
// Disconnect from daemon after getting status
await tspmIpcClient.disconnect();
} catch (error) {
@@ -135,4 +137,4 @@ export function registerDaemonCommand(smartcli: plugins.smartcli.Smartcli) {
},
complete: () => {},
});
}
}