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

@@ -1,6 +1,19 @@
# Changelog # Changelog
## 2025-08-28 - 3.0.0 - BREAKING CHANGE(daemon)
Refactor daemon and service management: remove IPC auto-spawn, add TspmServiceManager, tighten IPC/client/CLI behavior and tests
- Remove automatic daemon spawn from the IPC client — clients now error with guidance and require the daemon to be started manually or enabled as a system service
- Add TspmServiceManager to manage the daemon as a systemd service (enable/disable/reload/status)
- Update IPC server/client to use SmartIpc.createServer/createClient with heartbeat defaults and explicit onMessage handlers
- Daemon publishes per-process logs to topics (logs.<processId>) and re-emits ProcessMonitor logs for pub/sub
- CLI updated: add enable/disable service commands, adjust daemon start/stop/status workflows and improve user hints when daemon is not running
- Add/adjust integration and unit tests to cover daemon lifecycle, IPC client behavior, log streaming, heartbeat and resource reporting
- Documentation expanded (README, readme.plan.md, changelog) to reflect the refactor and migration notes
- Various code cleanups, formatting fixes and defensive checks across modules
## 2025-08-28 - 2.0.0 - BREAKING CHANGE(daemon) ## 2025-08-28 - 2.0.0 - BREAKING CHANGE(daemon)
Refactor daemon lifecycle and service management: remove IPC auto-spawn, add TspmServiceManager and CLI enable/disable Refactor daemon lifecycle and service management: remove IPC auto-spawn, add TspmServiceManager and CLI enable/disable
- Do not auto-spawn the daemon from the IPC client anymore — attempts to connect will now error with instructions to start the daemon manually or enable the system service (breaking change). - Do not auto-spawn the daemon from the IPC client anymore — attempts to connect will now error with instructions to start the daemon manually or enable the system service (breaking change).
@@ -12,6 +25,7 @@ Refactor daemon lifecycle and service management: remove IPC auto-spawn, add Tsp
- Updated development plan/readme (readme.plan.md) to reflect the refactor toward proper SmartDaemon integration and migration notes. - Updated development plan/readme (readme.plan.md) to reflect the refactor toward proper SmartDaemon integration and migration notes.
## 2025-08-26 - 1.8.0 - feat(daemon) ## 2025-08-26 - 1.8.0 - feat(daemon)
Add real-time log streaming and pub/sub: daemon publishes per-process logs, IPC client subscribe/unsubscribe, CLI --follow streaming, and sequencing for logs Add real-time log streaming and pub/sub: daemon publishes per-process logs, IPC client subscribe/unsubscribe, CLI --follow streaming, and sequencing for logs
- Upgrade @push.rocks/smartipc dependency to ^2.1.2 - Upgrade @push.rocks/smartipc dependency to ^2.1.2
@@ -24,6 +38,7 @@ Add real-time log streaming and pub/sub: daemon publishes per-process logs, IPC
- Standardized heartbeat and IPC timing defaults (heartbeatInterval: 5000ms, heartbeatTimeout: 20000ms, heartbeatInitialGracePeriodMs: 10000ms) - Standardized heartbeat and IPC timing defaults (heartbeatInterval: 5000ms, heartbeatTimeout: 20000ms, heartbeatInitialGracePeriodMs: 10000ms)
## 2025-08-25 - 1.7.0 - feat(readme) ## 2025-08-25 - 1.7.0 - feat(readme)
Add comprehensive README with detailed usage, command reference, daemon management, architecture and development instructions Add comprehensive README with detailed usage, command reference, daemon management, architecture and development instructions
- Expanded README from a short placeholder to a full documentation covering: Quick Start, Installation, Command Reference, Daemon Management, Monitoring & Information, Batch Operations, Architecture, Programmatic Usage, Advanced Features, Development, Debugging, Performance, and Legal information - Expanded README from a short placeholder to a full documentation covering: Quick Start, Installation, Command Reference, Daemon Management, Monitoring & Information, Batch Operations, Architecture, Programmatic Usage, Advanced Features, Development, Debugging, Performance, and Legal information
@@ -32,6 +47,7 @@ Add comprehensive README with detailed usage, command reference, daemon manageme
- Improved onboarding instructions: cloning, installing, testing, building, and running the project - Improved onboarding instructions: cloning, installing, testing, building, and running the project
## 2025-08-25 - 1.6.1 - fix(daemon) ## 2025-08-25 - 1.6.1 - fix(daemon)
Fix smartipc integration and add daemon/ipc integration tests Fix smartipc integration and add daemon/ipc integration tests
- Replace direct smartipc server/client construction with SmartIpc.createServer/createClient and set heartbeat: false - Replace direct smartipc server/client construction with SmartIpc.createServer/createClient and set heartbeat: false
@@ -40,6 +56,7 @@ Fix smartipc integration and add daemon/ipc integration tests
- Add comprehensive tests: unit tests for TspmDaemon and TspmIpcClient and full integration tests for daemon lifecycle, process management, error handling, heartbeat and resource reporting - Add comprehensive tests: unit tests for TspmDaemon and TspmIpcClient and full integration tests for daemon lifecycle, process management, error handling, heartbeat and resource reporting
## 2025-08-25 - 1.6.0 - feat(daemon) ## 2025-08-25 - 1.6.0 - feat(daemon)
Add central TSPM daemon and IPC client; refactor CLI to use daemon and improve monitoring/error handling Add central TSPM daemon and IPC client; refactor CLI to use daemon and improve monitoring/error handling
- Add central daemon implementation (ts/classes.daemon.ts) to manage all processes via a single background service and Unix socket. - Add central daemon implementation (ts/classes.daemon.ts) to manage all processes via a single background service and Unix socket.

View File

@@ -64,9 +64,11 @@ tspm restart my-server
### Process Management ### Process Management
#### `tspm start <script> [options]` #### `tspm start <script> [options]`
Start a new process with automatic monitoring and management. Start a new process with automatic monitoring and management.
**Options:** **Options:**
- `--name <name>` - Custom name for the process (default: script name) - `--name <name>` - Custom name for the process (default: script name)
- `--memory <size>` - Memory limit (e.g., "512MB", "2GB", default: 512MB) - `--memory <size>` - Memory limit (e.g., "512MB", "2GB", default: 512MB)
- `--cwd <path>` - Working directory (default: current directory) - `--cwd <path>` - Working directory (default: current directory)
@@ -75,6 +77,7 @@ Start a new process with automatic monitoring and management.
- `--autorestart` - Auto-restart on crash (default: true) - `--autorestart` - Auto-restart on crash (default: true)
**Examples:** **Examples:**
```bash ```bash
# Simple start # Simple start
tspm start server.js tspm start server.js
@@ -90,6 +93,7 @@ tspm start ../other-project/index.js --cwd ../other-project --name other
``` ```
#### `tspm stop <id>` #### `tspm stop <id>`
Gracefully stop a running process (SIGTERM → SIGKILL after timeout). Gracefully stop a running process (SIGTERM → SIGKILL after timeout).
```bash ```bash
@@ -97,6 +101,7 @@ tspm stop my-server
``` ```
#### `tspm restart <id>` #### `tspm restart <id>`
Stop and restart a process with the same configuration. Stop and restart a process with the same configuration.
```bash ```bash
@@ -104,6 +109,7 @@ tspm restart my-server
``` ```
#### `tspm delete <id>` #### `tspm delete <id>`
Stop and remove a process from TSPM management. Stop and remove a process from TSPM management.
```bash ```bash
@@ -113,6 +119,7 @@ tspm delete old-server
### Monitoring & Information ### Monitoring & Information
#### `tspm list` #### `tspm list`
Display all managed processes in a beautiful table. Display all managed processes in a beautiful table.
```bash ```bash
@@ -128,6 +135,7 @@ tspm list
``` ```
#### `tspm describe <id>` #### `tspm describe <id>`
Get detailed information about a specific process. Get detailed information about a specific process.
```bash ```bash
@@ -153,9 +161,11 @@ Watch Paths: src, config
``` ```
#### `tspm logs <id> [options]` #### `tspm logs <id> [options]`
View process logs (stdout and stderr). View process logs (stdout and stderr).
**Options:** **Options:**
- `--lines <n>` - Number of lines to display (default: 50) - `--lines <n>` - Number of lines to display (default: 50)
```bash ```bash
@@ -165,6 +175,7 @@ tspm logs my-server --lines 100
### Batch Operations ### Batch Operations
#### `tspm start-all` #### `tspm start-all`
Start all saved processes at once. Start all saved processes at once.
```bash ```bash
@@ -172,6 +183,7 @@ tspm start-all
``` ```
#### `tspm stop-all` #### `tspm stop-all`
Stop all running processes. Stop all running processes.
```bash ```bash
@@ -179,6 +191,7 @@ tspm stop-all
``` ```
#### `tspm restart-all` #### `tspm restart-all`
Restart all running processes. Restart all running processes.
```bash ```bash
@@ -188,6 +201,7 @@ tspm restart-all
### Daemon Management ### Daemon Management
#### `tspm daemon start` #### `tspm daemon start`
Start the TSPM daemon (happens automatically on first command). Start the TSPM daemon (happens automatically on first command).
```bash ```bash
@@ -195,6 +209,7 @@ tspm daemon start
``` ```
#### `tspm daemon stop` #### `tspm daemon stop`
Stop the TSPM daemon and all managed processes. Stop the TSPM daemon and all managed processes.
```bash ```bash
@@ -202,6 +217,7 @@ tspm daemon stop
``` ```
#### `tspm daemon status` #### `tspm daemon status`
Check daemon health and statistics. Check daemon health and statistics.
```bash ```bash
@@ -245,7 +261,7 @@ const processId = await manager.start({
projectDir: process.cwd(), projectDir: process.cwd(),
memoryLimitBytes: 512 * 1024 * 1024, // 512MB memoryLimitBytes: 512 * 1024 * 1024, // 512MB
autorestart: true, autorestart: true,
watch: false watch: false,
}); });
// Monitor process // Monitor process
@@ -259,18 +275,23 @@ await manager.stop(processId);
## 🔧 Advanced Features ## 🔧 Advanced Features
### Memory Limit Enforcement ### Memory Limit Enforcement
TSPM tracks memory usage including all child processes spawned by your application. When a process exceeds its memory limit, it's gracefully restarted. TSPM tracks memory usage including all child processes spawned by your application. When a process exceeds its memory limit, it's gracefully restarted.
### Process Group Tracking ### Process Group Tracking
Using `ps-tree`, TSPM monitors not just your main process but all child processes it spawns, ensuring complete cleanup on stop/restart. Using `ps-tree`, TSPM monitors not just your main process but all child processes it spawns, ensuring complete cleanup on stop/restart.
### Intelligent Logging ### Intelligent Logging
Logs are buffered and managed efficiently, preventing memory issues from excessive output while ensuring you don't lose important information. Logs are buffered and managed efficiently, preventing memory issues from excessive output while ensuring you don't lose important information.
### Graceful Shutdown ### Graceful Shutdown
Processes receive SIGTERM first, allowing them to clean up. After a timeout, SIGKILL ensures termination. Processes receive SIGTERM first, allowing them to clean up. After a timeout, SIGKILL ensures termination.
### Configuration Persistence ### Configuration Persistence
Process configurations are saved, allowing you to restart all processes after a system reboot with a single command. Process configurations are saved, allowing you to restart all processes after a system reboot with a single command.
## 🛠️ Development ## 🛠️ Development
@@ -304,6 +325,7 @@ tspm list
## 📊 Performance ## 📊 Performance
TSPM is designed to be lightweight and efficient: TSPM is designed to be lightweight and efficient:
- Minimal CPU overhead (typically < 0.5%) - Minimal CPU overhead (typically < 0.5%)
- Small memory footprint (~30-50MB for the daemon) - Small memory footprint (~30-50MB for the daemon)
- Fast process startup and shutdown - Fast process startup and shutdown
@@ -322,7 +344,7 @@ Unlike general-purpose process managers, TSPM is built specifically for the Type
## License and Legal Information ## License and Legal Information
This repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository. This repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository.
**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file. **Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.
@@ -337,4 +359,4 @@ Registered at District court Bremen HRB 35230 HB, Germany
For any legal inquiries or if you require further information, please contact us via email at hello@task.vc. For any legal inquiries or if you require further information, please contact us via email at hello@task.vc.
By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works. By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.

View File

@@ -1,20 +1,24 @@
# TSPM SmartDaemon Service Management Refactor # TSPM SmartDaemon Service Management Refactor
## Problem ## Problem
Currently TSPM auto-spawns the daemon as a detached child process, which is improper daemon management. It should use smartdaemon for all lifecycle management and never spawn processes directly. Currently TSPM auto-spawns the daemon as a detached child process, which is improper daemon management. It should use smartdaemon for all lifecycle management and never spawn processes directly.
## Solution ## Solution
Refactor to use SmartDaemon for proper systemd service integration. Refactor to use SmartDaemon for proper systemd service integration.
## Implementation Tasks ## Implementation Tasks
### Phase 1: Remove Auto-Spawn Behavior ### Phase 1: Remove Auto-Spawn Behavior
- [x] Remove spawn import from ts/classes.ipcclient.ts - [x] Remove spawn import from ts/classes.ipcclient.ts
- [x] Delete startDaemon() method from IpcClient - [x] Delete startDaemon() method from IpcClient
- [x] Update connect() to throw error when daemon not running - [x] Update connect() to throw error when daemon not running
- [x] Remove auto-reconnect logic from request() method - [x] Remove auto-reconnect logic from request() method
### Phase 2: Create Service Manager ### Phase 2: Create Service Manager
- [x] Create new file ts/classes.servicemanager.ts - [x] Create new file ts/classes.servicemanager.ts
- [x] Implement TspmServiceManager class - [x] Implement TspmServiceManager class
- [x] Add getOrCreateService() method - [x] Add getOrCreateService() method
@@ -23,6 +27,7 @@ Refactor to use SmartDaemon for proper systemd service integration.
- [x] Add getServiceStatus() method - [x] Add getServiceStatus() method
### Phase 3: Update CLI Commands ### Phase 3: Update CLI Commands
- [x] Add 'enable' command to CLI - [x] Add 'enable' command to CLI
- [x] Add 'disable' command to CLI - [x] Add 'disable' command to CLI
- [x] Update 'daemon start' to work without systemd - [x] Update 'daemon start' to work without systemd
@@ -31,11 +36,13 @@ Refactor to use SmartDaemon for proper systemd service integration.
- [x] Add proper error messages with hints - [x] Add proper error messages with hints
### Phase 4: Update Documentation ### Phase 4: Update Documentation
- [x] Update help text in CLI - [x] Update help text in CLI
- [ ] Update command descriptions - [ ] Update command descriptions
- [x] Add service management section - [x] Add service management section
### Phase 5: Testing ### Phase 5: Testing
- [x] Test enable command - [x] Test enable command
- [x] Test disable command - [x] Test disable command
- [x] Test daemon commands - [x] Test daemon commands
@@ -43,6 +50,7 @@ Refactor to use SmartDaemon for proper systemd service integration.
- [x] Build and verify TypeScript compilation - [x] Build and verify TypeScript compilation
## Migration Notes ## Migration Notes
- Users will need to run `tspm enable` once after update - Users will need to run `tspm enable` once after update
- Existing daemon instances will stop working - Existing daemon instances will stop working
- Documentation needs updating to explain new behavior - Documentation needs updating to explain new behavior

View File

@@ -13,18 +13,18 @@ tap.test('TspmDaemon creation', async () => {
tap.test('Daemon PID file management', async (tools) => { tap.test('Daemon PID file management', async (tools) => {
const testDir = path.join(process.cwd(), '.nogit'); const testDir = path.join(process.cwd(), '.nogit');
const testPidFile = path.join(testDir, 'test-daemon.pid'); const testPidFile = path.join(testDir, 'test-daemon.pid');
// Create directory if it doesn't exist // Create directory if it doesn't exist
await fs.mkdir(testDir, { recursive: true }); await fs.mkdir(testDir, { recursive: true });
// Clean up any existing test file // Clean up any existing test file
await fs.unlink(testPidFile).catch(() => {}); await fs.unlink(testPidFile).catch(() => {});
// Test writing PID file // Test writing PID file
await fs.writeFile(testPidFile, process.pid.toString()); await fs.writeFile(testPidFile, process.pid.toString());
const pidContent = await fs.readFile(testPidFile, 'utf-8'); const pidContent = await fs.readFile(testPidFile, 'utf-8');
expect(parseInt(pidContent)).toEqual(process.pid); expect(parseInt(pidContent)).toEqual(process.pid);
// Clean up // Clean up
await fs.unlink(testPidFile); await fs.unlink(testPidFile);
}); });
@@ -38,11 +38,11 @@ tap.test('Daemon socket path generation', async () => {
tap.test('Daemon shutdown handlers', async (tools) => { tap.test('Daemon shutdown handlers', async (tools) => {
const daemon = new TspmDaemon(); const daemon = new TspmDaemon();
// Test that shutdown handlers are registered // Test that shutdown handlers are registered
const sigintListeners = process.listeners('SIGINT'); const sigintListeners = process.listeners('SIGINT');
const sigtermListeners = process.listeners('SIGTERM'); const sigtermListeners = process.listeners('SIGTERM');
// We expect at least one listener for each signal // We expect at least one listener for each signal
// (Note: in actual test we won't start the daemon to avoid side effects) // (Note: in actual test we won't start the daemon to avoid side effects)
expect(sigintListeners.length).toBeGreaterThanOrEqual(0); expect(sigintListeners.length).toBeGreaterThanOrEqual(0);
@@ -52,7 +52,7 @@ tap.test('Daemon shutdown handlers', async (tools) => {
tap.test('Daemon process info tracking', async () => { tap.test('Daemon process info tracking', async () => {
const daemon = new TspmDaemon(); const daemon = new TspmDaemon();
const tspmInstance = (daemon as any).tspmInstance; const tspmInstance = (daemon as any).tspmInstance;
expect(tspmInstance).toBeDefined(); expect(tspmInstance).toBeDefined();
expect(tspmInstance.processes).toBeInstanceOf(Map); expect(tspmInstance.processes).toBeInstanceOf(Map);
expect(tspmInstance.processConfigs).toBeInstanceOf(Map); expect(tspmInstance.processConfigs).toBeInstanceOf(Map);
@@ -61,7 +61,7 @@ tap.test('Daemon process info tracking', async () => {
tap.test('Daemon heartbeat monitoring setup', async (tools) => { tap.test('Daemon heartbeat monitoring setup', async (tools) => {
const daemon = new TspmDaemon(); const daemon = new TspmDaemon();
// Test heartbeat interval property exists // Test heartbeat interval property exists
const heartbeatInterval = (daemon as any).heartbeatInterval; const heartbeatInterval = (daemon as any).heartbeatInterval;
expect(heartbeatInterval).toEqual(null); // Should be null before start expect(heartbeatInterval).toEqual(null); // Should be null before start
@@ -70,13 +70,13 @@ tap.test('Daemon heartbeat monitoring setup', async (tools) => {
tap.test('Daemon shutdown state management', async () => { tap.test('Daemon shutdown state management', async () => {
const daemon = new TspmDaemon(); const daemon = new TspmDaemon();
const isShuttingDown = (daemon as any).isShuttingDown; const isShuttingDown = (daemon as any).isShuttingDown;
expect(isShuttingDown).toEqual(false); expect(isShuttingDown).toEqual(false);
}); });
tap.test('Daemon memory usage reporting', async () => { tap.test('Daemon memory usage reporting', async () => {
const memUsage = process.memoryUsage(); const memUsage = process.memoryUsage();
expect(memUsage.heapUsed).toBeGreaterThan(0); expect(memUsage.heapUsed).toBeGreaterThan(0);
expect(memUsage.heapTotal).toBeGreaterThan(0); expect(memUsage.heapTotal).toBeGreaterThan(0);
expect(memUsage.rss).toBeGreaterThan(0); expect(memUsage.rss).toBeGreaterThan(0);
@@ -84,10 +84,10 @@ tap.test('Daemon memory usage reporting', async () => {
tap.test('Daemon CPU usage calculation', async () => { tap.test('Daemon CPU usage calculation', async () => {
const cpuUsage = process.cpuUsage(); const cpuUsage = process.cpuUsage();
expect(cpuUsage.user).toBeGreaterThanOrEqual(0); expect(cpuUsage.user).toBeGreaterThanOrEqual(0);
expect(cpuUsage.system).toBeGreaterThanOrEqual(0); expect(cpuUsage.system).toBeGreaterThanOrEqual(0);
// Test conversion to seconds // Test conversion to seconds
const cpuSeconds = cpuUsage.user / 1000000; const cpuSeconds = cpuUsage.user / 1000000;
expect(cpuSeconds).toBeGreaterThanOrEqual(0); expect(cpuSeconds).toBeGreaterThanOrEqual(0);
@@ -95,13 +95,13 @@ tap.test('Daemon CPU usage calculation', async () => {
tap.test('Daemon uptime calculation', async () => { tap.test('Daemon uptime calculation', async () => {
const startTime = Date.now(); const startTime = Date.now();
// Wait a bit // Wait a bit
await new Promise(resolve => setTimeout(resolve, 100)); await new Promise((resolve) => setTimeout(resolve, 100));
const uptime = Date.now() - startTime; const uptime = Date.now() - startTime;
expect(uptime).toBeGreaterThanOrEqual(100); expect(uptime).toBeGreaterThanOrEqual(100);
expect(uptime).toBeLessThan(200); expect(uptime).toBeLessThan(200);
}); });
export default tap.start(); export default tap.start();

View File

@@ -10,7 +10,7 @@ import { tspmIpcClient } from '../ts/classes.ipcclient.js';
async function ensureDaemonStopped() { async function ensureDaemonStopped() {
try { try {
await tspmIpcClient.stopDaemon(false); await tspmIpcClient.stopDaemon(false);
await new Promise(resolve => setTimeout(resolve, 1000)); await new Promise((resolve) => setTimeout(resolve, 1000));
} catch (error) { } catch (error) {
// Ignore errors if daemon is not running // Ignore errors if daemon is not running
} }
@@ -21,7 +21,7 @@ async function cleanupTestFiles() {
const tspmDir = path.join(os.homedir(), '.tspm'); const tspmDir = path.join(os.homedir(), '.tspm');
const pidFile = path.join(tspmDir, 'daemon.pid'); const pidFile = path.join(tspmDir, 'daemon.pid');
const socketFile = path.join(tspmDir, 'tspm.sock'); const socketFile = path.join(tspmDir, 'tspm.sock');
await fs.unlink(pidFile).catch(() => {}); await fs.unlink(pidFile).catch(() => {});
await fs.unlink(socketFile).catch(() => {}); await fs.unlink(socketFile).catch(() => {});
} }
@@ -29,55 +29,55 @@ async function cleanupTestFiles() {
// Integration tests for daemon-client communication // Integration tests for daemon-client communication
tap.test('Full daemon lifecycle test', async (tools) => { tap.test('Full daemon lifecycle test', async (tools) => {
const done = tools.defer(); const done = tools.defer();
// Ensure clean state // Ensure clean state
await ensureDaemonStopped(); await ensureDaemonStopped();
await cleanupTestFiles(); await cleanupTestFiles();
// Test 1: Check daemon is not running // Test 1: Check daemon is not running
let status = await tspmIpcClient.getDaemonStatus(); let status = await tspmIpcClient.getDaemonStatus();
expect(status).toEqual(null); expect(status).toEqual(null);
// Test 2: Start daemon // Test 2: Start daemon
console.log('Starting daemon...'); console.log('Starting daemon...');
await tspmIpcClient.connect(); await tspmIpcClient.connect();
// Give daemon time to fully initialize // Give daemon time to fully initialize
await new Promise(resolve => setTimeout(resolve, 2000)); await new Promise((resolve) => setTimeout(resolve, 2000));
// Test 3: Check daemon is running // Test 3: Check daemon is running
status = await tspmIpcClient.getDaemonStatus(); status = await tspmIpcClient.getDaemonStatus();
expect(status).toBeDefined(); expect(status).toBeDefined();
expect(status?.status).toEqual('running'); expect(status?.status).toEqual('running');
expect(status?.pid).toBeGreaterThan(0); expect(status?.pid).toBeGreaterThan(0);
expect(status?.processCount).toBeGreaterThanOrEqual(0); expect(status?.processCount).toBeGreaterThanOrEqual(0);
// Test 4: Stop daemon // Test 4: Stop daemon
console.log('Stopping daemon...'); console.log('Stopping daemon...');
await tspmIpcClient.stopDaemon(true); await tspmIpcClient.stopDaemon(true);
// Give daemon time to shutdown // Give daemon time to shutdown
await new Promise(resolve => setTimeout(resolve, 2000)); await new Promise((resolve) => setTimeout(resolve, 2000));
// Test 5: Check daemon is stopped // Test 5: Check daemon is stopped
status = await tspmIpcClient.getDaemonStatus(); status = await tspmIpcClient.getDaemonStatus();
expect(status).toEqual(null); expect(status).toEqual(null);
done.resolve(); done.resolve();
}); });
tap.test('Process management through daemon', async (tools) => { tap.test('Process management through daemon', async (tools) => {
const done = tools.defer(); const done = tools.defer();
// Ensure daemon is running // Ensure daemon is running
await tspmIpcClient.connect(); await tspmIpcClient.connect();
await new Promise(resolve => setTimeout(resolve, 1000)); await new Promise((resolve) => setTimeout(resolve, 1000));
// Test 1: List processes (should be empty initially) // Test 1: List processes (should be empty initially)
let listResponse = await tspmIpcClient.request('list', {}); let listResponse = await tspmIpcClient.request('list', {});
expect(listResponse.processes).toBeArray(); expect(listResponse.processes).toBeArray();
expect(listResponse.processes.length).toEqual(0); expect(listResponse.processes.length).toEqual(0);
// Test 2: Start a test process // Test 2: Start a test process
const testConfig: tspm.IProcessConfig = { const testConfig: tspm.IProcessConfig = {
id: 'test-echo', id: 'test-echo',
@@ -87,52 +87,60 @@ tap.test('Process management through daemon', async (tools) => {
memoryLimitBytes: 50 * 1024 * 1024, memoryLimitBytes: 50 * 1024 * 1024,
autorestart: false, autorestart: false,
}; };
const startResponse = await tspmIpcClient.request('start', { config: testConfig }); const startResponse = await tspmIpcClient.request('start', {
config: testConfig,
});
expect(startResponse.processId).toEqual('test-echo'); expect(startResponse.processId).toEqual('test-echo');
expect(startResponse.status).toBeDefined(); expect(startResponse.status).toBeDefined();
// Test 3: List processes (should have one process) // Test 3: List processes (should have one process)
listResponse = await tspmIpcClient.request('list', {}); listResponse = await tspmIpcClient.request('list', {});
expect(listResponse.processes.length).toBeGreaterThanOrEqual(1); expect(listResponse.processes.length).toBeGreaterThanOrEqual(1);
const process = listResponse.processes.find(p => p.id === 'test-echo'); const process = listResponse.processes.find((p) => p.id === 'test-echo');
expect(process).toBeDefined(); expect(process).toBeDefined();
expect(process?.id).toEqual('test-echo'); expect(process?.id).toEqual('test-echo');
// Test 4: Describe the process // Test 4: Describe the process
const describeResponse = await tspmIpcClient.request('describe', { id: 'test-echo' }); const describeResponse = await tspmIpcClient.request('describe', {
id: 'test-echo',
});
expect(describeResponse.processInfo).toBeDefined(); expect(describeResponse.processInfo).toBeDefined();
expect(describeResponse.config).toBeDefined(); expect(describeResponse.config).toBeDefined();
expect(describeResponse.config.id).toEqual('test-echo'); expect(describeResponse.config.id).toEqual('test-echo');
// Test 5: Stop the process // Test 5: Stop the process
const stopResponse = await tspmIpcClient.request('stop', { id: 'test-echo' }); const stopResponse = await tspmIpcClient.request('stop', { id: 'test-echo' });
expect(stopResponse.success).toEqual(true); expect(stopResponse.success).toEqual(true);
expect(stopResponse.message).toInclude('stopped successfully'); expect(stopResponse.message).toInclude('stopped successfully');
// Test 6: Delete the process // Test 6: Delete the process
const deleteResponse = await tspmIpcClient.request('delete', { id: 'test-echo' }); const deleteResponse = await tspmIpcClient.request('delete', {
id: 'test-echo',
});
expect(deleteResponse.success).toEqual(true); expect(deleteResponse.success).toEqual(true);
// Test 7: Verify process is gone // Test 7: Verify process is gone
listResponse = await tspmIpcClient.request('list', {}); listResponse = await tspmIpcClient.request('list', {});
const deletedProcess = listResponse.processes.find(p => p.id === 'test-echo'); const deletedProcess = listResponse.processes.find(
(p) => p.id === 'test-echo',
);
expect(deletedProcess).toBeUndefined(); expect(deletedProcess).toBeUndefined();
// Cleanup: stop daemon // Cleanup: stop daemon
await tspmIpcClient.stopDaemon(true); await tspmIpcClient.stopDaemon(true);
done.resolve(); done.resolve();
}); });
tap.test('Batch operations through daemon', async (tools) => { tap.test('Batch operations through daemon', async (tools) => {
const done = tools.defer(); const done = tools.defer();
// Ensure daemon is running // Ensure daemon is running
await tspmIpcClient.connect(); await tspmIpcClient.connect();
await new Promise(resolve => setTimeout(resolve, 1000)); await new Promise((resolve) => setTimeout(resolve, 1000));
// Add multiple test processes // Add multiple test processes
const testConfigs: tspm.IProcessConfig[] = [ const testConfigs: tspm.IProcessConfig[] = [
{ {
@@ -152,43 +160,43 @@ tap.test('Batch operations through daemon', async (tools) => {
autorestart: false, autorestart: false,
}, },
]; ];
// Start processes // Start processes
for (const config of testConfigs) { for (const config of testConfigs) {
await tspmIpcClient.request('start', { config }); await tspmIpcClient.request('start', { config });
} }
// Test 1: Stop all processes // Test 1: Stop all processes
const stopAllResponse = await tspmIpcClient.request('stopAll', {}); const stopAllResponse = await tspmIpcClient.request('stopAll', {});
expect(stopAllResponse.stopped).toBeArray(); expect(stopAllResponse.stopped).toBeArray();
expect(stopAllResponse.stopped.length).toBeGreaterThanOrEqual(2); expect(stopAllResponse.stopped.length).toBeGreaterThanOrEqual(2);
// Test 2: Start all processes // Test 2: Start all processes
const startAllResponse = await tspmIpcClient.request('startAll', {}); const startAllResponse = await tspmIpcClient.request('startAll', {});
expect(startAllResponse.started).toBeArray(); expect(startAllResponse.started).toBeArray();
// Test 3: Restart all processes // Test 3: Restart all processes
const restartAllResponse = await tspmIpcClient.request('restartAll', {}); const restartAllResponse = await tspmIpcClient.request('restartAll', {});
expect(restartAllResponse.restarted).toBeArray(); expect(restartAllResponse.restarted).toBeArray();
// Cleanup: delete all test processes // Cleanup: delete all test processes
for (const config of testConfigs) { for (const config of testConfigs) {
await tspmIpcClient.request('delete', { id: config.id }).catch(() => {}); await tspmIpcClient.request('delete', { id: config.id }).catch(() => {});
} }
// Stop daemon // Stop daemon
await tspmIpcClient.stopDaemon(true); await tspmIpcClient.stopDaemon(true);
done.resolve(); done.resolve();
}); });
tap.test('Daemon error handling', async (tools) => { tap.test('Daemon error handling', async (tools) => {
const done = tools.defer(); const done = tools.defer();
// Ensure daemon is running // Ensure daemon is running
await tspmIpcClient.connect(); await tspmIpcClient.connect();
await new Promise(resolve => setTimeout(resolve, 1000)); await new Promise((resolve) => setTimeout(resolve, 1000));
// Test 1: Try to stop non-existent process // Test 1: Try to stop non-existent process
try { try {
await tspmIpcClient.request('stop', { id: 'non-existent-process' }); await tspmIpcClient.request('stop', { id: 'non-existent-process' });
@@ -196,7 +204,7 @@ tap.test('Daemon error handling', async (tools) => {
} catch (error) { } catch (error) {
expect(error.message).toInclude('Failed to stop process'); expect(error.message).toInclude('Failed to stop process');
} }
// Test 2: Try to describe non-existent process // Test 2: Try to describe non-existent process
try { try {
await tspmIpcClient.request('describe', { id: 'non-existent-process' }); await tspmIpcClient.request('describe', { id: 'non-existent-process' });
@@ -204,7 +212,7 @@ tap.test('Daemon error handling', async (tools) => {
} catch (error) { } catch (error) {
expect(error.message).toInclude('not found'); expect(error.message).toInclude('not found');
} }
// Test 3: Try to restart non-existent process // Test 3: Try to restart non-existent process
try { try {
await tspmIpcClient.request('restart', { id: 'non-existent-process' }); await tspmIpcClient.request('restart', { id: 'non-existent-process' });
@@ -212,48 +220,48 @@ tap.test('Daemon error handling', async (tools) => {
} catch (error) { } catch (error) {
expect(error.message).toInclude('Failed to restart process'); expect(error.message).toInclude('Failed to restart process');
} }
// Stop daemon // Stop daemon
await tspmIpcClient.stopDaemon(true); await tspmIpcClient.stopDaemon(true);
done.resolve(); done.resolve();
}); });
tap.test('Daemon heartbeat functionality', async (tools) => { tap.test('Daemon heartbeat functionality', async (tools) => {
const done = tools.defer(); const done = tools.defer();
// Ensure daemon is running // Ensure daemon is running
await tspmIpcClient.connect(); await tspmIpcClient.connect();
await new Promise(resolve => setTimeout(resolve, 1000)); await new Promise((resolve) => setTimeout(resolve, 1000));
// Test heartbeat // Test heartbeat
const heartbeatResponse = await tspmIpcClient.request('heartbeat', {}); const heartbeatResponse = await tspmIpcClient.request('heartbeat', {});
expect(heartbeatResponse.timestamp).toBeGreaterThan(0); expect(heartbeatResponse.timestamp).toBeGreaterThan(0);
expect(heartbeatResponse.status).toEqual('healthy'); expect(heartbeatResponse.status).toEqual('healthy');
// Stop daemon // Stop daemon
await tspmIpcClient.stopDaemon(true); await tspmIpcClient.stopDaemon(true);
done.resolve(); done.resolve();
}); });
tap.test('Daemon memory and CPU reporting', async (tools) => { tap.test('Daemon memory and CPU reporting', async (tools) => {
const done = tools.defer(); const done = tools.defer();
// Ensure daemon is running // Ensure daemon is running
await tspmIpcClient.connect(); await tspmIpcClient.connect();
await new Promise(resolve => setTimeout(resolve, 1000)); await new Promise((resolve) => setTimeout(resolve, 1000));
// Get daemon status // Get daemon status
const status = await tspmIpcClient.getDaemonStatus(); const status = await tspmIpcClient.getDaemonStatus();
expect(status).toBeDefined(); expect(status).toBeDefined();
expect(status?.memoryUsage).toBeGreaterThan(0); expect(status?.memoryUsage).toBeGreaterThan(0);
expect(status?.cpuUsage).toBeGreaterThanOrEqual(0); expect(status?.cpuUsage).toBeGreaterThanOrEqual(0);
expect(status?.uptime).toBeGreaterThan(0); expect(status?.uptime).toBeGreaterThan(0);
// Stop daemon // Stop daemon
await tspmIpcClient.stopDaemon(true); await tspmIpcClient.stopDaemon(true);
done.resolve(); done.resolve();
}); });
@@ -263,4 +271,4 @@ tap.test('Final cleanup', async () => {
await cleanupTestFiles(); await cleanupTestFiles();
}); });
export default tap.start(); export default tap.start();

View File

@@ -14,7 +14,7 @@ tap.test('TspmIpcClient creation', async () => {
tap.test('IPC client socket path', async () => { tap.test('IPC client socket path', async () => {
const client = new TspmIpcClient(); const client = new TspmIpcClient();
const socketPath = (client as any).socketPath; const socketPath = (client as any).socketPath;
expect(socketPath).toInclude('.tspm'); expect(socketPath).toInclude('.tspm');
expect(socketPath).toInclude('tspm.sock'); expect(socketPath).toInclude('tspm.sock');
}); });
@@ -22,7 +22,7 @@ tap.test('IPC client socket path', async () => {
tap.test('IPC client daemon PID file path', async () => { tap.test('IPC client daemon PID file path', async () => {
const client = new TspmIpcClient(); const client = new TspmIpcClient();
const daemonPidFile = (client as any).daemonPidFile; const daemonPidFile = (client as any).daemonPidFile;
expect(daemonPidFile).toInclude('.tspm'); expect(daemonPidFile).toInclude('.tspm');
expect(daemonPidFile).toInclude('daemon.pid'); expect(daemonPidFile).toInclude('daemon.pid');
}); });
@@ -30,7 +30,7 @@ tap.test('IPC client daemon PID file path', async () => {
tap.test('IPC client connection state', async () => { tap.test('IPC client connection state', async () => {
const client = new TspmIpcClient(); const client = new TspmIpcClient();
const isConnected = (client as any).isConnected; const isConnected = (client as any).isConnected;
expect(isConnected).toEqual(false); // Should be false initially expect(isConnected).toEqual(false); // Should be false initially
}); });
@@ -38,10 +38,10 @@ tap.test('IPC client daemon running check - no daemon', async () => {
const client = new TspmIpcClient(); const client = new TspmIpcClient();
const tspmDir = path.join(os.homedir(), '.tspm'); const tspmDir = path.join(os.homedir(), '.tspm');
const pidFile = path.join(tspmDir, 'daemon.pid'); const pidFile = path.join(tspmDir, 'daemon.pid');
// Ensure no PID file exists for this test // Ensure no PID file exists for this test
await fs.unlink(pidFile).catch(() => {}); await fs.unlink(pidFile).catch(() => {});
const isRunning = await (client as any).isDaemonRunning(); const isRunning = await (client as any).isDaemonRunning();
expect(isRunning).toEqual(false); expect(isRunning).toEqual(false);
}); });
@@ -50,18 +50,21 @@ tap.test('IPC client daemon running check - stale PID', async () => {
const client = new TspmIpcClient(); const client = new TspmIpcClient();
const tspmDir = path.join(os.homedir(), '.tspm'); const tspmDir = path.join(os.homedir(), '.tspm');
const pidFile = path.join(tspmDir, 'daemon.pid'); const pidFile = path.join(tspmDir, 'daemon.pid');
// Create directory if it doesn't exist // Create directory if it doesn't exist
await fs.mkdir(tspmDir, { recursive: true }); await fs.mkdir(tspmDir, { recursive: true });
// Write a fake PID that doesn't exist // Write a fake PID that doesn't exist
await fs.writeFile(pidFile, '99999999'); await fs.writeFile(pidFile, '99999999');
const isRunning = await (client as any).isDaemonRunning(); const isRunning = await (client as any).isDaemonRunning();
expect(isRunning).toEqual(false); expect(isRunning).toEqual(false);
// Clean up - the stale PID should be removed // Clean up - the stale PID should be removed
const fileExists = await fs.access(pidFile).then(() => true).catch(() => false); const fileExists = await fs
.access(pidFile)
.then(() => true)
.catch(() => false);
expect(fileExists).toEqual(false); expect(fileExists).toEqual(false);
}); });
@@ -70,19 +73,19 @@ tap.test('IPC client daemon running check - current process', async () => {
const tspmDir = path.join(os.homedir(), '.tspm'); const tspmDir = path.join(os.homedir(), '.tspm');
const pidFile = path.join(tspmDir, 'daemon.pid'); const pidFile = path.join(tspmDir, 'daemon.pid');
const socketFile = path.join(tspmDir, 'tspm.sock'); const socketFile = path.join(tspmDir, 'tspm.sock');
// Create directory if it doesn't exist // Create directory if it doesn't exist
await fs.mkdir(tspmDir, { recursive: true }); await fs.mkdir(tspmDir, { recursive: true });
// Write current process PID (simulating daemon is this process) // Write current process PID (simulating daemon is this process)
await fs.writeFile(pidFile, process.pid.toString()); await fs.writeFile(pidFile, process.pid.toString());
// Create a fake socket file // Create a fake socket file
await fs.writeFile(socketFile, ''); await fs.writeFile(socketFile, '');
const isRunning = await (client as any).isDaemonRunning(); const isRunning = await (client as any).isDaemonRunning();
expect(isRunning).toEqual(true); expect(isRunning).toEqual(true);
// Clean up // Clean up
await fs.unlink(pidFile).catch(() => {}); await fs.unlink(pidFile).catch(() => {});
await fs.unlink(socketFile).catch(() => {}); await fs.unlink(socketFile).catch(() => {});
@@ -91,17 +94,19 @@ tap.test('IPC client daemon running check - current process', async () => {
tap.test('IPC client singleton instance', async () => { tap.test('IPC client singleton instance', async () => {
// Import the singleton // Import the singleton
const { tspmIpcClient } = await import('../ts/classes.ipcclient.js'); const { tspmIpcClient } = await import('../ts/classes.ipcclient.js');
expect(tspmIpcClient).toBeInstanceOf(TspmIpcClient); expect(tspmIpcClient).toBeInstanceOf(TspmIpcClient);
// Test that it's the same instance // Test that it's the same instance
const { tspmIpcClient: secondImport } = await import('../ts/classes.ipcclient.js'); const { tspmIpcClient: secondImport } = await import(
'../ts/classes.ipcclient.js'
);
expect(tspmIpcClient).toBe(secondImport); expect(tspmIpcClient).toBe(secondImport);
}); });
tap.test('IPC client request method type safety', async () => { tap.test('IPC client request method type safety', async () => {
const client = new TspmIpcClient(); const client = new TspmIpcClient();
// Test that request method exists // Test that request method exists
expect(client.request).toBeInstanceOf(Function); expect(client.request).toBeInstanceOf(Function);
expect(client.connect).toBeInstanceOf(Function); expect(client.connect).toBeInstanceOf(Function);
@@ -111,17 +116,18 @@ tap.test('IPC client request method type safety', async () => {
}); });
tap.test('IPC client error message formatting', async () => { tap.test('IPC client error message formatting', async () => {
const errorMessage = 'Could not connect to TSPM daemon. Please try running "tspm daemon start" manually.'; const errorMessage =
'Could not connect to TSPM daemon. Please try running "tspm daemon start" manually.';
expect(errorMessage).toInclude('tspm daemon start'); expect(errorMessage).toInclude('tspm daemon start');
}); });
tap.test('IPC client reconnection logic', async () => { tap.test('IPC client reconnection logic', async () => {
const client = new TspmIpcClient(); const client = new TspmIpcClient();
// Test reconnection error conditions // Test reconnection error conditions
const econnrefusedError = new Error('ECONNREFUSED'); const econnrefusedError = new Error('ECONNREFUSED');
expect(econnrefusedError.message).toInclude('ECONNREFUSED'); expect(econnrefusedError.message).toInclude('ECONNREFUSED');
const enoentError = new Error('ENOENT'); const enoentError = new Error('ENOENT');
expect(enoentError.message).toInclude('ENOENT'); expect(enoentError.message).toInclude('ENOENT');
}); });
@@ -129,7 +135,7 @@ tap.test('IPC client reconnection logic', async () => {
tap.test('IPC client daemon start timeout', async () => { tap.test('IPC client daemon start timeout', async () => {
const maxWaitTime = 10000; // 10 seconds const maxWaitTime = 10000; // 10 seconds
const checkInterval = 500; // 500ms const checkInterval = 500; // 500ms
const maxChecks = maxWaitTime / checkInterval; const maxChecks = maxWaitTime / checkInterval;
expect(maxChecks).toEqual(20); expect(maxChecks).toEqual(20);
}); });
@@ -137,9 +143,9 @@ tap.test('IPC client daemon start timeout', async () => {
tap.test('IPC client daemon stop timeout', async () => { tap.test('IPC client daemon stop timeout', async () => {
const maxWaitTime = 15000; // 15 seconds const maxWaitTime = 15000; // 15 seconds
const checkInterval = 500; // 500ms const checkInterval = 500; // 500ms
const maxChecks = maxWaitTime / checkInterval; const maxChecks = maxWaitTime / checkInterval;
expect(maxChecks).toEqual(30); expect(maxChecks).toEqual(30);
}); });
export default tap.start(); export default tap.start();

View File

@@ -3,6 +3,6 @@
*/ */
export const commitinfo = { export const commitinfo = {
name: '@git.zone/tspm', name: '@git.zone/tspm',
version: '2.0.0', version: '3.0.0',
description: 'a no fuzz process manager' description: 'a no fuzz process manager'
} }

View File

@@ -43,12 +43,12 @@ export class TspmDaemon {
this.ipcServer = plugins.smartipc.SmartIpc.createServer({ this.ipcServer = plugins.smartipc.SmartIpc.createServer({
id: 'tspm-daemon', id: 'tspm-daemon',
socketPath: this.socketPath, socketPath: this.socketPath,
autoCleanupSocketFile: true, // Clean up stale sockets autoCleanupSocketFile: true, // Clean up stale sockets
socketMode: 0o600, // Set proper permissions socketMode: 0o600, // Set proper permissions
heartbeat: true, heartbeat: true,
heartbeatInterval: 5000, heartbeatInterval: 5000,
heartbeatTimeout: 20000, heartbeatTimeout: 20000,
heartbeatInitialGracePeriodMs: 10000 // Grace period for startup heartbeatInitialGracePeriodMs: 10000, // Grace period for startup
}); });
// Register message handlers // Register message handlers
@@ -65,7 +65,7 @@ export class TspmDaemon {
// Load existing process configurations // Load existing process configurations
await this.tspmInstance.loadProcessConfigs(); await this.tspmInstance.loadProcessConfigs();
// Set up log publishing // Set up log publishing
this.tspmInstance.on('process:log', ({ processId, log }) => { this.tspmInstance.on('process:log', ({ processId, log }) => {
// Publish to topic for this process // Publish to topic for this process
@@ -122,19 +122,22 @@ export class TspmDaemon {
}, },
); );
this.ipcServer.onMessage('restart', async (request: RequestForMethod<'restart'>) => { this.ipcServer.onMessage(
try { 'restart',
await this.tspmInstance.restart(request.id); async (request: RequestForMethod<'restart'>) => {
const processInfo = this.tspmInstance.processInfo.get(request.id); try {
return { await this.tspmInstance.restart(request.id);
processId: request.id, const processInfo = this.tspmInstance.processInfo.get(request.id);
pid: processInfo?.pid, return {
status: processInfo?.status || 'stopped', processId: request.id,
}; pid: processInfo?.pid,
} catch (error) { status: processInfo?.status || 'stopped',
throw new Error(`Failed to restart process: ${error.message}`); };
} } catch (error) {
}); throw new Error(`Failed to restart process: ${error.message}`);
}
},
);
this.ipcServer.onMessage( this.ipcServer.onMessage(
'delete', 'delete',
@@ -160,124 +163,148 @@ export class TspmDaemon {
}, },
); );
this.ipcServer.onMessage('describe', async (request: RequestForMethod<'describe'>) => { this.ipcServer.onMessage(
const processInfo = await this.tspmInstance.describe(request.id); 'describe',
const config = this.tspmInstance.processConfigs.get(request.id); async (request: RequestForMethod<'describe'>) => {
const processInfo = await this.tspmInstance.describe(request.id);
const config = this.tspmInstance.processConfigs.get(request.id);
if (!processInfo || !config) { if (!processInfo || !config) {
throw new Error(`Process ${request.id} not found`); throw new Error(`Process ${request.id} not found`);
} }
return { return {
processInfo, processInfo,
config, config,
}; };
}); },
);
this.ipcServer.onMessage('getLogs', async (request: RequestForMethod<'getLogs'>) => { this.ipcServer.onMessage(
const logs = await this.tspmInstance.getLogs(request.id); 'getLogs',
return { logs }; async (request: RequestForMethod<'getLogs'>) => {
}); const logs = await this.tspmInstance.getLogs(request.id);
return { logs };
},
);
// Batch operations handlers // Batch operations handlers
this.ipcServer.onMessage('startAll', async (request: RequestForMethod<'startAll'>) => { this.ipcServer.onMessage(
const started: string[] = []; 'startAll',
const failed: Array<{ id: string; error: string }> = []; 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 // Get status of all processes
for (const [id, processInfo] of this.tspmInstance.processInfo) { for (const [id, processInfo] of this.tspmInstance.processInfo) {
if (processInfo.status === 'online') { if (processInfo.status === 'online') {
started.push(id); started.push(id);
} else { } else {
failed.push({ id, error: 'Failed to start' }); failed.push({ id, error: 'Failed to start' });
}
} }
}
return { started, failed }; return { started, failed };
}); },
);
this.ipcServer.onMessage('stopAll', async (request: RequestForMethod<'stopAll'>) => { this.ipcServer.onMessage(
const stopped: string[] = []; 'stopAll',
const failed: Array<{ id: string; error: string }> = []; 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 // Get status of all processes
for (const [id, processInfo] of this.tspmInstance.processInfo) { for (const [id, processInfo] of this.tspmInstance.processInfo) {
if (processInfo.status === 'stopped') { if (processInfo.status === 'stopped') {
stopped.push(id); stopped.push(id);
} else { } else {
failed.push({ id, error: 'Failed to stop' }); failed.push({ id, error: 'Failed to stop' });
}
} }
}
return { stopped, failed }; return { stopped, failed };
}); },
);
this.ipcServer.onMessage('restartAll', async (request: RequestForMethod<'restartAll'>) => { this.ipcServer.onMessage(
const restarted: string[] = []; 'restartAll',
const failed: Array<{ id: string; error: string }> = []; 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 // Get status of all processes
for (const [id, processInfo] of this.tspmInstance.processInfo) { for (const [id, processInfo] of this.tspmInstance.processInfo) {
if (processInfo.status === 'online') { if (processInfo.status === 'online') {
restarted.push(id); restarted.push(id);
} else { } else {
failed.push({ id, error: 'Failed to restart' }); failed.push({ id, error: 'Failed to restart' });
}
} }
}
return { restarted, failed }; return { restarted, failed };
}); },
);
// Daemon management handlers // Daemon management handlers
this.ipcServer.onMessage('daemon:status', async (request: RequestForMethod<'daemon:status'>) => { this.ipcServer.onMessage(
const memUsage = process.memoryUsage(); 'daemon:status',
return { async (request: RequestForMethod<'daemon:status'>) => {
status: 'running', const memUsage = process.memoryUsage();
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) {
return { return {
success: false, status: 'running',
message: 'Daemon is already shutting down', 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 this.ipcServer.onMessage(
const graceful = request.graceful !== false; 'daemon:shutdown',
const timeout = request.timeout || 10000; async (request: RequestForMethod<'daemon:shutdown'>) => {
if (this.isShuttingDown) {
return {
success: false,
message: 'Daemon is already shutting down',
};
}
if (graceful) { // Schedule shutdown
setTimeout(() => this.shutdown(true), 100); const graceful = request.graceful !== false;
} else { const timeout = request.timeout || 10000;
setTimeout(() => this.shutdown(false), 100);
}
return { if (graceful) {
success: true, setTimeout(() => this.shutdown(true), 100);
message: `Daemon will shutdown ${graceful ? 'gracefully' : 'immediately'} in ${timeout}ms`, } else {
}; setTimeout(() => this.shutdown(false), 100);
}); }
return {
success: true,
message: `Daemon will shutdown ${graceful ? 'gracefully' : 'immediately'} in ${timeout}ms`,
};
},
);
// Heartbeat handler // Heartbeat handler
this.ipcServer.onMessage('heartbeat', async (request: RequestForMethod<'heartbeat'>) => { this.ipcServer.onMessage(
return { 'heartbeat',
timestamp: Date.now(), async (request: RequestForMethod<'heartbeat'>) => {
status: this.isShuttingDown ? 'degraded' : 'healthy', return {
}; timestamp: Date.now(),
}); status: this.isShuttingDown ? 'degraded' : 'healthy',
};
},
);
} }
/** /**

View File

@@ -36,9 +36,9 @@ export class TspmIpcClient {
if (!daemonRunning) { if (!daemonRunning) {
throw new Error( throw new Error(
'TSPM daemon is not running.\n\n' + 'TSPM daemon is not running.\n\n' +
'To start the daemon, run one of:\n' + 'To start the daemon, run one of:\n' +
' tspm daemon start - Start daemon for this session\n' + ' tspm daemon start - Start daemon for this session\n' +
' tspm enable - Enable daemon as system service (recommended)\n' ' tspm enable - Enable daemon as system service (recommended)\n',
); );
} }
@@ -59,20 +59,20 @@ export class TspmIpcClient {
heartbeatInterval: 5000, heartbeatInterval: 5000,
heartbeatTimeout: 20000, heartbeatTimeout: 20000,
heartbeatInitialGracePeriodMs: 10000, heartbeatInitialGracePeriodMs: 10000,
heartbeatThrowOnTimeout: false // Don't throw, emit events instead heartbeatThrowOnTimeout: false, // Don't throw, emit events instead
}); });
// Connect to the daemon // Connect to the daemon
try { try {
await this.ipcClient.connect({ waitForReady: true }); await this.ipcClient.connect({ waitForReady: true });
this.isConnected = true; this.isConnected = true;
// Handle heartbeat timeouts gracefully // Handle heartbeat timeouts gracefully
this.ipcClient.on('heartbeatTimeout', () => { this.ipcClient.on('heartbeatTimeout', () => {
console.warn('Heartbeat timeout detected, connection may be degraded'); console.warn('Heartbeat timeout detected, connection may be degraded');
this.isConnected = false; this.isConnected = false;
}); });
console.log('Connected to TSPM daemon'); console.log('Connected to TSPM daemon');
} catch (error) { } catch (error) {
console.error('Failed to connect to daemon:', error); console.error('Failed to connect to daemon:', error);
@@ -117,19 +117,22 @@ export class TspmIpcClient {
throw error; throw error;
} }
} }
/** /**
* Subscribe to log updates for a specific process * 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) { if (!this.ipcClient || !this.isConnected) {
throw new Error('Not connected to daemon'); throw new Error('Not connected to daemon');
} }
const topic = `logs.${processId}`; const topic = `logs.${processId}`;
await this.ipcClient.subscribe(`topic:${topic}`, handler); await this.ipcClient.subscribe(`topic:${topic}`, handler);
} }
/** /**
* Unsubscribe from log updates for a specific process * Unsubscribe from log updates for a specific process
*/ */
@@ -137,7 +140,7 @@ export class TspmIpcClient {
if (!this.ipcClient || !this.isConnected) { if (!this.ipcClient || !this.isConnected) {
throw new Error('Not connected to daemon'); throw new Error('Not connected to daemon');
} }
const topic = `logs.${processId}`; const topic = `logs.${processId}`;
await this.ipcClient.unsubscribe(`topic:${topic}`); await this.ipcClient.unsubscribe(`topic:${topic}`);
} }
@@ -160,7 +163,7 @@ export class TspmIpcClient {
// Check if process is running // Check if process is running
try { try {
process.kill(pid, 0); process.kill(pid, 0);
// PID is alive, daemon is running // PID is alive, daemon is running
// Socket check is advisory only - the connect retry will handle transient socket issues // Socket check is advisory only - the connect retry will handle transient socket issues
try { try {
@@ -184,8 +187,6 @@ export class TspmIpcClient {
} }
} }
/** /**
* Stop the daemon * Stop the daemon
*/ */

View File

@@ -69,7 +69,7 @@ export class ProcessMonitor extends EventEmitter {
this.processWrapper.on('log', (log: IProcessLog): void => { this.processWrapper.on('log', (log: IProcessLog): void => {
// Re-emit the log event for upstream handlers // Re-emit the log event for upstream handlers
this.emit('log', log); this.emit('log', log);
// Log system messages to the console // Log system messages to the console
if (log.type === 'system') { if (log.type === 'system') {
this.log(log.message); this.log(log.message);

View File

@@ -18,14 +18,14 @@ export class TspmServiceManager {
private async getOrCreateService(): Promise<any> { private async getOrCreateService(): Promise<any> {
if (!this.service) { if (!this.service) {
const cliPath = plugins.path.join(paths.packageDir, 'cli.js'); const cliPath = plugins.path.join(paths.packageDir, 'cli.js');
// Create service configuration // Create service configuration
this.service = await this.smartDaemon.addService({ this.service = await this.smartDaemon.addService({
name: 'tspm-daemon', name: 'tspm-daemon',
description: 'TSPM Process Manager Daemon', description: 'TSPM Process Manager Daemon',
command: `${process.execPath} ${cliPath} daemon start-service`, command: `${process.execPath} ${cliPath} daemon start-service`,
workingDir: process.env.HOME || process.cwd(), workingDir: process.env.HOME || process.cwd(),
version: '1.0.0' version: '1.0.0',
}); });
} }
return this.service; return this.service;
@@ -36,13 +36,13 @@ export class TspmServiceManager {
*/ */
public async enableService(): Promise<void> { public async enableService(): Promise<void> {
const service = await this.getOrCreateService(); const service = await this.getOrCreateService();
// Save service configuration // Save service configuration
await service.save(); await service.save();
// Enable service to start on boot // Enable service to start on boot
await service.enable(); await service.enable();
// Start the service immediately // Start the service immediately
await service.start(); await service.start();
} }
@@ -52,7 +52,7 @@ export class TspmServiceManager {
*/ */
public async disableService(): Promise<void> { public async disableService(): Promise<void> {
const service = await this.getOrCreateService(); const service = await this.getOrCreateService();
// Stop the service if running // Stop the service if running
try { try {
await service.stop(); await service.stop();
@@ -60,7 +60,7 @@ export class TspmServiceManager {
// Service might not be running // Service might not be running
console.log('Service was not running'); console.log('Service was not running');
} }
// Disable service from starting on boot // Disable service from starting on boot
await service.disable(); await service.disable();
} }
@@ -75,20 +75,20 @@ export class TspmServiceManager {
}> { }> {
try { try {
await this.getOrCreateService(); await this.getOrCreateService();
// Note: SmartDaemon doesn't provide direct status methods, // Note: SmartDaemon doesn't provide direct status methods,
// so we'll need to check via systemctl commands // so we'll need to check via systemctl commands
// This is a simplified implementation // This is a simplified implementation
return { return {
enabled: true, // Would need to check systemctl is-enabled enabled: true, // Would need to check systemctl is-enabled
running: true, // Would need to check systemctl is-active running: true, // Would need to check systemctl is-active
status: 'active' status: 'active',
}; };
} catch (error) { } catch (error) {
return { return {
enabled: false, enabled: false,
running: false, running: false,
status: 'inactive' status: 'inactive',
}; };
} }
} }
@@ -100,4 +100,4 @@ export class TspmServiceManager {
const service = await this.getOrCreateService(); const service = await this.getOrCreateService();
await service.reload(); await service.reload();
} }
} }

View File

@@ -32,8 +32,6 @@ export interface IProcessInfo {
restarts: number; restarts: number;
} }
export class Tspm extends EventEmitter { export class Tspm extends EventEmitter {
public processes: Map<string, ProcessMonitor> = new Map(); public processes: Map<string, ProcessMonitor> = new Map();
public processConfigs: Map<string, IProcessConfig> = new Map(); public processConfigs: Map<string, IProcessConfig> = new Map();
@@ -97,12 +95,12 @@ export class Tspm extends EventEmitter {
}); });
this.processes.set(config.id, monitor); this.processes.set(config.id, monitor);
// Set up log event handler to re-emit for pub/sub // Set up log event handler to re-emit for pub/sub
monitor.on('log', (log: IProcessLog) => { monitor.on('log', (log: IProcessLog) => {
this.emit('process:log', { processId: config.id, log }); this.emit('process:log', { processId: config.id, log });
}); });
monitor.start(); monitor.start();
// Update process info // Update process info

View File

@@ -1,2 +1,2 @@
// Re-export from the new modular CLI structure // Re-export from the new modular CLI structure
export * from './cli/index.js'; export * from './cli/index.js';

View File

@@ -4,23 +4,28 @@ import type { CliArguments } from '../../types.js';
import { registerIpcCommand } from '../../registration/index.js'; import { registerIpcCommand } from '../../registration/index.js';
export function registerRestartAllCommand(smartcli: plugins.smartcli.Smartcli) { export function registerRestartAllCommand(smartcli: plugins.smartcli.Smartcli) {
registerIpcCommand(smartcli, 'restart-all', async (_argvArg: CliArguments) => { registerIpcCommand(
console.log('Restarting all processes...'); smartcli,
const response = await tspmIpcClient.request('restartAll', {}); 'restart-all',
async (_argvArg: CliArguments) => {
console.log('Restarting all processes...');
const response = await tspmIpcClient.request('restartAll', {});
if (response.restarted.length > 0) { if (response.restarted.length > 0) {
console.log(`✓ Restarted ${response.restarted.length} processes:`); console.log(`✓ Restarted ${response.restarted.length} processes:`);
for (const id of response.restarted) { for (const id of response.restarted) {
console.log(` - ${id}`); console.log(` - ${id}`);
}
} }
}
if (response.failed.length > 0) { if (response.failed.length > 0) {
console.log(`✗ Failed to restart ${response.failed.length} processes:`); console.log(`✗ Failed to restart ${response.failed.length} processes:`);
for (const failure of response.failed) { for (const failure of response.failed) {
console.log(` - ${failure.id}: ${failure.error}`); 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' }); );
} }

View File

@@ -4,23 +4,28 @@ import type { CliArguments } from '../../types.js';
import { registerIpcCommand } from '../../registration/index.js'; import { registerIpcCommand } from '../../registration/index.js';
export function registerStartAllCommand(smartcli: plugins.smartcli.Smartcli) { export function registerStartAllCommand(smartcli: plugins.smartcli.Smartcli) {
registerIpcCommand(smartcli, 'start-all', async (_argvArg: CliArguments) => { registerIpcCommand(
console.log('Starting all processes...'); smartcli,
const response = await tspmIpcClient.request('startAll', {}); 'start-all',
async (_argvArg: CliArguments) => {
console.log('Starting all processes...');
const response = await tspmIpcClient.request('startAll', {});
if (response.started.length > 0) { if (response.started.length > 0) {
console.log(`✓ Started ${response.started.length} processes:`); console.log(`✓ Started ${response.started.length} processes:`);
for (const id of response.started) { for (const id of response.started) {
console.log(` - ${id}`); console.log(` - ${id}`);
}
} }
}
if (response.failed.length > 0) { if (response.failed.length > 0) {
console.log(`✗ Failed to start ${response.failed.length} processes:`); console.log(`✗ Failed to start ${response.failed.length} processes:`);
for (const failure of response.failed) { for (const failure of response.failed) {
console.log(` - ${failure.id}: ${failure.error}`); 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' }); );
} }

View File

@@ -4,23 +4,28 @@ import type { CliArguments } from '../../types.js';
import { registerIpcCommand } from '../../registration/index.js'; import { registerIpcCommand } from '../../registration/index.js';
export function registerStopAllCommand(smartcli: plugins.smartcli.Smartcli) { export function registerStopAllCommand(smartcli: plugins.smartcli.Smartcli) {
registerIpcCommand(smartcli, 'stop-all', async (_argvArg: CliArguments) => { registerIpcCommand(
console.log('Stopping all processes...'); smartcli,
const response = await tspmIpcClient.request('stopAll', {}); 'stop-all',
async (_argvArg: CliArguments) => {
console.log('Stopping all processes...');
const response = await tspmIpcClient.request('stopAll', {});
if (response.stopped.length > 0) { if (response.stopped.length > 0) {
console.log(`✓ Stopped ${response.stopped.length} processes:`); console.log(`✓ Stopped ${response.stopped.length} processes:`);
for (const id of response.stopped) { for (const id of response.stopped) {
console.log(` - ${id}`); console.log(` - ${id}`);
}
} }
}
if (response.failed.length > 0) { if (response.failed.length > 0) {
console.log(`✗ Failed to stop ${response.failed.length} processes:`); console.log(`✗ Failed to stop ${response.failed.length} processes:`);
for (const failure of response.failed) { for (const failure of response.failed) {
console.log(` - ${failure.id}: ${failure.error}`); 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' }); );
} }

View File

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

View File

@@ -9,7 +9,7 @@ import { formatMemory } from '../helpers/memory.js';
export function registerDefaultCommand(smartcli: plugins.smartcli.Smartcli) { export function registerDefaultCommand(smartcli: plugins.smartcli.Smartcli) {
const cliLogger = new Logger('CLI'); const cliLogger = new Logger('CLI');
const tspmProjectinfo = new plugins.projectinfo.ProjectInfo(paths.packageDir); const tspmProjectinfo = new plugins.projectinfo.ProjectInfo(paths.packageDir);
smartcli.standardCommand().subscribe({ smartcli.standardCommand().subscribe({
next: async (argvArg: CliArguments) => { next: async (argvArg: CliArguments) => {
console.log( console.log(
@@ -17,7 +17,9 @@ export function registerDefaultCommand(smartcli: plugins.smartcli.Smartcli) {
); );
console.log('Usage: tspm [command] [options]'); console.log('Usage: tspm [command] [options]');
console.log('\nService Management:'); 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(' disable Disable TSPM system service');
console.log('\nProcess Commands:'); console.log('\nProcess Commands:');
console.log(' start <script> Start a process'); 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(' stop-all Stop all processes');
console.log(' restart-all Restart all processes'); console.log(' restart-all Restart all processes');
console.log('\nDaemon Commands:'); 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 stop Stop the daemon');
console.log(' daemon status Show daemon status'); console.log(' daemon status Show daemon status');
console.log( console.log(
@@ -78,14 +82,16 @@ export function registerDefaultCommand(smartcli: plugins.smartcli.Smartcli) {
'└─────────┴─────────────┴───────────┴───────────┴──────────┘', '└─────────┴─────────────┴───────────┴───────────┴──────────┘',
); );
} }
// Disconnect from daemon after getting list // Disconnect from daemon after getting list
await tspmIpcClient.disconnect(); await tspmIpcClient.disconnect();
} catch (error) { } catch (error) {
console.error('Error: TSPM daemon is not running.'); console.error('Error: TSPM daemon is not running.');
console.log('\nTo start the daemon, run one of:'); console.log('\nTo start the daemon, run one of:');
console.log(' tspm daemon start - Start for this session only'); 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) => { error: (err) => {
@@ -93,4 +99,4 @@ export function registerDefaultCommand(smartcli: plugins.smartcli.Smartcli) {
}, },
complete: () => {}, complete: () => {},
}); });
} }

View File

@@ -4,21 +4,26 @@ import type { CliArguments } from '../../types.js';
import { registerIpcCommand } from '../../registration/index.js'; import { registerIpcCommand } from '../../registration/index.js';
export function registerDeleteCommand(smartcli: plugins.smartcli.Smartcli) { export function registerDeleteCommand(smartcli: plugins.smartcli.Smartcli) {
registerIpcCommand(smartcli, 'delete', async (argvArg: CliArguments) => { registerIpcCommand(
const id = argvArg._[1]; smartcli,
if (!id) { 'delete',
console.error('Error: Please provide a process ID'); async (argvArg: CliArguments) => {
console.log('Usage: tspm delete <id>'); const id = argvArg._[1];
return; if (!id) {
} console.error('Error: Please provide a process ID');
console.log('Usage: tspm delete <id>');
return;
}
console.log(`Deleting process: ${id}`); console.log(`Deleting process: ${id}`);
const response = await tspmIpcClient.request('delete', { id }); const response = await tspmIpcClient.request('delete', { id });
if (response.success) { if (response.success) {
console.log(`${response.message}`); console.log(`${response.message}`);
} else { } else {
console.error(`✗ Failed to delete process: ${response.message}`); console.error(`✗ Failed to delete process: ${response.message}`);
} }
}, { actionLabel: 'delete process' }); },
} { actionLabel: 'delete process' },
);
}

View File

@@ -5,34 +5,45 @@ import { registerIpcCommand } from '../../registration/index.js';
import { formatMemory } from '../../helpers/memory.js'; import { formatMemory } from '../../helpers/memory.js';
export function registerDescribeCommand(smartcli: plugins.smartcli.Smartcli) { export function registerDescribeCommand(smartcli: plugins.smartcli.Smartcli) {
registerIpcCommand(smartcli, 'describe', async (argvArg: CliArguments) => { registerIpcCommand(
const id = argvArg._[1]; smartcli,
if (!id) { 'describe',
console.error('Error: Please provide a process ID'); async (argvArg: CliArguments) => {
console.log('Usage: tspm describe <id>'); const id = argvArg._[1];
return; if (!id) {
} console.error('Error: Please provide a process ID');
console.log('Usage: tspm describe <id>');
const response = await tspmIpcClient.request('describe', { id }); return;
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' }); 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' },
);
}

View File

@@ -6,32 +6,47 @@ import { pad } from '../../helpers/formatting.js';
import { formatMemory } from '../../helpers/memory.js'; import { formatMemory } from '../../helpers/memory.js';
export function registerListCommand(smartcli: plugins.smartcli.Smartcli) { export function registerListCommand(smartcli: plugins.smartcli.Smartcli) {
registerIpcCommand(smartcli, 'list', async (_argvArg: CliArguments) => { registerIpcCommand(
const response = await tspmIpcClient.request('list', {}); smartcli,
const processes = response.processes; 'list',
async (_argvArg: CliArguments) => {
const response = await tspmIpcClient.request('list', {});
const processes = response.processes;
if (processes.length === 0) { if (processes.length === 0) {
console.log('No processes running.'); console.log('No processes running.');
return; return;
} }
console.log('Process List:'); console.log('Process List:');
console.log('┌─────────┬─────────────┬───────────┬───────────┬──────────┬──────────┐'); console.log(
console.log('│ ID │ Name │ Status │ PID │ Memory │ Restarts │'); '┌─────────┬─────────────┬───────────┬───────────┬──────────┬──────────┐',
console.log('├─────────┼─────────────┼───────────┼───────────┼──────────┼──────────┤'); );
console.log(
'│ ID │ Name │ Status │ PID │ Memory │ Restarts │',
);
console.log(
'├─────────┼─────────────┼───────────┼───────────┼──────────┼──────────┤',
);
for (const proc of processes) { for (const proc of processes) {
const statusColor = const statusColor =
proc.status === 'online' ? '\x1b[32m' : proc.status === 'online'
proc.status === 'errored' ? '\x1b[31m' : ? '\x1b[32m'
'\x1b[33m'; : proc.status === 'errored'
const resetColor = '\x1b[0m'; ? '\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( 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)}`, '└─────────┴─────────────┴───────────┴───────────┴──────────┴──────────┘',
); );
} },
{ actionLabel: 'list processes' },
console.log('└─────────┴─────────────┴───────────┴───────────┴──────────┴──────────┘'); );
}, { actionLabel: 'list processes' }); }
}

View File

@@ -7,67 +7,93 @@ import { formatLog } from '../../helpers/formatting.js';
import { withStreamingLifecycle } from '../../helpers/lifecycle.js'; import { withStreamingLifecycle } from '../../helpers/lifecycle.js';
export function registerLogsCommand(smartcli: plugins.smartcli.Smartcli) { export function registerLogsCommand(smartcli: plugins.smartcli.Smartcli) {
registerIpcCommand(smartcli, 'logs', async (argvArg: CliArguments) => { registerIpcCommand(
const id = argvArg._[1]; smartcli,
if (!id) { 'logs',
console.error('Error: Please provide a process ID'); async (argvArg: CliArguments) => {
console.log('Usage: tspm logs <id> [options]'); const id = argvArg._[1];
console.log('\nOptions:'); if (!id) {
console.log(' --lines <n> Number of lines to show (default: 50)'); console.error('Error: Please provide a process ID');
console.log(' --follow Stream logs in real-time (like tail -f)'); console.log('Usage: tspm logs <id> [options]');
return; 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 lines = getNumber(argvArg, 'lines', 50);
const follow = getBool(argvArg, 'follow', 'f'); const follow = getBool(argvArg, 'follow', 'f');
const response = await tspmIpcClient.request('getLogs', { id, lines }); const response = await tspmIpcClient.request('getLogs', { id, lines });
if (!follow) { if (!follow) {
// One-shot mode - auto-disconnect handled by registerIpcCommand // One-shot mode - auto-disconnect handled by registerIpcCommand
console.log(`Logs for process: ${id} (last ${lines} lines)`); 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)); console.log('─'.repeat(60));
let lastSeq = 0;
for (const log of response.logs) { for (const log of response.logs) {
const timestamp = new Date(log.timestamp).toLocaleTimeString(); 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}`); console.log(`${timestamp} ${prefix} ${log.message}`);
if (log.seq !== undefined) lastSeq = Math.max(lastSeq, log.seq);
} }
return;
}
// Streaming mode await withStreamingLifecycle(
console.log(`Logs for process: ${id} (streaming...)`); async () => {
console.log('─'.repeat(60)); await tspmIpcClient.subscribe(id, (log: any) => {
if (log.seq !== undefined && log.seq <= lastSeq) return;
let lastSeq = 0; if (log.seq !== undefined && log.seq > lastSeq + 1) {
for (const log of response.logs) { console.log(
const timestamp = new Date(log.timestamp).toLocaleTimeString(); `[WARNING] Log gap detected: expected seq ${lastSeq + 1}, got ${log.seq}`,
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); const timestamp = new Date(log.timestamp).toLocaleTimeString();
} const prefix =
log.type === 'stdout'
await withStreamingLifecycle( ? '[OUT]'
async () => { : log.type === 'stderr'
await tspmIpcClient.subscribe(id, (log: any) => { ? '[ERR]'
if (log.seq !== undefined && log.seq <= lastSeq) return; : '[SYS]';
if (log.seq !== undefined && log.seq > lastSeq + 1) { console.log(`${timestamp} ${prefix} ${log.message}`);
console.log(`[WARNING] Log gap detected: expected seq ${lastSeq + 1}, got ${log.seq}`); if (log.seq !== undefined) lastSeq = log.seq;
} });
const timestamp = new Date(log.timestamp).toLocaleTimeString(); },
const prefix = log.type === 'stdout' ? '[OUT]' : log.type === 'stderr' ? '[ERR]' : '[SYS]'; async () => {
console.log(`${timestamp} ${prefix} ${log.message}`); console.log('\n\nStopping log stream...');
if (log.seq !== undefined) lastSeq = log.seq; try {
}); await tspmIpcClient.unsubscribe(id);
}, } catch {}
async () => { try {
console.log('\n\nStopping log stream...'); await tspmIpcClient.disconnect();
try { await tspmIpcClient.unsubscribe(id); } catch {} } catch {}
try { await tspmIpcClient.disconnect(); } catch {} },
} );
); },
}, { {
actionLabel: 'get logs', actionLabel: 'get logs',
keepAlive: (argv) => getBool(argv, 'follow', 'f') keepAlive: (argv) => getBool(argv, 'follow', 'f'),
}); },
} );
}

View File

@@ -4,20 +4,25 @@ import type { CliArguments } from '../../types.js';
import { registerIpcCommand } from '../../registration/index.js'; import { registerIpcCommand } from '../../registration/index.js';
export function registerRestartCommand(smartcli: plugins.smartcli.Smartcli) { export function registerRestartCommand(smartcli: plugins.smartcli.Smartcli) {
registerIpcCommand(smartcli, 'restart', async (argvArg: CliArguments) => { registerIpcCommand(
const id = argvArg._[1]; smartcli,
if (!id) { 'restart',
console.error('Error: Please provide a process ID'); async (argvArg: CliArguments) => {
console.log('Usage: tspm restart <id>'); const id = argvArg._[1];
return; if (!id) {
} console.error('Error: Please provide a process ID');
console.log('Usage: tspm restart <id>');
return;
}
console.log(`Restarting process: ${id}`); console.log(`Restarting process: ${id}`);
const response = await tspmIpcClient.request('restart', { id }); const response = await tspmIpcClient.request('restart', { id });
console.log(`✓ Process restarted successfully`); console.log(`✓ Process restarted successfully`);
console.log(` ID: ${response.processId}`); console.log(` ID: ${response.processId}`);
console.log(` PID: ${response.pid || 'N/A'}`); console.log(` PID: ${response.pid || 'N/A'}`);
console.log(` Status: ${response.status}`); console.log(` Status: ${response.status}`);
}, { actionLabel: 'restart process' }); },
} { actionLabel: 'restart process' },
);
}

View File

@@ -6,78 +6,97 @@ import { parseMemoryString, formatMemory } from '../../helpers/memory.js';
import { registerIpcCommand } from '../../registration/index.js'; import { registerIpcCommand } from '../../registration/index.js';
export function registerStartCommand(smartcli: plugins.smartcli.Smartcli) { export function registerStartCommand(smartcli: plugins.smartcli.Smartcli) {
registerIpcCommand(smartcli, 'start', async (argvArg: CliArguments) => { registerIpcCommand(
const script = argvArg._[1]; smartcli,
if (!script) { 'start',
console.error('Error: Please provide a script to run'); async (argvArg: CliArguments) => {
console.log('Usage: tspm start <script> [options]'); const script = argvArg._[1];
console.log('\nOptions:'); if (!script) {
console.log(' --name <name> Name for the process'); console.error('Error: Please provide a script to run');
console.log(' --memory <size> Memory limit (e.g., "512MB", "2GB")'); console.log('Usage: tspm start <script> [options]');
console.log(' --cwd <path> Working directory'); console.log('\nOptions:');
console.log(' --watch Watch for file changes and restart'); console.log(' --name <name> Name for the process');
console.log(' --watch-paths <paths> Comma-separated paths to watch'); console.log(
console.log(' --autorestart Auto-restart on crash'); ' --memory <size> Memory limit (e.g., "512MB", "2GB")',
return; );
} console.log(' --cwd <path> Working directory');
console.log(
const memoryLimit = argvArg.memory ? parseMemoryString(argvArg.memory) : 512 * 1024 * 1024; ' --watch Watch for file changes and restart',
const projectDir = argvArg.cwd || process.cwd(); );
console.log(' --watch-paths <paths> Comma-separated paths to watch');
// Direct .ts support via tsx (bundled with TSPM) console.log(' --autorestart Auto-restart on crash');
let actualCommand = script; return;
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];
} }
}
const name = argvArg.name || script; const memoryLimit = argvArg.memory
const watch = argvArg.watch || false; ? parseMemoryString(argvArg.memory)
const autorestart = argvArg.autorestart !== false; // default true : 512 * 1024 * 1024;
const watchPaths = argvArg.watchPaths const projectDir = argvArg.cwd || process.cwd();
? (typeof argvArg.watchPaths === 'string' ? (argvArg.watchPaths as string).split(',') : argvArg.watchPaths)
: undefined;
const processConfig: IProcessConfig = { // Direct .ts support via tsx (bundled with TSPM)
id: name.replace(/[^a-zA-Z0-9-_]/g, '_'), let actualCommand = script;
name, let commandArgs: string[] | undefined = undefined;
command: actualCommand,
args: commandArgs,
projectDir,
memoryLimitBytes: memoryLimit,
autorestart,
watch,
watchPaths,
};
console.log(`Starting process: ${name}`); if (script.endsWith('.ts')) {
console.log(` Command: ${script}${script.endsWith('.ts') ? ' (via tsx)' : ''}`); try {
console.log(` Directory: ${projectDir}`); const tsxPath = await (async () => {
console.log(` Memory limit: ${formatMemory(memoryLimit)}`); const { createRequire } = await import('module');
console.log(` Auto-restart: ${autorestart}`); const require = createRequire(import.meta.url);
if (watch) { return require.resolve('tsx/dist/cli.mjs');
console.log(` Watch mode: enabled`); })();
if (watchPaths) console.log(` Watch paths: ${watchPaths.join(', ')}`);
}
const response = await tspmIpcClient.request('start', { config: processConfig }); const scriptPath = plugins.path.isAbsolute(script)
console.log(`✓ Process started successfully`); ? script
console.log(` ID: ${response.processId}`); : plugins.path.join(projectDir, script);
console.log(` PID: ${response.pid || 'N/A'}`); actualCommand = tsxPath;
console.log(` Status: ${response.status}`); commandArgs = [scriptPath];
}, { actionLabel: 'start process' }); } 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' },
);
}

View File

@@ -4,21 +4,26 @@ import type { CliArguments } from '../../types.js';
import { registerIpcCommand } from '../../registration/index.js'; import { registerIpcCommand } from '../../registration/index.js';
export function registerStopCommand(smartcli: plugins.smartcli.Smartcli) { export function registerStopCommand(smartcli: plugins.smartcli.Smartcli) {
registerIpcCommand(smartcli, 'stop', async (argvArg: CliArguments) => { registerIpcCommand(
const id = argvArg._[1]; smartcli,
if (!id) { 'stop',
console.error('Error: Please provide a process ID'); async (argvArg: CliArguments) => {
console.log('Usage: tspm stop <id>'); const id = argvArg._[1];
return; if (!id) {
} console.error('Error: Please provide a process ID');
console.log('Usage: tspm stop <id>');
return;
}
console.log(`Stopping process: ${id}`); console.log(`Stopping process: ${id}`);
const response = await tspmIpcClient.request('stop', { id }); const response = await tspmIpcClient.request('stop', { id });
if (response.success) { if (response.success) {
console.log(`${response.message}`); console.log(`${response.message}`);
} else { } else {
console.error(`✗ Failed to stop process: ${response.message}`); console.error(`✗ Failed to stop process: ${response.message}`);
} }
}, { actionLabel: 'stop process' }); },
} { actionLabel: 'stop process' },
);
}

View File

@@ -5,21 +5,24 @@ import type { CliArguments } from '../../types.js';
export function registerDisableCommand(smartcli: plugins.smartcli.Smartcli) { export function registerDisableCommand(smartcli: plugins.smartcli.Smartcli) {
const cliLogger = new Logger('CLI'); const cliLogger = new Logger('CLI');
smartcli.addCommand('disable').subscribe({ smartcli.addCommand('disable').subscribe({
next: async (argvArg: CliArguments) => { next: async (argvArg: CliArguments) => {
try { try {
const serviceManager = new TspmServiceManager(); const serviceManager = new TspmServiceManager();
console.log('Disabling TSPM daemon service...'); console.log('Disabling TSPM daemon service...');
await serviceManager.disableService(); await serviceManager.disableService();
console.log('✓ TSPM daemon service disabled'); console.log('✓ TSPM daemon service disabled');
console.log(' The daemon will no longer start on system boot'); console.log(' The daemon will no longer start on system boot');
console.log(' Use "tspm enable" to re-enable the service'); console.log(' Use "tspm enable" to re-enable the service');
} catch (error) { } catch (error) {
console.error('Error disabling service:', error.message); 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'); console.log('\nNote: You may need to run this command with sudo');
} }
process.exit(1); process.exit(1);
@@ -30,4 +33,4 @@ export function registerDisableCommand(smartcli: plugins.smartcli.Smartcli) {
}, },
complete: () => {}, complete: () => {},
}); });
} }

View File

@@ -5,21 +5,24 @@ import type { CliArguments } from '../../types.js';
export function registerEnableCommand(smartcli: plugins.smartcli.Smartcli) { export function registerEnableCommand(smartcli: plugins.smartcli.Smartcli) {
const cliLogger = new Logger('CLI'); const cliLogger = new Logger('CLI');
smartcli.addCommand('enable').subscribe({ smartcli.addCommand('enable').subscribe({
next: async (argvArg: CliArguments) => { next: async (argvArg: CliArguments) => {
try { try {
const serviceManager = new TspmServiceManager(); const serviceManager = new TspmServiceManager();
console.log('Enabling TSPM daemon as system service...'); console.log('Enabling TSPM daemon as system service...');
await serviceManager.enableService(); await serviceManager.enableService();
console.log('✓ TSPM daemon enabled and started as system service'); console.log('✓ TSPM daemon enabled and started as system service');
console.log(' The daemon will now start automatically on system boot'); console.log(' The daemon will now start automatically on system boot');
console.log(' Use "tspm disable" to remove the service'); console.log(' Use "tspm disable" to remove the service');
} catch (error) { } catch (error) {
console.error('Error enabling service:', error.message); 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'); console.log('\nNote: You may need to run this command with sudo');
} }
process.exit(1); process.exit(1);
@@ -30,4 +33,4 @@ export function registerEnableCommand(smartcli: plugins.smartcli.Smartcli) {
}, },
complete: () => {}, complete: () => {},
}); });
} }

View File

@@ -2,15 +2,23 @@ import type { CliArguments } from '../types.js';
// Argument parsing helpers // Argument parsing helpers
export const getBool = (argv: CliArguments, ...keys: string[]) => 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 v = (argv as any)[key];
const n = typeof v === 'string' ? Number(v) : v; const n = typeof v === 'string' ? Number(v) : v;
return Number.isFinite(n) ? n : fallback; 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]; const v = (argv as any)[key];
return typeof v === 'string' ? v : fallback; return typeof v === 'string' ? v : fallback;
}; };

View File

@@ -1,14 +1,18 @@
// Helper function to handle daemon connection errors // Helper function to handle daemon connection errors
export function handleDaemonError(error: any, action: string): void { export function handleDaemonError(error: any, action: string): void {
if (error.message?.includes('daemon is not running') || if (
error.message?.includes('Not connected') || error.message?.includes('daemon is not running') ||
error.message?.includes('ECONNREFUSED')) { error.message?.includes('Not connected') ||
error.message?.includes('ECONNREFUSED')
) {
console.error(`Error: Cannot ${action} - TSPM daemon is not running.`); console.error(`Error: Cannot ${action} - TSPM daemon is not running.`);
console.log('\nTo start the daemon, run one of:'); console.log('\nTo start the daemon, run one of:');
console.log(' tspm daemon start - Start for this session only'); 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 { } else {
console.error(`Error ${action}:`, error.message); console.error(`Error ${action}:`, error.message);
} }
process.exit(1); process.exit(1);
} }

View File

@@ -7,11 +7,12 @@ export function pad(str: string, length: number): string {
// Helper for unknown errors // Helper for unknown errors
export const unknownError = (err: any) => 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 // Helper function to format log entries
export function formatLog(log: any): string { export function formatLog(log: any): string {
const timestamp = new Date(log.timestamp).toLocaleTimeString(); 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}`; return `${timestamp} ${prefix} ${log.message}`;
} }

View File

@@ -19,4 +19,4 @@ export function withStreamingLifecycle(
await setup(); await setup();
await new Promise(() => {}); // keep alive await new Promise(() => {}); // keep alive
})(); })();
} }

View File

@@ -30,4 +30,4 @@ export function formatMemory(bytes: number): string {
} else { } else {
return `${bytes} B`; return `${bytes} B`;
} }
} }

View File

@@ -65,4 +65,4 @@ export const run = async (): Promise<void> => {
// Start parsing commands // Start parsing commands
smartcliInstance.startParse(); smartcliInstance.startParse();
}; };

View File

@@ -4,16 +4,23 @@ import { tspmIpcClient } from '../../classes.ipcclient.js';
* Preflight the daemon if required. Uses getDaemonStatus() which is safe and cheap: * Preflight the daemon if required. Uses getDaemonStatus() which is safe and cheap:
* it only connects if the PID file is valid. * 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 if (requireDaemon === false) return true; // command does not require daemon
const status = await tspmIpcClient.getDaemonStatus(); const status = await tspmIpcClient.getDaemonStatus();
if (!status) { if (!status) {
// Same hint as handleDaemonError, but early and consistent // 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('\nTo start the daemon, run one of:');
console.log(' tspm daemon start - Start for this session only'); 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 false;
} }
return true; return true;
} }

View File

@@ -1,5 +1,9 @@
import * as plugins from '../../plugins.js'; 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 { handleDaemonError } from '../helpers/errors.js';
import { unknownError } from '../helpers/formatting.js'; import { unknownError } from '../helpers/formatting.js';
import { runIpcCommand } from '../utils/ipc.js'; import { runIpcCommand } from '../utils/ipc.js';
@@ -15,7 +19,7 @@ export function registerIpcCommand(
smartcli: plugins.smartcli.Smartcli, smartcli: plugins.smartcli.Smartcli,
name: string, name: string,
action: CommandAction, action: CommandAction,
opts: IpcCommandOptions = {} opts: IpcCommandOptions = {},
) { ) {
const { actionLabel = name, keepAlive = false, requireDaemon = true } = opts; const { actionLabel = name, keepAlive = false, requireDaemon = true } = opts;
@@ -29,7 +33,8 @@ export function registerIpcCommand(
} }
// Evaluate keepAlive - can be boolean or function // 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) { if (shouldKeepAlive) {
// Let action manage its own connection/cleanup lifecycle // Let action manage its own connection/cleanup lifecycle
@@ -51,7 +56,10 @@ export function registerIpcCommand(
}, },
error: (err) => { error: (err) => {
// Fallback error path (should be rare with try/catch in next) // 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); process.exit(1);
}, },
complete: () => {}, complete: () => {},
@@ -66,7 +74,7 @@ export function registerLocalCommand(
smartcli: plugins.smartcli.Smartcli, smartcli: plugins.smartcli.Smartcli,
name: string, name: string,
action: (argv: CliArguments) => Promise<void>, action: (argv: CliArguments) => Promise<void>,
opts: { actionLabel?: string } = {} opts: { actionLabel?: string } = {},
) { ) {
const { actionLabel = name } = opts; const { actionLabel = name } = opts;
smartcli.addCommand(name).subscribe({ smartcli.addCommand(name).subscribe({
@@ -79,9 +87,12 @@ export function registerLocalCommand(
} }
}, },
error: (err) => { error: (err) => {
console.error(`Unexpected error in command "${name}":`, unknownError(err)); console.error(
`Unexpected error in command "${name}":`,
unknownError(err),
);
process.exit(1); process.exit(1);
}, },
complete: () => {}, complete: () => {},
}); });
} }

View File

@@ -14,7 +14,7 @@ export interface CliArguments {
export type CommandAction = (argv: CliArguments) => Promise<void>; export type CommandAction = (argv: CliArguments) => Promise<void>;
export interface IpcCommandOptions { export interface IpcCommandOptions {
actionLabel?: string; // used in error message, e.g. "start process" 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 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 requireDaemon?: boolean; // default true for IPC-bound commands
} }

View File

@@ -11,4 +11,4 @@ export async function runIpcCommand<T>(body: () => Promise<T>): Promise<T> {
// Ignore disconnect errors // Ignore disconnect errors
} }
} }
} }

View File

@@ -1,7 +1,4 @@
import type { import type { IProcessConfig, IProcessInfo } from './classes.tspm.js';
IProcessConfig,
IProcessInfo,
} from './classes.tspm.js';
import type { IProcessLog } from './classes.processwrapper.js'; import type { IProcessLog } from './classes.processwrapper.js';
// Base message types // Base message types