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:
@@ -3,6 +3,6 @@
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@git.zone/tspm',
|
||||
version: '2.0.0',
|
||||
version: '3.0.0',
|
||||
description: 'a no fuzz process manager'
|
||||
}
|
||||
|
@@ -43,12 +43,12 @@ export class TspmDaemon {
|
||||
this.ipcServer = plugins.smartipc.SmartIpc.createServer({
|
||||
id: 'tspm-daemon',
|
||||
socketPath: this.socketPath,
|
||||
autoCleanupSocketFile: true, // Clean up stale sockets
|
||||
socketMode: 0o600, // Set proper permissions
|
||||
autoCleanupSocketFile: true, // Clean up stale sockets
|
||||
socketMode: 0o600, // Set proper permissions
|
||||
heartbeat: true,
|
||||
heartbeatInterval: 5000,
|
||||
heartbeatTimeout: 20000,
|
||||
heartbeatInitialGracePeriodMs: 10000 // Grace period for startup
|
||||
heartbeatInitialGracePeriodMs: 10000, // Grace period for startup
|
||||
});
|
||||
|
||||
// Register message handlers
|
||||
@@ -65,7 +65,7 @@ export class TspmDaemon {
|
||||
|
||||
// Load existing process configurations
|
||||
await this.tspmInstance.loadProcessConfigs();
|
||||
|
||||
|
||||
// Set up log publishing
|
||||
this.tspmInstance.on('process:log', ({ processId, log }) => {
|
||||
// Publish to topic for this process
|
||||
@@ -122,19 +122,22 @@ export class TspmDaemon {
|
||||
},
|
||||
);
|
||||
|
||||
this.ipcServer.onMessage('restart', async (request: RequestForMethod<'restart'>) => {
|
||||
try {
|
||||
await this.tspmInstance.restart(request.id);
|
||||
const processInfo = this.tspmInstance.processInfo.get(request.id);
|
||||
return {
|
||||
processId: request.id,
|
||||
pid: processInfo?.pid,
|
||||
status: processInfo?.status || 'stopped',
|
||||
};
|
||||
} catch (error) {
|
||||
throw new Error(`Failed to restart process: ${error.message}`);
|
||||
}
|
||||
});
|
||||
this.ipcServer.onMessage(
|
||||
'restart',
|
||||
async (request: RequestForMethod<'restart'>) => {
|
||||
try {
|
||||
await this.tspmInstance.restart(request.id);
|
||||
const processInfo = this.tspmInstance.processInfo.get(request.id);
|
||||
return {
|
||||
processId: request.id,
|
||||
pid: processInfo?.pid,
|
||||
status: processInfo?.status || 'stopped',
|
||||
};
|
||||
} catch (error) {
|
||||
throw new Error(`Failed to restart process: ${error.message}`);
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
this.ipcServer.onMessage(
|
||||
'delete',
|
||||
@@ -160,124 +163,148 @@ export class TspmDaemon {
|
||||
},
|
||||
);
|
||||
|
||||
this.ipcServer.onMessage('describe', async (request: RequestForMethod<'describe'>) => {
|
||||
const processInfo = await this.tspmInstance.describe(request.id);
|
||||
const config = this.tspmInstance.processConfigs.get(request.id);
|
||||
this.ipcServer.onMessage(
|
||||
'describe',
|
||||
async (request: RequestForMethod<'describe'>) => {
|
||||
const processInfo = await this.tspmInstance.describe(request.id);
|
||||
const config = this.tspmInstance.processConfigs.get(request.id);
|
||||
|
||||
if (!processInfo || !config) {
|
||||
throw new Error(`Process ${request.id} not found`);
|
||||
}
|
||||
if (!processInfo || !config) {
|
||||
throw new Error(`Process ${request.id} not found`);
|
||||
}
|
||||
|
||||
return {
|
||||
processInfo,
|
||||
config,
|
||||
};
|
||||
});
|
||||
return {
|
||||
processInfo,
|
||||
config,
|
||||
};
|
||||
},
|
||||
);
|
||||
|
||||
this.ipcServer.onMessage('getLogs', async (request: RequestForMethod<'getLogs'>) => {
|
||||
const logs = await this.tspmInstance.getLogs(request.id);
|
||||
return { logs };
|
||||
});
|
||||
this.ipcServer.onMessage(
|
||||
'getLogs',
|
||||
async (request: RequestForMethod<'getLogs'>) => {
|
||||
const logs = await this.tspmInstance.getLogs(request.id);
|
||||
return { logs };
|
||||
},
|
||||
);
|
||||
|
||||
// Batch operations handlers
|
||||
this.ipcServer.onMessage('startAll', async (request: RequestForMethod<'startAll'>) => {
|
||||
const started: string[] = [];
|
||||
const failed: Array<{ id: string; error: string }> = [];
|
||||
this.ipcServer.onMessage(
|
||||
'startAll',
|
||||
async (request: RequestForMethod<'startAll'>) => {
|
||||
const started: string[] = [];
|
||||
const failed: Array<{ id: string; error: string }> = [];
|
||||
|
||||
await this.tspmInstance.startAll();
|
||||
await this.tspmInstance.startAll();
|
||||
|
||||
// Get status of all processes
|
||||
for (const [id, processInfo] of this.tspmInstance.processInfo) {
|
||||
if (processInfo.status === 'online') {
|
||||
started.push(id);
|
||||
} else {
|
||||
failed.push({ id, error: 'Failed to start' });
|
||||
// Get status of all processes
|
||||
for (const [id, processInfo] of this.tspmInstance.processInfo) {
|
||||
if (processInfo.status === 'online') {
|
||||
started.push(id);
|
||||
} else {
|
||||
failed.push({ id, error: 'Failed to start' });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return { started, failed };
|
||||
});
|
||||
return { started, failed };
|
||||
},
|
||||
);
|
||||
|
||||
this.ipcServer.onMessage('stopAll', async (request: RequestForMethod<'stopAll'>) => {
|
||||
const stopped: string[] = [];
|
||||
const failed: Array<{ id: string; error: string }> = [];
|
||||
this.ipcServer.onMessage(
|
||||
'stopAll',
|
||||
async (request: RequestForMethod<'stopAll'>) => {
|
||||
const stopped: string[] = [];
|
||||
const failed: Array<{ id: string; error: string }> = [];
|
||||
|
||||
await this.tspmInstance.stopAll();
|
||||
await this.tspmInstance.stopAll();
|
||||
|
||||
// Get status of all processes
|
||||
for (const [id, processInfo] of this.tspmInstance.processInfo) {
|
||||
if (processInfo.status === 'stopped') {
|
||||
stopped.push(id);
|
||||
} else {
|
||||
failed.push({ id, error: 'Failed to stop' });
|
||||
// Get status of all processes
|
||||
for (const [id, processInfo] of this.tspmInstance.processInfo) {
|
||||
if (processInfo.status === 'stopped') {
|
||||
stopped.push(id);
|
||||
} else {
|
||||
failed.push({ id, error: 'Failed to stop' });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return { stopped, failed };
|
||||
});
|
||||
return { stopped, failed };
|
||||
},
|
||||
);
|
||||
|
||||
this.ipcServer.onMessage('restartAll', async (request: RequestForMethod<'restartAll'>) => {
|
||||
const restarted: string[] = [];
|
||||
const failed: Array<{ id: string; error: string }> = [];
|
||||
this.ipcServer.onMessage(
|
||||
'restartAll',
|
||||
async (request: RequestForMethod<'restartAll'>) => {
|
||||
const restarted: string[] = [];
|
||||
const failed: Array<{ id: string; error: string }> = [];
|
||||
|
||||
await this.tspmInstance.restartAll();
|
||||
await this.tspmInstance.restartAll();
|
||||
|
||||
// Get status of all processes
|
||||
for (const [id, processInfo] of this.tspmInstance.processInfo) {
|
||||
if (processInfo.status === 'online') {
|
||||
restarted.push(id);
|
||||
} else {
|
||||
failed.push({ id, error: 'Failed to restart' });
|
||||
// Get status of all processes
|
||||
for (const [id, processInfo] of this.tspmInstance.processInfo) {
|
||||
if (processInfo.status === 'online') {
|
||||
restarted.push(id);
|
||||
} else {
|
||||
failed.push({ id, error: 'Failed to restart' });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return { restarted, failed };
|
||||
});
|
||||
return { restarted, failed };
|
||||
},
|
||||
);
|
||||
|
||||
// Daemon management handlers
|
||||
this.ipcServer.onMessage('daemon:status', async (request: RequestForMethod<'daemon:status'>) => {
|
||||
const memUsage = process.memoryUsage();
|
||||
return {
|
||||
status: 'running',
|
||||
pid: process.pid,
|
||||
uptime: Date.now() - this.startTime,
|
||||
processCount: this.tspmInstance.processes.size,
|
||||
memoryUsage: memUsage.heapUsed,
|
||||
cpuUsage: process.cpuUsage().user / 1000000, // Convert to seconds
|
||||
};
|
||||
});
|
||||
|
||||
this.ipcServer.onMessage('daemon:shutdown', async (request: RequestForMethod<'daemon:shutdown'>) => {
|
||||
if (this.isShuttingDown) {
|
||||
this.ipcServer.onMessage(
|
||||
'daemon:status',
|
||||
async (request: RequestForMethod<'daemon:status'>) => {
|
||||
const memUsage = process.memoryUsage();
|
||||
return {
|
||||
success: false,
|
||||
message: 'Daemon is already shutting down',
|
||||
status: 'running',
|
||||
pid: process.pid,
|
||||
uptime: Date.now() - this.startTime,
|
||||
processCount: this.tspmInstance.processes.size,
|
||||
memoryUsage: memUsage.heapUsed,
|
||||
cpuUsage: process.cpuUsage().user / 1000000, // Convert to seconds
|
||||
};
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
// Schedule shutdown
|
||||
const graceful = request.graceful !== false;
|
||||
const timeout = request.timeout || 10000;
|
||||
this.ipcServer.onMessage(
|
||||
'daemon:shutdown',
|
||||
async (request: RequestForMethod<'daemon:shutdown'>) => {
|
||||
if (this.isShuttingDown) {
|
||||
return {
|
||||
success: false,
|
||||
message: 'Daemon is already shutting down',
|
||||
};
|
||||
}
|
||||
|
||||
if (graceful) {
|
||||
setTimeout(() => this.shutdown(true), 100);
|
||||
} else {
|
||||
setTimeout(() => this.shutdown(false), 100);
|
||||
}
|
||||
// Schedule shutdown
|
||||
const graceful = request.graceful !== false;
|
||||
const timeout = request.timeout || 10000;
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: `Daemon will shutdown ${graceful ? 'gracefully' : 'immediately'} in ${timeout}ms`,
|
||||
};
|
||||
});
|
||||
if (graceful) {
|
||||
setTimeout(() => this.shutdown(true), 100);
|
||||
} else {
|
||||
setTimeout(() => this.shutdown(false), 100);
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: `Daemon will shutdown ${graceful ? 'gracefully' : 'immediately'} in ${timeout}ms`,
|
||||
};
|
||||
},
|
||||
);
|
||||
|
||||
// Heartbeat handler
|
||||
this.ipcServer.onMessage('heartbeat', async (request: RequestForMethod<'heartbeat'>) => {
|
||||
return {
|
||||
timestamp: Date.now(),
|
||||
status: this.isShuttingDown ? 'degraded' : 'healthy',
|
||||
};
|
||||
});
|
||||
this.ipcServer.onMessage(
|
||||
'heartbeat',
|
||||
async (request: RequestForMethod<'heartbeat'>) => {
|
||||
return {
|
||||
timestamp: Date.now(),
|
||||
status: this.isShuttingDown ? 'degraded' : 'healthy',
|
||||
};
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -36,9 +36,9 @@ export class TspmIpcClient {
|
||||
if (!daemonRunning) {
|
||||
throw new Error(
|
||||
'TSPM daemon is not running.\n\n' +
|
||||
'To start the daemon, run one of:\n' +
|
||||
' tspm daemon start - Start daemon for this session\n' +
|
||||
' tspm enable - Enable daemon as system service (recommended)\n'
|
||||
'To start the daemon, run one of:\n' +
|
||||
' tspm daemon start - Start daemon for this session\n' +
|
||||
' tspm enable - Enable daemon as system service (recommended)\n',
|
||||
);
|
||||
}
|
||||
|
||||
@@ -59,20 +59,20 @@ export class TspmIpcClient {
|
||||
heartbeatInterval: 5000,
|
||||
heartbeatTimeout: 20000,
|
||||
heartbeatInitialGracePeriodMs: 10000,
|
||||
heartbeatThrowOnTimeout: false // Don't throw, emit events instead
|
||||
heartbeatThrowOnTimeout: false, // Don't throw, emit events instead
|
||||
});
|
||||
|
||||
// Connect to the daemon
|
||||
try {
|
||||
await this.ipcClient.connect({ waitForReady: true });
|
||||
this.isConnected = true;
|
||||
|
||||
|
||||
// Handle heartbeat timeouts gracefully
|
||||
this.ipcClient.on('heartbeatTimeout', () => {
|
||||
console.warn('Heartbeat timeout detected, connection may be degraded');
|
||||
this.isConnected = false;
|
||||
});
|
||||
|
||||
|
||||
console.log('Connected to TSPM daemon');
|
||||
} catch (error) {
|
||||
console.error('Failed to connect to daemon:', error);
|
||||
@@ -117,19 +117,22 @@ export class TspmIpcClient {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Subscribe to log updates for a specific process
|
||||
*/
|
||||
public async subscribe(processId: string, handler: (log: any) => void): Promise<void> {
|
||||
public async subscribe(
|
||||
processId: string,
|
||||
handler: (log: any) => void,
|
||||
): Promise<void> {
|
||||
if (!this.ipcClient || !this.isConnected) {
|
||||
throw new Error('Not connected to daemon');
|
||||
}
|
||||
|
||||
|
||||
const topic = `logs.${processId}`;
|
||||
await this.ipcClient.subscribe(`topic:${topic}`, handler);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Unsubscribe from log updates for a specific process
|
||||
*/
|
||||
@@ -137,7 +140,7 @@ export class TspmIpcClient {
|
||||
if (!this.ipcClient || !this.isConnected) {
|
||||
throw new Error('Not connected to daemon');
|
||||
}
|
||||
|
||||
|
||||
const topic = `logs.${processId}`;
|
||||
await this.ipcClient.unsubscribe(`topic:${topic}`);
|
||||
}
|
||||
@@ -160,7 +163,7 @@ export class TspmIpcClient {
|
||||
// Check if process is running
|
||||
try {
|
||||
process.kill(pid, 0);
|
||||
|
||||
|
||||
// PID is alive, daemon is running
|
||||
// Socket check is advisory only - the connect retry will handle transient socket issues
|
||||
try {
|
||||
@@ -184,8 +187,6 @@ export class TspmIpcClient {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Stop the daemon
|
||||
*/
|
||||
|
@@ -69,7 +69,7 @@ export class ProcessMonitor extends EventEmitter {
|
||||
this.processWrapper.on('log', (log: IProcessLog): void => {
|
||||
// Re-emit the log event for upstream handlers
|
||||
this.emit('log', log);
|
||||
|
||||
|
||||
// Log system messages to the console
|
||||
if (log.type === 'system') {
|
||||
this.log(log.message);
|
||||
|
@@ -18,14 +18,14 @@ export class TspmServiceManager {
|
||||
private async getOrCreateService(): Promise<any> {
|
||||
if (!this.service) {
|
||||
const cliPath = plugins.path.join(paths.packageDir, 'cli.js');
|
||||
|
||||
|
||||
// Create service configuration
|
||||
this.service = await this.smartDaemon.addService({
|
||||
name: 'tspm-daemon',
|
||||
description: 'TSPM Process Manager Daemon',
|
||||
command: `${process.execPath} ${cliPath} daemon start-service`,
|
||||
workingDir: process.env.HOME || process.cwd(),
|
||||
version: '1.0.0'
|
||||
version: '1.0.0',
|
||||
});
|
||||
}
|
||||
return this.service;
|
||||
@@ -36,13 +36,13 @@ export class TspmServiceManager {
|
||||
*/
|
||||
public async enableService(): Promise<void> {
|
||||
const service = await this.getOrCreateService();
|
||||
|
||||
|
||||
// Save service configuration
|
||||
await service.save();
|
||||
|
||||
|
||||
// Enable service to start on boot
|
||||
await service.enable();
|
||||
|
||||
|
||||
// Start the service immediately
|
||||
await service.start();
|
||||
}
|
||||
@@ -52,7 +52,7 @@ export class TspmServiceManager {
|
||||
*/
|
||||
public async disableService(): Promise<void> {
|
||||
const service = await this.getOrCreateService();
|
||||
|
||||
|
||||
// Stop the service if running
|
||||
try {
|
||||
await service.stop();
|
||||
@@ -60,7 +60,7 @@ export class TspmServiceManager {
|
||||
// Service might not be running
|
||||
console.log('Service was not running');
|
||||
}
|
||||
|
||||
|
||||
// Disable service from starting on boot
|
||||
await service.disable();
|
||||
}
|
||||
@@ -75,20 +75,20 @@ export class TspmServiceManager {
|
||||
}> {
|
||||
try {
|
||||
await this.getOrCreateService();
|
||||
|
||||
|
||||
// Note: SmartDaemon doesn't provide direct status methods,
|
||||
// so we'll need to check via systemctl commands
|
||||
// This is a simplified implementation
|
||||
return {
|
||||
enabled: true, // Would need to check systemctl is-enabled
|
||||
running: true, // Would need to check systemctl is-active
|
||||
status: 'active'
|
||||
status: 'active',
|
||||
};
|
||||
} catch (error) {
|
||||
return {
|
||||
enabled: false,
|
||||
running: false,
|
||||
status: 'inactive'
|
||||
status: 'inactive',
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -100,4 +100,4 @@ export class TspmServiceManager {
|
||||
const service = await this.getOrCreateService();
|
||||
await service.reload();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -32,8 +32,6 @@ export interface IProcessInfo {
|
||||
restarts: number;
|
||||
}
|
||||
|
||||
|
||||
|
||||
export class Tspm extends EventEmitter {
|
||||
public processes: Map<string, ProcessMonitor> = new Map();
|
||||
public processConfigs: Map<string, IProcessConfig> = new Map();
|
||||
@@ -97,12 +95,12 @@ export class Tspm extends EventEmitter {
|
||||
});
|
||||
|
||||
this.processes.set(config.id, monitor);
|
||||
|
||||
|
||||
// Set up log event handler to re-emit for pub/sub
|
||||
monitor.on('log', (log: IProcessLog) => {
|
||||
this.emit('process:log', { processId: config.id, log });
|
||||
});
|
||||
|
||||
|
||||
monitor.start();
|
||||
|
||||
// Update process info
|
||||
|
@@ -1,2 +1,2 @@
|
||||
// Re-export from the new modular CLI structure
|
||||
export * from './cli/index.js';
|
||||
export * from './cli/index.js';
|
||||
|
@@ -4,23 +4,28 @@ import type { CliArguments } from '../../types.js';
|
||||
import { registerIpcCommand } from '../../registration/index.js';
|
||||
|
||||
export function registerRestartAllCommand(smartcli: plugins.smartcli.Smartcli) {
|
||||
registerIpcCommand(smartcli, 'restart-all', async (_argvArg: CliArguments) => {
|
||||
console.log('Restarting all processes...');
|
||||
const response = await tspmIpcClient.request('restartAll', {});
|
||||
registerIpcCommand(
|
||||
smartcli,
|
||||
'restart-all',
|
||||
async (_argvArg: CliArguments) => {
|
||||
console.log('Restarting all processes...');
|
||||
const response = await tspmIpcClient.request('restartAll', {});
|
||||
|
||||
if (response.restarted.length > 0) {
|
||||
console.log(`✓ Restarted ${response.restarted.length} processes:`);
|
||||
for (const id of response.restarted) {
|
||||
console.log(` - ${id}`);
|
||||
if (response.restarted.length > 0) {
|
||||
console.log(`✓ Restarted ${response.restarted.length} processes:`);
|
||||
for (const id of response.restarted) {
|
||||
console.log(` - ${id}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (response.failed.length > 0) {
|
||||
console.log(`✗ Failed to restart ${response.failed.length} processes:`);
|
||||
for (const failure of response.failed) {
|
||||
console.log(` - ${failure.id}: ${failure.error}`);
|
||||
if (response.failed.length > 0) {
|
||||
console.log(`✗ Failed to restart ${response.failed.length} processes:`);
|
||||
for (const failure of response.failed) {
|
||||
console.log(` - ${failure.id}: ${failure.error}`);
|
||||
}
|
||||
process.exitCode = 1; // Signal partial failure
|
||||
}
|
||||
process.exitCode = 1; // Signal partial failure
|
||||
}
|
||||
}, { actionLabel: 'restart all processes' });
|
||||
}
|
||||
},
|
||||
{ actionLabel: 'restart all processes' },
|
||||
);
|
||||
}
|
||||
|
@@ -4,23 +4,28 @@ import type { CliArguments } from '../../types.js';
|
||||
import { registerIpcCommand } from '../../registration/index.js';
|
||||
|
||||
export function registerStartAllCommand(smartcli: plugins.smartcli.Smartcli) {
|
||||
registerIpcCommand(smartcli, 'start-all', async (_argvArg: CliArguments) => {
|
||||
console.log('Starting all processes...');
|
||||
const response = await tspmIpcClient.request('startAll', {});
|
||||
registerIpcCommand(
|
||||
smartcli,
|
||||
'start-all',
|
||||
async (_argvArg: CliArguments) => {
|
||||
console.log('Starting all processes...');
|
||||
const response = await tspmIpcClient.request('startAll', {});
|
||||
|
||||
if (response.started.length > 0) {
|
||||
console.log(`✓ Started ${response.started.length} processes:`);
|
||||
for (const id of response.started) {
|
||||
console.log(` - ${id}`);
|
||||
if (response.started.length > 0) {
|
||||
console.log(`✓ Started ${response.started.length} processes:`);
|
||||
for (const id of response.started) {
|
||||
console.log(` - ${id}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (response.failed.length > 0) {
|
||||
console.log(`✗ Failed to start ${response.failed.length} processes:`);
|
||||
for (const failure of response.failed) {
|
||||
console.log(` - ${failure.id}: ${failure.error}`);
|
||||
if (response.failed.length > 0) {
|
||||
console.log(`✗ Failed to start ${response.failed.length} processes:`);
|
||||
for (const failure of response.failed) {
|
||||
console.log(` - ${failure.id}: ${failure.error}`);
|
||||
}
|
||||
process.exitCode = 1; // Signal partial failure
|
||||
}
|
||||
process.exitCode = 1; // Signal partial failure
|
||||
}
|
||||
}, { actionLabel: 'start all processes' });
|
||||
}
|
||||
},
|
||||
{ actionLabel: 'start all processes' },
|
||||
);
|
||||
}
|
||||
|
@@ -4,23 +4,28 @@ import type { CliArguments } from '../../types.js';
|
||||
import { registerIpcCommand } from '../../registration/index.js';
|
||||
|
||||
export function registerStopAllCommand(smartcli: plugins.smartcli.Smartcli) {
|
||||
registerIpcCommand(smartcli, 'stop-all', async (_argvArg: CliArguments) => {
|
||||
console.log('Stopping all processes...');
|
||||
const response = await tspmIpcClient.request('stopAll', {});
|
||||
registerIpcCommand(
|
||||
smartcli,
|
||||
'stop-all',
|
||||
async (_argvArg: CliArguments) => {
|
||||
console.log('Stopping all processes...');
|
||||
const response = await tspmIpcClient.request('stopAll', {});
|
||||
|
||||
if (response.stopped.length > 0) {
|
||||
console.log(`✓ Stopped ${response.stopped.length} processes:`);
|
||||
for (const id of response.stopped) {
|
||||
console.log(` - ${id}`);
|
||||
if (response.stopped.length > 0) {
|
||||
console.log(`✓ Stopped ${response.stopped.length} processes:`);
|
||||
for (const id of response.stopped) {
|
||||
console.log(` - ${id}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (response.failed.length > 0) {
|
||||
console.log(`✗ Failed to stop ${response.failed.length} processes:`);
|
||||
for (const failure of response.failed) {
|
||||
console.log(` - ${failure.id}: ${failure.error}`);
|
||||
if (response.failed.length > 0) {
|
||||
console.log(`✗ Failed to stop ${response.failed.length} processes:`);
|
||||
for (const failure of response.failed) {
|
||||
console.log(` - ${failure.id}: ${failure.error}`);
|
||||
}
|
||||
process.exitCode = 1; // Signal partial failure
|
||||
}
|
||||
process.exitCode = 1; // Signal partial failure
|
||||
}
|
||||
}, { actionLabel: 'stop all processes' });
|
||||
}
|
||||
},
|
||||
{ actionLabel: 'stop all processes' },
|
||||
);
|
||||
}
|
||||
|
@@ -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: () => {},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@@ -9,7 +9,7 @@ import { formatMemory } from '../helpers/memory.js';
|
||||
export function registerDefaultCommand(smartcli: plugins.smartcli.Smartcli) {
|
||||
const cliLogger = new Logger('CLI');
|
||||
const tspmProjectinfo = new plugins.projectinfo.ProjectInfo(paths.packageDir);
|
||||
|
||||
|
||||
smartcli.standardCommand().subscribe({
|
||||
next: async (argvArg: CliArguments) => {
|
||||
console.log(
|
||||
@@ -17,7 +17,9 @@ export function registerDefaultCommand(smartcli: plugins.smartcli.Smartcli) {
|
||||
);
|
||||
console.log('Usage: tspm [command] [options]');
|
||||
console.log('\nService Management:');
|
||||
console.log(' enable Enable TSPM as system service (systemd)');
|
||||
console.log(
|
||||
' enable Enable TSPM as system service (systemd)',
|
||||
);
|
||||
console.log(' disable Disable TSPM system service');
|
||||
console.log('\nProcess Commands:');
|
||||
console.log(' start <script> Start a process');
|
||||
@@ -31,7 +33,9 @@ export function registerDefaultCommand(smartcli: plugins.smartcli.Smartcli) {
|
||||
console.log(' stop-all Stop all processes');
|
||||
console.log(' restart-all Restart all processes');
|
||||
console.log('\nDaemon Commands:');
|
||||
console.log(' daemon start Start daemon manually (current session)');
|
||||
console.log(
|
||||
' daemon start Start daemon manually (current session)',
|
||||
);
|
||||
console.log(' daemon stop Stop the daemon');
|
||||
console.log(' daemon status Show daemon status');
|
||||
console.log(
|
||||
@@ -78,14 +82,16 @@ export function registerDefaultCommand(smartcli: plugins.smartcli.Smartcli) {
|
||||
'└─────────┴─────────────┴───────────┴───────────┴──────────┘',
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
// Disconnect from daemon after getting list
|
||||
await tspmIpcClient.disconnect();
|
||||
} catch (error) {
|
||||
console.error('Error: TSPM daemon is not running.');
|
||||
console.log('\nTo start the daemon, run one of:');
|
||||
console.log(' tspm daemon start - Start for this session only');
|
||||
console.log(' tspm enable - Enable as system service (recommended)');
|
||||
console.log(
|
||||
' tspm enable - Enable as system service (recommended)',
|
||||
);
|
||||
}
|
||||
},
|
||||
error: (err) => {
|
||||
@@ -93,4 +99,4 @@ export function registerDefaultCommand(smartcli: plugins.smartcli.Smartcli) {
|
||||
},
|
||||
complete: () => {},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@@ -4,21 +4,26 @@ import type { CliArguments } from '../../types.js';
|
||||
import { registerIpcCommand } from '../../registration/index.js';
|
||||
|
||||
export function registerDeleteCommand(smartcli: plugins.smartcli.Smartcli) {
|
||||
registerIpcCommand(smartcli, 'delete', async (argvArg: CliArguments) => {
|
||||
const id = argvArg._[1];
|
||||
if (!id) {
|
||||
console.error('Error: Please provide a process ID');
|
||||
console.log('Usage: tspm delete <id>');
|
||||
return;
|
||||
}
|
||||
registerIpcCommand(
|
||||
smartcli,
|
||||
'delete',
|
||||
async (argvArg: CliArguments) => {
|
||||
const id = argvArg._[1];
|
||||
if (!id) {
|
||||
console.error('Error: Please provide a process ID');
|
||||
console.log('Usage: tspm delete <id>');
|
||||
return;
|
||||
}
|
||||
|
||||
console.log(`Deleting process: ${id}`);
|
||||
const response = await tspmIpcClient.request('delete', { id });
|
||||
console.log(`Deleting process: ${id}`);
|
||||
const response = await tspmIpcClient.request('delete', { id });
|
||||
|
||||
if (response.success) {
|
||||
console.log(`✓ ${response.message}`);
|
||||
} else {
|
||||
console.error(`✗ Failed to delete process: ${response.message}`);
|
||||
}
|
||||
}, { actionLabel: 'delete process' });
|
||||
}
|
||||
if (response.success) {
|
||||
console.log(`✓ ${response.message}`);
|
||||
} else {
|
||||
console.error(`✗ Failed to delete process: ${response.message}`);
|
||||
}
|
||||
},
|
||||
{ actionLabel: 'delete process' },
|
||||
);
|
||||
}
|
||||
|
@@ -5,34 +5,45 @@ import { registerIpcCommand } from '../../registration/index.js';
|
||||
import { formatMemory } from '../../helpers/memory.js';
|
||||
|
||||
export function registerDescribeCommand(smartcli: plugins.smartcli.Smartcli) {
|
||||
registerIpcCommand(smartcli, 'describe', async (argvArg: CliArguments) => {
|
||||
const id = argvArg._[1];
|
||||
if (!id) {
|
||||
console.error('Error: Please provide a process ID');
|
||||
console.log('Usage: tspm describe <id>');
|
||||
return;
|
||||
}
|
||||
|
||||
const response = await tspmIpcClient.request('describe', { id });
|
||||
|
||||
console.log(`Process Details: ${id}`);
|
||||
console.log('─'.repeat(40));
|
||||
console.log(`Status: ${response.processInfo.status}`);
|
||||
console.log(`PID: ${response.processInfo.pid || 'N/A'}`);
|
||||
console.log(`Memory: ${formatMemory(response.processInfo.memory)}`);
|
||||
console.log(`CPU: ${response.processInfo.cpu ? response.processInfo.cpu.toFixed(1) + '%' : 'N/A'}`);
|
||||
console.log(`Uptime: ${response.processInfo.uptime ? Math.floor(response.processInfo.uptime / 1000) + 's' : 'N/A'}`);
|
||||
console.log(`Restarts: ${response.processInfo.restarts}`);
|
||||
console.log('\nConfiguration:');
|
||||
console.log(`Command: ${response.config.command}`);
|
||||
console.log(`Directory: ${response.config.projectDir}`);
|
||||
console.log(`Memory Limit: ${formatMemory(response.config.memoryLimitBytes)}`);
|
||||
console.log(`Auto-restart: ${response.config.autorestart}`);
|
||||
if (response.config.watch) {
|
||||
console.log(`Watch: enabled`);
|
||||
if (response.config.watchPaths) {
|
||||
console.log(`Watch Paths: ${response.config.watchPaths.join(', ')}`);
|
||||
registerIpcCommand(
|
||||
smartcli,
|
||||
'describe',
|
||||
async (argvArg: CliArguments) => {
|
||||
const id = argvArg._[1];
|
||||
if (!id) {
|
||||
console.error('Error: Please provide a process ID');
|
||||
console.log('Usage: tspm describe <id>');
|
||||
return;
|
||||
}
|
||||
}
|
||||
}, { actionLabel: 'describe process' });
|
||||
}
|
||||
|
||||
const response = await tspmIpcClient.request('describe', { id });
|
||||
|
||||
console.log(`Process Details: ${id}`);
|
||||
console.log('─'.repeat(40));
|
||||
console.log(`Status: ${response.processInfo.status}`);
|
||||
console.log(`PID: ${response.processInfo.pid || 'N/A'}`);
|
||||
console.log(`Memory: ${formatMemory(response.processInfo.memory)}`);
|
||||
console.log(
|
||||
`CPU: ${response.processInfo.cpu ? response.processInfo.cpu.toFixed(1) + '%' : 'N/A'}`,
|
||||
);
|
||||
console.log(
|
||||
`Uptime: ${response.processInfo.uptime ? Math.floor(response.processInfo.uptime / 1000) + 's' : 'N/A'}`,
|
||||
);
|
||||
console.log(`Restarts: ${response.processInfo.restarts}`);
|
||||
console.log('\nConfiguration:');
|
||||
console.log(`Command: ${response.config.command}`);
|
||||
console.log(`Directory: ${response.config.projectDir}`);
|
||||
console.log(
|
||||
`Memory Limit: ${formatMemory(response.config.memoryLimitBytes)}`,
|
||||
);
|
||||
console.log(`Auto-restart: ${response.config.autorestart}`);
|
||||
if (response.config.watch) {
|
||||
console.log(`Watch: enabled`);
|
||||
if (response.config.watchPaths) {
|
||||
console.log(`Watch Paths: ${response.config.watchPaths.join(', ')}`);
|
||||
}
|
||||
}
|
||||
},
|
||||
{ actionLabel: 'describe process' },
|
||||
);
|
||||
}
|
||||
|
@@ -6,32 +6,47 @@ import { pad } from '../../helpers/formatting.js';
|
||||
import { formatMemory } from '../../helpers/memory.js';
|
||||
|
||||
export function registerListCommand(smartcli: plugins.smartcli.Smartcli) {
|
||||
registerIpcCommand(smartcli, 'list', async (_argvArg: CliArguments) => {
|
||||
const response = await tspmIpcClient.request('list', {});
|
||||
const processes = response.processes;
|
||||
registerIpcCommand(
|
||||
smartcli,
|
||||
'list',
|
||||
async (_argvArg: CliArguments) => {
|
||||
const response = await tspmIpcClient.request('list', {});
|
||||
const processes = response.processes;
|
||||
|
||||
if (processes.length === 0) {
|
||||
console.log('No processes running.');
|
||||
return;
|
||||
}
|
||||
if (processes.length === 0) {
|
||||
console.log('No processes running.');
|
||||
return;
|
||||
}
|
||||
|
||||
console.log('Process List:');
|
||||
console.log('┌─────────┬─────────────┬───────────┬───────────┬──────────┬──────────┐');
|
||||
console.log('│ ID │ Name │ Status │ PID │ Memory │ Restarts │');
|
||||
console.log('├─────────┼─────────────┼───────────┼───────────┼──────────┼──────────┤');
|
||||
console.log('Process List:');
|
||||
console.log(
|
||||
'┌─────────┬─────────────┬───────────┬───────────┬──────────┬──────────┐',
|
||||
);
|
||||
console.log(
|
||||
'│ ID │ Name │ Status │ PID │ Memory │ Restarts │',
|
||||
);
|
||||
console.log(
|
||||
'├─────────┼─────────────┼───────────┼───────────┼──────────┼──────────┤',
|
||||
);
|
||||
|
||||
for (const proc of processes) {
|
||||
const statusColor =
|
||||
proc.status === 'online' ? '\x1b[32m' :
|
||||
proc.status === 'errored' ? '\x1b[31m' :
|
||||
'\x1b[33m';
|
||||
const resetColor = '\x1b[0m';
|
||||
for (const proc of processes) {
|
||||
const statusColor =
|
||||
proc.status === 'online'
|
||||
? '\x1b[32m'
|
||||
: proc.status === 'errored'
|
||||
? '\x1b[31m'
|
||||
: '\x1b[33m';
|
||||
const resetColor = '\x1b[0m';
|
||||
|
||||
console.log(
|
||||
`│ ${pad(proc.id, 7)} │ ${pad(proc.id, 11)} │ ${statusColor}${pad(proc.status, 9)}${resetColor} │ ${pad((proc.pid || '-').toString(), 9)} │ ${pad(formatMemory(proc.memory), 8)} │ ${pad(proc.restarts.toString(), 8)} │`,
|
||||
);
|
||||
}
|
||||
|
||||
console.log(
|
||||
`│ ${pad(proc.id, 7)} │ ${pad(proc.id, 11)} │ ${statusColor}${pad(proc.status, 9)}${resetColor} │ ${pad((proc.pid || '-').toString(), 9)} │ ${pad(formatMemory(proc.memory), 8)} │ ${pad(proc.restarts.toString(), 8)} │`,
|
||||
'└─────────┴─────────────┴───────────┴───────────┴──────────┴──────────┘',
|
||||
);
|
||||
}
|
||||
|
||||
console.log('└─────────┴─────────────┴───────────┴───────────┴──────────┴──────────┘');
|
||||
}, { actionLabel: 'list processes' });
|
||||
}
|
||||
},
|
||||
{ actionLabel: 'list processes' },
|
||||
);
|
||||
}
|
||||
|
@@ -7,67 +7,93 @@ import { formatLog } from '../../helpers/formatting.js';
|
||||
import { withStreamingLifecycle } from '../../helpers/lifecycle.js';
|
||||
|
||||
export function registerLogsCommand(smartcli: plugins.smartcli.Smartcli) {
|
||||
registerIpcCommand(smartcli, 'logs', async (argvArg: CliArguments) => {
|
||||
const id = argvArg._[1];
|
||||
if (!id) {
|
||||
console.error('Error: Please provide a process ID');
|
||||
console.log('Usage: tspm logs <id> [options]');
|
||||
console.log('\nOptions:');
|
||||
console.log(' --lines <n> Number of lines to show (default: 50)');
|
||||
console.log(' --follow Stream logs in real-time (like tail -f)');
|
||||
return;
|
||||
}
|
||||
registerIpcCommand(
|
||||
smartcli,
|
||||
'logs',
|
||||
async (argvArg: CliArguments) => {
|
||||
const id = argvArg._[1];
|
||||
if (!id) {
|
||||
console.error('Error: Please provide a process ID');
|
||||
console.log('Usage: tspm logs <id> [options]');
|
||||
console.log('\nOptions:');
|
||||
console.log(' --lines <n> Number of lines to show (default: 50)');
|
||||
console.log(' --follow Stream logs in real-time (like tail -f)');
|
||||
return;
|
||||
}
|
||||
|
||||
const lines = getNumber(argvArg, 'lines', 50);
|
||||
const follow = getBool(argvArg, 'follow', 'f');
|
||||
const lines = getNumber(argvArg, 'lines', 50);
|
||||
const follow = getBool(argvArg, 'follow', 'f');
|
||||
|
||||
const response = await tspmIpcClient.request('getLogs', { id, lines });
|
||||
|
||||
if (!follow) {
|
||||
// One-shot mode - auto-disconnect handled by registerIpcCommand
|
||||
console.log(`Logs for process: ${id} (last ${lines} lines)`);
|
||||
const response = await tspmIpcClient.request('getLogs', { id, lines });
|
||||
|
||||
if (!follow) {
|
||||
// One-shot mode - auto-disconnect handled by registerIpcCommand
|
||||
console.log(`Logs for process: ${id} (last ${lines} lines)`);
|
||||
console.log('─'.repeat(60));
|
||||
for (const log of response.logs) {
|
||||
const timestamp = new Date(log.timestamp).toLocaleTimeString();
|
||||
const prefix =
|
||||
log.type === 'stdout'
|
||||
? '[OUT]'
|
||||
: log.type === 'stderr'
|
||||
? '[ERR]'
|
||||
: '[SYS]';
|
||||
console.log(`${timestamp} ${prefix} ${log.message}`);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Streaming mode
|
||||
console.log(`Logs for process: ${id} (streaming...)`);
|
||||
console.log('─'.repeat(60));
|
||||
|
||||
let lastSeq = 0;
|
||||
for (const log of response.logs) {
|
||||
const timestamp = new Date(log.timestamp).toLocaleTimeString();
|
||||
const prefix = log.type === 'stdout' ? '[OUT]' : log.type === 'stderr' ? '[ERR]' : '[SYS]';
|
||||
const prefix =
|
||||
log.type === 'stdout'
|
||||
? '[OUT]'
|
||||
: log.type === 'stderr'
|
||||
? '[ERR]'
|
||||
: '[SYS]';
|
||||
console.log(`${timestamp} ${prefix} ${log.message}`);
|
||||
if (log.seq !== undefined) lastSeq = Math.max(lastSeq, log.seq);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Streaming mode
|
||||
console.log(`Logs for process: ${id} (streaming...)`);
|
||||
console.log('─'.repeat(60));
|
||||
|
||||
let lastSeq = 0;
|
||||
for (const log of response.logs) {
|
||||
const timestamp = new Date(log.timestamp).toLocaleTimeString();
|
||||
const prefix = log.type === 'stdout' ? '[OUT]' : log.type === 'stderr' ? '[ERR]' : '[SYS]';
|
||||
console.log(`${timestamp} ${prefix} ${log.message}`);
|
||||
if (log.seq !== undefined) lastSeq = Math.max(lastSeq, log.seq);
|
||||
}
|
||||
|
||||
await withStreamingLifecycle(
|
||||
async () => {
|
||||
await tspmIpcClient.subscribe(id, (log: any) => {
|
||||
if (log.seq !== undefined && log.seq <= lastSeq) return;
|
||||
if (log.seq !== undefined && log.seq > lastSeq + 1) {
|
||||
console.log(`[WARNING] Log gap detected: expected seq ${lastSeq + 1}, got ${log.seq}`);
|
||||
}
|
||||
const timestamp = new Date(log.timestamp).toLocaleTimeString();
|
||||
const prefix = log.type === 'stdout' ? '[OUT]' : log.type === 'stderr' ? '[ERR]' : '[SYS]';
|
||||
console.log(`${timestamp} ${prefix} ${log.message}`);
|
||||
if (log.seq !== undefined) lastSeq = log.seq;
|
||||
});
|
||||
},
|
||||
async () => {
|
||||
console.log('\n\nStopping log stream...');
|
||||
try { await tspmIpcClient.unsubscribe(id); } catch {}
|
||||
try { await tspmIpcClient.disconnect(); } catch {}
|
||||
}
|
||||
);
|
||||
}, {
|
||||
actionLabel: 'get logs',
|
||||
keepAlive: (argv) => getBool(argv, 'follow', 'f')
|
||||
});
|
||||
}
|
||||
await withStreamingLifecycle(
|
||||
async () => {
|
||||
await tspmIpcClient.subscribe(id, (log: any) => {
|
||||
if (log.seq !== undefined && log.seq <= lastSeq) return;
|
||||
if (log.seq !== undefined && log.seq > lastSeq + 1) {
|
||||
console.log(
|
||||
`[WARNING] Log gap detected: expected seq ${lastSeq + 1}, got ${log.seq}`,
|
||||
);
|
||||
}
|
||||
const timestamp = new Date(log.timestamp).toLocaleTimeString();
|
||||
const prefix =
|
||||
log.type === 'stdout'
|
||||
? '[OUT]'
|
||||
: log.type === 'stderr'
|
||||
? '[ERR]'
|
||||
: '[SYS]';
|
||||
console.log(`${timestamp} ${prefix} ${log.message}`);
|
||||
if (log.seq !== undefined) lastSeq = log.seq;
|
||||
});
|
||||
},
|
||||
async () => {
|
||||
console.log('\n\nStopping log stream...');
|
||||
try {
|
||||
await tspmIpcClient.unsubscribe(id);
|
||||
} catch {}
|
||||
try {
|
||||
await tspmIpcClient.disconnect();
|
||||
} catch {}
|
||||
},
|
||||
);
|
||||
},
|
||||
{
|
||||
actionLabel: 'get logs',
|
||||
keepAlive: (argv) => getBool(argv, 'follow', 'f'),
|
||||
},
|
||||
);
|
||||
}
|
||||
|
@@ -4,20 +4,25 @@ import type { CliArguments } from '../../types.js';
|
||||
import { registerIpcCommand } from '../../registration/index.js';
|
||||
|
||||
export function registerRestartCommand(smartcli: plugins.smartcli.Smartcli) {
|
||||
registerIpcCommand(smartcli, 'restart', async (argvArg: CliArguments) => {
|
||||
const id = argvArg._[1];
|
||||
if (!id) {
|
||||
console.error('Error: Please provide a process ID');
|
||||
console.log('Usage: tspm restart <id>');
|
||||
return;
|
||||
}
|
||||
registerIpcCommand(
|
||||
smartcli,
|
||||
'restart',
|
||||
async (argvArg: CliArguments) => {
|
||||
const id = argvArg._[1];
|
||||
if (!id) {
|
||||
console.error('Error: Please provide a process ID');
|
||||
console.log('Usage: tspm restart <id>');
|
||||
return;
|
||||
}
|
||||
|
||||
console.log(`Restarting process: ${id}`);
|
||||
const response = await tspmIpcClient.request('restart', { id });
|
||||
console.log(`Restarting process: ${id}`);
|
||||
const response = await tspmIpcClient.request('restart', { id });
|
||||
|
||||
console.log(`✓ Process restarted successfully`);
|
||||
console.log(` ID: ${response.processId}`);
|
||||
console.log(` PID: ${response.pid || 'N/A'}`);
|
||||
console.log(` Status: ${response.status}`);
|
||||
}, { actionLabel: 'restart process' });
|
||||
}
|
||||
console.log(`✓ Process restarted successfully`);
|
||||
console.log(` ID: ${response.processId}`);
|
||||
console.log(` PID: ${response.pid || 'N/A'}`);
|
||||
console.log(` Status: ${response.status}`);
|
||||
},
|
||||
{ actionLabel: 'restart process' },
|
||||
);
|
||||
}
|
||||
|
@@ -6,78 +6,97 @@ import { parseMemoryString, formatMemory } from '../../helpers/memory.js';
|
||||
import { registerIpcCommand } from '../../registration/index.js';
|
||||
|
||||
export function registerStartCommand(smartcli: plugins.smartcli.Smartcli) {
|
||||
registerIpcCommand(smartcli, 'start', async (argvArg: CliArguments) => {
|
||||
const script = argvArg._[1];
|
||||
if (!script) {
|
||||
console.error('Error: Please provide a script to run');
|
||||
console.log('Usage: tspm start <script> [options]');
|
||||
console.log('\nOptions:');
|
||||
console.log(' --name <name> Name for the process');
|
||||
console.log(' --memory <size> Memory limit (e.g., "512MB", "2GB")');
|
||||
console.log(' --cwd <path> Working directory');
|
||||
console.log(' --watch Watch for file changes and restart');
|
||||
console.log(' --watch-paths <paths> Comma-separated paths to watch');
|
||||
console.log(' --autorestart Auto-restart on crash');
|
||||
return;
|
||||
}
|
||||
|
||||
const memoryLimit = argvArg.memory ? parseMemoryString(argvArg.memory) : 512 * 1024 * 1024;
|
||||
const projectDir = argvArg.cwd || process.cwd();
|
||||
|
||||
// Direct .ts support via tsx (bundled with TSPM)
|
||||
let actualCommand = script;
|
||||
let commandArgs: string[] | undefined = undefined;
|
||||
|
||||
if (script.endsWith('.ts')) {
|
||||
try {
|
||||
const tsxPath = await (async () => {
|
||||
const { createRequire } = await import('module');
|
||||
const require = createRequire(import.meta.url);
|
||||
return require.resolve('tsx/dist/cli.mjs');
|
||||
})();
|
||||
|
||||
const scriptPath = plugins.path.isAbsolute(script) ? script : plugins.path.join(projectDir, script);
|
||||
actualCommand = tsxPath;
|
||||
commandArgs = [scriptPath];
|
||||
} catch {
|
||||
actualCommand = 'tsx';
|
||||
commandArgs = [script];
|
||||
registerIpcCommand(
|
||||
smartcli,
|
||||
'start',
|
||||
async (argvArg: CliArguments) => {
|
||||
const script = argvArg._[1];
|
||||
if (!script) {
|
||||
console.error('Error: Please provide a script to run');
|
||||
console.log('Usage: tspm start <script> [options]');
|
||||
console.log('\nOptions:');
|
||||
console.log(' --name <name> Name for the process');
|
||||
console.log(
|
||||
' --memory <size> Memory limit (e.g., "512MB", "2GB")',
|
||||
);
|
||||
console.log(' --cwd <path> Working directory');
|
||||
console.log(
|
||||
' --watch Watch for file changes and restart',
|
||||
);
|
||||
console.log(' --watch-paths <paths> Comma-separated paths to watch');
|
||||
console.log(' --autorestart Auto-restart on crash');
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
const name = argvArg.name || script;
|
||||
const watch = argvArg.watch || false;
|
||||
const autorestart = argvArg.autorestart !== false; // default true
|
||||
const watchPaths = argvArg.watchPaths
|
||||
? (typeof argvArg.watchPaths === 'string' ? (argvArg.watchPaths as string).split(',') : argvArg.watchPaths)
|
||||
: undefined;
|
||||
const memoryLimit = argvArg.memory
|
||||
? parseMemoryString(argvArg.memory)
|
||||
: 512 * 1024 * 1024;
|
||||
const projectDir = argvArg.cwd || process.cwd();
|
||||
|
||||
const processConfig: IProcessConfig = {
|
||||
id: name.replace(/[^a-zA-Z0-9-_]/g, '_'),
|
||||
name,
|
||||
command: actualCommand,
|
||||
args: commandArgs,
|
||||
projectDir,
|
||||
memoryLimitBytes: memoryLimit,
|
||||
autorestart,
|
||||
watch,
|
||||
watchPaths,
|
||||
};
|
||||
// Direct .ts support via tsx (bundled with TSPM)
|
||||
let actualCommand = script;
|
||||
let commandArgs: string[] | undefined = undefined;
|
||||
|
||||
console.log(`Starting process: ${name}`);
|
||||
console.log(` Command: ${script}${script.endsWith('.ts') ? ' (via tsx)' : ''}`);
|
||||
console.log(` Directory: ${projectDir}`);
|
||||
console.log(` Memory limit: ${formatMemory(memoryLimit)}`);
|
||||
console.log(` Auto-restart: ${autorestart}`);
|
||||
if (watch) {
|
||||
console.log(` Watch mode: enabled`);
|
||||
if (watchPaths) console.log(` Watch paths: ${watchPaths.join(', ')}`);
|
||||
}
|
||||
if (script.endsWith('.ts')) {
|
||||
try {
|
||||
const tsxPath = await (async () => {
|
||||
const { createRequire } = await import('module');
|
||||
const require = createRequire(import.meta.url);
|
||||
return require.resolve('tsx/dist/cli.mjs');
|
||||
})();
|
||||
|
||||
const response = await tspmIpcClient.request('start', { config: processConfig });
|
||||
console.log(`✓ Process started successfully`);
|
||||
console.log(` ID: ${response.processId}`);
|
||||
console.log(` PID: ${response.pid || 'N/A'}`);
|
||||
console.log(` Status: ${response.status}`);
|
||||
}, { actionLabel: 'start process' });
|
||||
}
|
||||
const scriptPath = plugins.path.isAbsolute(script)
|
||||
? script
|
||||
: plugins.path.join(projectDir, script);
|
||||
actualCommand = tsxPath;
|
||||
commandArgs = [scriptPath];
|
||||
} catch {
|
||||
actualCommand = 'tsx';
|
||||
commandArgs = [script];
|
||||
}
|
||||
}
|
||||
|
||||
const name = argvArg.name || script;
|
||||
const watch = argvArg.watch || false;
|
||||
const autorestart = argvArg.autorestart !== false; // default true
|
||||
const watchPaths = argvArg.watchPaths
|
||||
? typeof argvArg.watchPaths === 'string'
|
||||
? (argvArg.watchPaths as string).split(',')
|
||||
: argvArg.watchPaths
|
||||
: undefined;
|
||||
|
||||
const processConfig: IProcessConfig = {
|
||||
id: name.replace(/[^a-zA-Z0-9-_]/g, '_'),
|
||||
name,
|
||||
command: actualCommand,
|
||||
args: commandArgs,
|
||||
projectDir,
|
||||
memoryLimitBytes: memoryLimit,
|
||||
autorestart,
|
||||
watch,
|
||||
watchPaths,
|
||||
};
|
||||
|
||||
console.log(`Starting process: ${name}`);
|
||||
console.log(
|
||||
` Command: ${script}${script.endsWith('.ts') ? ' (via tsx)' : ''}`,
|
||||
);
|
||||
console.log(` Directory: ${projectDir}`);
|
||||
console.log(` Memory limit: ${formatMemory(memoryLimit)}`);
|
||||
console.log(` Auto-restart: ${autorestart}`);
|
||||
if (watch) {
|
||||
console.log(` Watch mode: enabled`);
|
||||
if (watchPaths) console.log(` Watch paths: ${watchPaths.join(', ')}`);
|
||||
}
|
||||
|
||||
const response = await tspmIpcClient.request('start', {
|
||||
config: processConfig,
|
||||
});
|
||||
console.log(`✓ Process started successfully`);
|
||||
console.log(` ID: ${response.processId}`);
|
||||
console.log(` PID: ${response.pid || 'N/A'}`);
|
||||
console.log(` Status: ${response.status}`);
|
||||
},
|
||||
{ actionLabel: 'start process' },
|
||||
);
|
||||
}
|
||||
|
@@ -4,21 +4,26 @@ import type { CliArguments } from '../../types.js';
|
||||
import { registerIpcCommand } from '../../registration/index.js';
|
||||
|
||||
export function registerStopCommand(smartcli: plugins.smartcli.Smartcli) {
|
||||
registerIpcCommand(smartcli, 'stop', async (argvArg: CliArguments) => {
|
||||
const id = argvArg._[1];
|
||||
if (!id) {
|
||||
console.error('Error: Please provide a process ID');
|
||||
console.log('Usage: tspm stop <id>');
|
||||
return;
|
||||
}
|
||||
registerIpcCommand(
|
||||
smartcli,
|
||||
'stop',
|
||||
async (argvArg: CliArguments) => {
|
||||
const id = argvArg._[1];
|
||||
if (!id) {
|
||||
console.error('Error: Please provide a process ID');
|
||||
console.log('Usage: tspm stop <id>');
|
||||
return;
|
||||
}
|
||||
|
||||
console.log(`Stopping process: ${id}`);
|
||||
const response = await tspmIpcClient.request('stop', { id });
|
||||
console.log(`Stopping process: ${id}`);
|
||||
const response = await tspmIpcClient.request('stop', { id });
|
||||
|
||||
if (response.success) {
|
||||
console.log(`✓ ${response.message}`);
|
||||
} else {
|
||||
console.error(`✗ Failed to stop process: ${response.message}`);
|
||||
}
|
||||
}, { actionLabel: 'stop process' });
|
||||
}
|
||||
if (response.success) {
|
||||
console.log(`✓ ${response.message}`);
|
||||
} else {
|
||||
console.error(`✗ Failed to stop process: ${response.message}`);
|
||||
}
|
||||
},
|
||||
{ actionLabel: 'stop process' },
|
||||
);
|
||||
}
|
||||
|
@@ -5,21 +5,24 @@ import type { CliArguments } from '../../types.js';
|
||||
|
||||
export function registerDisableCommand(smartcli: plugins.smartcli.Smartcli) {
|
||||
const cliLogger = new Logger('CLI');
|
||||
|
||||
|
||||
smartcli.addCommand('disable').subscribe({
|
||||
next: async (argvArg: CliArguments) => {
|
||||
try {
|
||||
const serviceManager = new TspmServiceManager();
|
||||
console.log('Disabling TSPM daemon service...');
|
||||
|
||||
|
||||
await serviceManager.disableService();
|
||||
|
||||
|
||||
console.log('✓ TSPM daemon service disabled');
|
||||
console.log(' The daemon will no longer start on system boot');
|
||||
console.log(' Use "tspm enable" to re-enable the service');
|
||||
} catch (error) {
|
||||
console.error('Error disabling service:', error.message);
|
||||
if (error.message.includes('permission') || error.message.includes('denied')) {
|
||||
if (
|
||||
error.message.includes('permission') ||
|
||||
error.message.includes('denied')
|
||||
) {
|
||||
console.log('\nNote: You may need to run this command with sudo');
|
||||
}
|
||||
process.exit(1);
|
||||
@@ -30,4 +33,4 @@ export function registerDisableCommand(smartcli: plugins.smartcli.Smartcli) {
|
||||
},
|
||||
complete: () => {},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@@ -5,21 +5,24 @@ import type { CliArguments } from '../../types.js';
|
||||
|
||||
export function registerEnableCommand(smartcli: plugins.smartcli.Smartcli) {
|
||||
const cliLogger = new Logger('CLI');
|
||||
|
||||
|
||||
smartcli.addCommand('enable').subscribe({
|
||||
next: async (argvArg: CliArguments) => {
|
||||
try {
|
||||
const serviceManager = new TspmServiceManager();
|
||||
console.log('Enabling TSPM daemon as system service...');
|
||||
|
||||
|
||||
await serviceManager.enableService();
|
||||
|
||||
|
||||
console.log('✓ TSPM daemon enabled and started as system service');
|
||||
console.log(' The daemon will now start automatically on system boot');
|
||||
console.log(' Use "tspm disable" to remove the service');
|
||||
} catch (error) {
|
||||
console.error('Error enabling service:', error.message);
|
||||
if (error.message.includes('permission') || error.message.includes('denied')) {
|
||||
if (
|
||||
error.message.includes('permission') ||
|
||||
error.message.includes('denied')
|
||||
) {
|
||||
console.log('\nNote: You may need to run this command with sudo');
|
||||
}
|
||||
process.exit(1);
|
||||
@@ -30,4 +33,4 @@ export function registerEnableCommand(smartcli: plugins.smartcli.Smartcli) {
|
||||
},
|
||||
complete: () => {},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@@ -2,15 +2,23 @@ import type { CliArguments } from '../types.js';
|
||||
|
||||
// Argument parsing helpers
|
||||
export const getBool = (argv: CliArguments, ...keys: string[]) =>
|
||||
keys.some(k => Boolean((argv as any)[k]));
|
||||
keys.some((k) => Boolean((argv as any)[k]));
|
||||
|
||||
export const getNumber = (argv: CliArguments, key: string, fallback: number) => {
|
||||
export const getNumber = (
|
||||
argv: CliArguments,
|
||||
key: string,
|
||||
fallback: number,
|
||||
) => {
|
||||
const v = (argv as any)[key];
|
||||
const n = typeof v === 'string' ? Number(v) : v;
|
||||
return Number.isFinite(n) ? n : fallback;
|
||||
};
|
||||
|
||||
export const getString = (argv: CliArguments, key: string, fallback?: string) => {
|
||||
export const getString = (
|
||||
argv: CliArguments,
|
||||
key: string,
|
||||
fallback?: string,
|
||||
) => {
|
||||
const v = (argv as any)[key];
|
||||
return typeof v === 'string' ? v : fallback;
|
||||
};
|
||||
};
|
||||
|
@@ -1,14 +1,18 @@
|
||||
// Helper function to handle daemon connection errors
|
||||
export function handleDaemonError(error: any, action: string): void {
|
||||
if (error.message?.includes('daemon is not running') ||
|
||||
error.message?.includes('Not connected') ||
|
||||
error.message?.includes('ECONNREFUSED')) {
|
||||
if (
|
||||
error.message?.includes('daemon is not running') ||
|
||||
error.message?.includes('Not connected') ||
|
||||
error.message?.includes('ECONNREFUSED')
|
||||
) {
|
||||
console.error(`Error: Cannot ${action} - TSPM daemon is not running.`);
|
||||
console.log('\nTo start the daemon, run one of:');
|
||||
console.log(' tspm daemon start - Start for this session only');
|
||||
console.log(' tspm enable - Enable as system service (recommended)');
|
||||
console.log(
|
||||
' tspm enable - Enable as system service (recommended)',
|
||||
);
|
||||
} else {
|
||||
console.error(`Error ${action}:`, error.message);
|
||||
}
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
@@ -7,11 +7,12 @@ export function pad(str: string, length: number): string {
|
||||
|
||||
// Helper for unknown errors
|
||||
export const unknownError = (err: any) =>
|
||||
(err?.message && typeof err.message === 'string') ? err.message : String(err);
|
||||
err?.message && typeof err.message === 'string' ? err.message : String(err);
|
||||
|
||||
// Helper function to format log entries
|
||||
export function formatLog(log: any): string {
|
||||
const timestamp = new Date(log.timestamp).toLocaleTimeString();
|
||||
const prefix = log.type === 'stdout' ? '[OUT]' : log.type === 'stderr' ? '[ERR]' : '[SYS]';
|
||||
const prefix =
|
||||
log.type === 'stdout' ? '[OUT]' : log.type === 'stderr' ? '[ERR]' : '[SYS]';
|
||||
return `${timestamp} ${prefix} ${log.message}`;
|
||||
}
|
||||
}
|
||||
|
@@ -19,4 +19,4 @@ export function withStreamingLifecycle(
|
||||
await setup();
|
||||
await new Promise(() => {}); // keep alive
|
||||
})();
|
||||
}
|
||||
}
|
||||
|
@@ -30,4 +30,4 @@ export function formatMemory(bytes: number): string {
|
||||
} else {
|
||||
return `${bytes} B`;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -65,4 +65,4 @@ export const run = async (): Promise<void> => {
|
||||
|
||||
// Start parsing commands
|
||||
smartcliInstance.startParse();
|
||||
};
|
||||
};
|
||||
|
@@ -4,16 +4,23 @@ import { tspmIpcClient } from '../../classes.ipcclient.js';
|
||||
* Preflight the daemon if required. Uses getDaemonStatus() which is safe and cheap:
|
||||
* it only connects if the PID file is valid.
|
||||
*/
|
||||
export async function ensureDaemonOrHint(requireDaemon: boolean | undefined, actionLabel?: string): Promise<boolean> {
|
||||
export async function ensureDaemonOrHint(
|
||||
requireDaemon: boolean | undefined,
|
||||
actionLabel?: string,
|
||||
): Promise<boolean> {
|
||||
if (requireDaemon === false) return true; // command does not require daemon
|
||||
const status = await tspmIpcClient.getDaemonStatus();
|
||||
if (!status) {
|
||||
// Same hint as handleDaemonError, but early and consistent
|
||||
console.error(`Error: Cannot ${actionLabel || 'perform action'} - TSPM daemon is not running.`);
|
||||
console.error(
|
||||
`Error: Cannot ${actionLabel || 'perform action'} - TSPM daemon is not running.`,
|
||||
);
|
||||
console.log('\nTo start the daemon, run one of:');
|
||||
console.log(' tspm daemon start - Start for this session only');
|
||||
console.log(' tspm enable - Enable as system service (recommended)');
|
||||
console.log(
|
||||
' tspm enable - Enable as system service (recommended)',
|
||||
);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@@ -1,5 +1,9 @@
|
||||
import * as plugins from '../../plugins.js';
|
||||
import type { CliArguments, CommandAction, IpcCommandOptions } from '../types.js';
|
||||
import type {
|
||||
CliArguments,
|
||||
CommandAction,
|
||||
IpcCommandOptions,
|
||||
} from '../types.js';
|
||||
import { handleDaemonError } from '../helpers/errors.js';
|
||||
import { unknownError } from '../helpers/formatting.js';
|
||||
import { runIpcCommand } from '../utils/ipc.js';
|
||||
@@ -15,7 +19,7 @@ export function registerIpcCommand(
|
||||
smartcli: plugins.smartcli.Smartcli,
|
||||
name: string,
|
||||
action: CommandAction,
|
||||
opts: IpcCommandOptions = {}
|
||||
opts: IpcCommandOptions = {},
|
||||
) {
|
||||
const { actionLabel = name, keepAlive = false, requireDaemon = true } = opts;
|
||||
|
||||
@@ -29,7 +33,8 @@ export function registerIpcCommand(
|
||||
}
|
||||
|
||||
// Evaluate keepAlive - can be boolean or function
|
||||
const shouldKeepAlive = typeof keepAlive === 'function' ? keepAlive(argv) : keepAlive;
|
||||
const shouldKeepAlive =
|
||||
typeof keepAlive === 'function' ? keepAlive(argv) : keepAlive;
|
||||
|
||||
if (shouldKeepAlive) {
|
||||
// Let action manage its own connection/cleanup lifecycle
|
||||
@@ -51,7 +56,10 @@ export function registerIpcCommand(
|
||||
},
|
||||
error: (err) => {
|
||||
// Fallback error path (should be rare with try/catch in next)
|
||||
console.error(`Unexpected error in command "${name}":`, unknownError(err));
|
||||
console.error(
|
||||
`Unexpected error in command "${name}":`,
|
||||
unknownError(err),
|
||||
);
|
||||
process.exit(1);
|
||||
},
|
||||
complete: () => {},
|
||||
@@ -66,7 +74,7 @@ export function registerLocalCommand(
|
||||
smartcli: plugins.smartcli.Smartcli,
|
||||
name: string,
|
||||
action: (argv: CliArguments) => Promise<void>,
|
||||
opts: { actionLabel?: string } = {}
|
||||
opts: { actionLabel?: string } = {},
|
||||
) {
|
||||
const { actionLabel = name } = opts;
|
||||
smartcli.addCommand(name).subscribe({
|
||||
@@ -79,9 +87,12 @@ export function registerLocalCommand(
|
||||
}
|
||||
},
|
||||
error: (err) => {
|
||||
console.error(`Unexpected error in command "${name}":`, unknownError(err));
|
||||
console.error(
|
||||
`Unexpected error in command "${name}":`,
|
||||
unknownError(err),
|
||||
);
|
||||
process.exit(1);
|
||||
},
|
||||
complete: () => {},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@@ -14,7 +14,7 @@ export interface CliArguments {
|
||||
export type CommandAction = (argv: CliArguments) => Promise<void>;
|
||||
|
||||
export interface IpcCommandOptions {
|
||||
actionLabel?: string; // used in error message, e.g. "start process"
|
||||
keepAlive?: boolean | ((argv: CliArguments) => boolean); // true for streaming commands (don't auto-disconnect), or function to determine at runtime
|
||||
requireDaemon?: boolean; // default true for IPC-bound commands
|
||||
}
|
||||
actionLabel?: string; // used in error message, e.g. "start process"
|
||||
keepAlive?: boolean | ((argv: CliArguments) => boolean); // true for streaming commands (don't auto-disconnect), or function to determine at runtime
|
||||
requireDaemon?: boolean; // default true for IPC-bound commands
|
||||
}
|
||||
|
@@ -11,4 +11,4 @@ export async function runIpcCommand<T>(body: () => Promise<T>): Promise<T> {
|
||||
// Ignore disconnect errors
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,7 +1,4 @@
|
||||
import type {
|
||||
IProcessConfig,
|
||||
IProcessInfo,
|
||||
} from './classes.tspm.js';
|
||||
import type { IProcessConfig, IProcessInfo } from './classes.tspm.js';
|
||||
import type { IProcessLog } from './classes.processwrapper.js';
|
||||
|
||||
// Base message types
|
||||
|
Reference in New Issue
Block a user