Compare commits
7 Commits
Author | SHA1 | Date | |
---|---|---|---|
529a403c4b | |||
ece16b75e2 | |||
1516185c4d | |||
1a782f0768 | |||
ae4148c82f | |||
6141b26530 | |||
e73f4acd63 |
37
changelog.md
37
changelog.md
@@ -1,6 +1,39 @@
|
||||
# Changelog
|
||||
|
||||
## 2025-08-28 - 3.1.0 - feat(daemon)
|
||||
Reorganize and refactor core into client/daemon/shared modules; add IPC protocol and tests
|
||||
|
||||
- Reorganized core code: split daemon and client logic into ts/daemon and ts/client directories
|
||||
- Moved process management into ProcessManager, ProcessMonitor and ProcessWrapper under ts/daemon
|
||||
- Added a dedicated IPC client and service manager under ts/client (tspm.ipcclient, tspm.servicemanager)
|
||||
- Introduced shared protocol and error handling: ts/shared/protocol/ipc.types.ts, protocol.version.ts and ts/shared/common/utils.errorhandler.ts
|
||||
- Updated CLI to import Logger from shared/common utils and updated related helpers
|
||||
- Added daemon entrypoint at ts/daemon/index.ts and reorganized daemon startup/shutdown/heartbeat handling
|
||||
- Added test assets (test/testassets/simple-test.ts, simple-script2.ts) and expanded test files under test/
|
||||
- Removed legacy top-level class files (classes.*) in favor of the new structured layout
|
||||
|
||||
## 2025-08-28 - 3.0.2 - fix(daemon)
|
||||
Ensure TSPM runtime dir exists and improve daemon startup/debug output
|
||||
|
||||
- Create ~/.tspm directory before starting the daemon to avoid missing-directory errors
|
||||
- Start daemon child process with stdio inherited when TSPM_DEBUG=true to surface startup errors during debugging
|
||||
- Add warning and troubleshooting guidance when daemon process starts but does not respond (suggest checking socket file and using TSPM_DEBUG)
|
||||
- Bump package version to 3.0.1
|
||||
|
||||
## 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)
|
||||
|
||||
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).
|
||||
@@ -12,6 +45,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.
|
||||
|
||||
## 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
|
||||
|
||||
- Upgrade @push.rocks/smartipc dependency to ^2.1.2
|
||||
@@ -24,6 +58,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)
|
||||
|
||||
## 2025-08-25 - 1.7.0 - feat(readme)
|
||||
|
||||
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
|
||||
@@ -32,6 +67,7 @@ Add comprehensive README with detailed usage, command reference, daemon manageme
|
||||
- Improved onboarding instructions: cloning, installing, testing, building, and running the project
|
||||
|
||||
## 2025-08-25 - 1.6.1 - fix(daemon)
|
||||
|
||||
Fix smartipc integration and add daemon/ipc integration tests
|
||||
|
||||
- Replace direct smartipc server/client construction with SmartIpc.createServer/createClient and set heartbeat: false
|
||||
@@ -40,6 +76,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
|
||||
|
||||
## 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 daemon implementation (ts/classes.daemon.ts) to manage all processes via a single background service and Unix socket.
|
||||
|
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@git.zone/tspm",
|
||||
"version": "2.0.1",
|
||||
"version": "3.1.0",
|
||||
"private": false,
|
||||
"description": "a no fuzz process manager",
|
||||
"main": "dist_ts/index.js",
|
||||
|
24
readme.md
24
readme.md
@@ -64,9 +64,11 @@ tspm restart my-server
|
||||
### Process Management
|
||||
|
||||
#### `tspm start <script> [options]`
|
||||
|
||||
Start a new process with automatic monitoring and management.
|
||||
|
||||
**Options:**
|
||||
|
||||
- `--name <name>` - Custom name for the process (default: script name)
|
||||
- `--memory <size>` - Memory limit (e.g., "512MB", "2GB", default: 512MB)
|
||||
- `--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)
|
||||
|
||||
**Examples:**
|
||||
|
||||
```bash
|
||||
# Simple start
|
||||
tspm start server.js
|
||||
@@ -90,6 +93,7 @@ tspm start ../other-project/index.js --cwd ../other-project --name other
|
||||
```
|
||||
|
||||
#### `tspm stop <id>`
|
||||
|
||||
Gracefully stop a running process (SIGTERM → SIGKILL after timeout).
|
||||
|
||||
```bash
|
||||
@@ -97,6 +101,7 @@ tspm stop my-server
|
||||
```
|
||||
|
||||
#### `tspm restart <id>`
|
||||
|
||||
Stop and restart a process with the same configuration.
|
||||
|
||||
```bash
|
||||
@@ -104,6 +109,7 @@ tspm restart my-server
|
||||
```
|
||||
|
||||
#### `tspm delete <id>`
|
||||
|
||||
Stop and remove a process from TSPM management.
|
||||
|
||||
```bash
|
||||
@@ -113,6 +119,7 @@ tspm delete old-server
|
||||
### Monitoring & Information
|
||||
|
||||
#### `tspm list`
|
||||
|
||||
Display all managed processes in a beautiful table.
|
||||
|
||||
```bash
|
||||
@@ -128,6 +135,7 @@ tspm list
|
||||
```
|
||||
|
||||
#### `tspm describe <id>`
|
||||
|
||||
Get detailed information about a specific process.
|
||||
|
||||
```bash
|
||||
@@ -153,9 +161,11 @@ Watch Paths: src, config
|
||||
```
|
||||
|
||||
#### `tspm logs <id> [options]`
|
||||
|
||||
View process logs (stdout and stderr).
|
||||
|
||||
**Options:**
|
||||
|
||||
- `--lines <n>` - Number of lines to display (default: 50)
|
||||
|
||||
```bash
|
||||
@@ -165,6 +175,7 @@ tspm logs my-server --lines 100
|
||||
### Batch Operations
|
||||
|
||||
#### `tspm start-all`
|
||||
|
||||
Start all saved processes at once.
|
||||
|
||||
```bash
|
||||
@@ -172,6 +183,7 @@ tspm start-all
|
||||
```
|
||||
|
||||
#### `tspm stop-all`
|
||||
|
||||
Stop all running processes.
|
||||
|
||||
```bash
|
||||
@@ -179,6 +191,7 @@ tspm stop-all
|
||||
```
|
||||
|
||||
#### `tspm restart-all`
|
||||
|
||||
Restart all running processes.
|
||||
|
||||
```bash
|
||||
@@ -188,6 +201,7 @@ tspm restart-all
|
||||
### Daemon Management
|
||||
|
||||
#### `tspm daemon start`
|
||||
|
||||
Start the TSPM daemon (happens automatically on first command).
|
||||
|
||||
```bash
|
||||
@@ -195,6 +209,7 @@ tspm daemon start
|
||||
```
|
||||
|
||||
#### `tspm daemon stop`
|
||||
|
||||
Stop the TSPM daemon and all managed processes.
|
||||
|
||||
```bash
|
||||
@@ -202,6 +217,7 @@ tspm daemon stop
|
||||
```
|
||||
|
||||
#### `tspm daemon status`
|
||||
|
||||
Check daemon health and statistics.
|
||||
|
||||
```bash
|
||||
@@ -245,7 +261,7 @@ const processId = await manager.start({
|
||||
projectDir: process.cwd(),
|
||||
memoryLimitBytes: 512 * 1024 * 1024, // 512MB
|
||||
autorestart: true,
|
||||
watch: false
|
||||
watch: false,
|
||||
});
|
||||
|
||||
// Monitor process
|
||||
@@ -259,18 +275,23 @@ await manager.stop(processId);
|
||||
## 🔧 Advanced Features
|
||||
|
||||
### 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.
|
||||
|
||||
### 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.
|
||||
|
||||
### Intelligent Logging
|
||||
|
||||
Logs are buffered and managed efficiently, preventing memory issues from excessive output while ensuring you don't lose important information.
|
||||
|
||||
### Graceful Shutdown
|
||||
|
||||
Processes receive SIGTERM first, allowing them to clean up. After a timeout, SIGKILL ensures termination.
|
||||
|
||||
### Configuration Persistence
|
||||
|
||||
Process configurations are saved, allowing you to restart all processes after a system reboot with a single command.
|
||||
|
||||
## 🛠️ Development
|
||||
@@ -304,6 +325,7 @@ tspm list
|
||||
## 📊 Performance
|
||||
|
||||
TSPM is designed to be lightweight and efficient:
|
||||
|
||||
- Minimal CPU overhead (typically < 0.5%)
|
||||
- Small memory footprint (~30-50MB for the daemon)
|
||||
- Fast process startup and shutdown
|
||||
|
279
readme.plan.md
279
readme.plan.md
@@ -1,48 +1,249 @@
|
||||
# TSPM SmartDaemon Service Management Refactor
|
||||
# TSPM Architecture Refactoring Plan
|
||||
|
||||
## 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.
|
||||
## Current Problems
|
||||
The current architecture has several issues that make the codebase confusing:
|
||||
|
||||
## Solution
|
||||
Refactor to use SmartDaemon for proper systemd service integration.
|
||||
1. **Flat structure confusion**: All classes are mixed together in the `ts/` directory with a `classes.` prefix naming convention
|
||||
2. **Unclear boundaries**: It's hard to tell what code runs in the daemon vs the client
|
||||
3. **Misleading naming**: The `Tspm` class is actually the core ProcessManager, not the overall system
|
||||
4. **Coupling risk**: Client code could accidentally import daemon internals, bloating bundles
|
||||
5. **No architectural enforcement**: Nothing prevents cross-boundary imports
|
||||
|
||||
## Implementation Tasks
|
||||
## Goal
|
||||
Refactor into a clean 3-folder architecture (daemon/client/shared) with proper separation of concerns and enforced boundaries.
|
||||
|
||||
### Phase 1: Remove Auto-Spawn Behavior
|
||||
- [x] Remove spawn import from ts/classes.ipcclient.ts
|
||||
- [x] Delete startDaemon() method from IpcClient
|
||||
- [x] Update connect() to throw error when daemon not running
|
||||
- [x] Remove auto-reconnect logic from request() method
|
||||
## Key Insights from Architecture Review
|
||||
|
||||
### Phase 2: Create Service Manager
|
||||
- [x] Create new file ts/classes.servicemanager.ts
|
||||
- [x] Implement TspmServiceManager class
|
||||
- [x] Add getOrCreateService() method
|
||||
- [x] Add enableService() method
|
||||
- [x] Add disableService() method
|
||||
- [x] Add getServiceStatus() method
|
||||
### Why This Separation Makes Sense
|
||||
After discussion with GPT-5, we identified that:
|
||||
|
||||
### Phase 3: Update CLI Commands
|
||||
- [x] Add 'enable' command to CLI
|
||||
- [x] Add 'disable' command to CLI
|
||||
- [x] Update 'daemon start' to work without systemd
|
||||
- [x] Add 'daemon start-service' internal command for systemd
|
||||
- [x] Update all commands to handle missing daemon gracefully
|
||||
- [x] Add proper error messages with hints
|
||||
1. **ProcessManager/Monitor/Wrapper are daemon-only**: These classes actually spawn and manage processes. Clients never need them - they only communicate via IPC.
|
||||
|
||||
### Phase 4: Update Documentation
|
||||
- [x] Update help text in CLI
|
||||
- [ ] Update command descriptions
|
||||
- [x] Add service management section
|
||||
2. **The client is just an IPC bridge**: The client (CLI and library users) only needs to send messages to the daemon and receive responses. It should never directly manage processes.
|
||||
|
||||
### Phase 5: Testing
|
||||
- [x] Test enable command
|
||||
- [x] Test disable command
|
||||
- [x] Test daemon commands
|
||||
- [x] Test error handling when daemon not running
|
||||
- [x] Build and verify TypeScript compilation
|
||||
3. **Shared should be minimal**: Only the IPC protocol types and pure utilities should be shared. No Node.js APIs, no file system access.
|
||||
|
||||
## Migration Notes
|
||||
- Users will need to run `tspm enable` once after update
|
||||
- Existing daemon instances will stop working
|
||||
- Documentation needs updating to explain new behavior
|
||||
4. **Protocol is the contract**: The IPC types are the only coupling between client and daemon. This allows independent evolution.
|
||||
|
||||
## Architecture Overview
|
||||
|
||||
### Folder Structure
|
||||
- **ts/daemon/** - Process orchestration (runs in daemon process only)
|
||||
- Contains all process management logic
|
||||
- Spawns and monitors actual system processes
|
||||
- Manages configuration and state
|
||||
- Never imported by client code
|
||||
|
||||
- **ts/client/** - IPC communication (runs in CLI/client process)
|
||||
- Only knows how to talk to the daemon via IPC
|
||||
- Lightweight - no process management logic
|
||||
- What library users import when they use TSPM
|
||||
- Can work in any Node.js environment (or potentially browser)
|
||||
|
||||
- **ts/shared/** - Minimal shared contract (protocol & pure utilities)
|
||||
- **protocol/** - IPC request/response types, error codes, version
|
||||
- **common/** - Pure utilities with no environment dependencies
|
||||
- No fs, net, child_process, or Node-specific APIs
|
||||
- Keep as small as possible to minimize coupling
|
||||
|
||||
## File Organization Rationale
|
||||
|
||||
### What Goes in Daemon
|
||||
These files are daemon-only because they actually manage processes:
|
||||
- `processmanager.ts` (was Tspm) - Core process orchestration logic
|
||||
- `processmonitor.ts` - Monitors memory and restarts processes
|
||||
- `processwrapper.ts` - Wraps child processes with logging
|
||||
- `tspm.config.ts` - Persists process configurations to disk
|
||||
- `tspm.daemon.ts` - Wires everything together, handles IPC requests
|
||||
|
||||
### What Goes in Client
|
||||
These files are client-only because they just communicate:
|
||||
- `tspm.ipcclient.ts` - Sends requests to daemon via Unix socket
|
||||
- `tspm.servicemanager.ts` - Manages systemd service (delegates to smartdaemon)
|
||||
- CLI files - Command-line interface that uses the IPC client
|
||||
|
||||
### What Goes in Shared
|
||||
Only the absolute minimum needed by both:
|
||||
- `protocol/ipc.types.ts` - Request/response type definitions
|
||||
- `protocol/error.codes.ts` - Standardized error codes
|
||||
- `common/utils.errorhandler.ts` - If it's pure (no I/O)
|
||||
- Parts of `paths.ts` - Constants like socket path (not OS-specific resolution)
|
||||
- Plugin interfaces only (not loading logic)
|
||||
|
||||
### Critical Design Decisions
|
||||
|
||||
1. **Rename Tspm to ProcessManager**: The class name should reflect what it does
|
||||
2. **No process management in shared**: ProcessManager, ProcessMonitor, ProcessWrapper are daemon-only
|
||||
3. **Protocol versioning**: Add version to allow client/daemon compatibility
|
||||
4. **Enforce boundaries**: Use TypeScript project references to prevent violations
|
||||
5. **Control exports**: Package.json exports map ensures library users can't import daemon code
|
||||
|
||||
## Detailed Task List
|
||||
|
||||
### Phase 1: Create New Structure
|
||||
- [ ] Create directory `ts/daemon/`
|
||||
- [ ] Create directory `ts/client/`
|
||||
- [ ] Create directory `ts/shared/`
|
||||
- [ ] Create directory `ts/shared/protocol/`
|
||||
- [ ] Create directory `ts/shared/common/`
|
||||
|
||||
### Phase 2: Move Daemon Files
|
||||
- [ ] Move `ts/daemon.ts` → `ts/daemon/index.ts`
|
||||
- [ ] Move `ts/classes.daemon.ts` → `ts/daemon/tspm.daemon.ts`
|
||||
- [ ] Move `ts/classes.tspm.ts` → `ts/daemon/processmanager.ts`
|
||||
- [ ] Move `ts/classes.processmonitor.ts` → `ts/daemon/processmonitor.ts`
|
||||
- [ ] Move `ts/classes.processwrapper.ts` → `ts/daemon/processwrapper.ts`
|
||||
- [ ] Move `ts/classes.config.ts` → `ts/daemon/tspm.config.ts`
|
||||
|
||||
### Phase 3: Move Client Files
|
||||
- [ ] Move `ts/classes.ipcclient.ts` → `ts/client/tspm.ipcclient.ts`
|
||||
- [ ] Move `ts/classes.servicemanager.ts` → `ts/client/tspm.servicemanager.ts`
|
||||
- [ ] Create `ts/client/index.ts` barrel export file
|
||||
|
||||
### Phase 4: Move Shared Files
|
||||
- [ ] Move `ts/ipc.types.ts` → `ts/shared/protocol/ipc.types.ts`
|
||||
- [ ] Create `ts/shared/protocol/protocol.version.ts` with version constant
|
||||
- [ ] Create `ts/shared/protocol/error.codes.ts` with standardized error codes
|
||||
- [ ] Move `ts/utils.errorhandler.ts` → `ts/shared/common/utils.errorhandler.ts`
|
||||
- [ ] Analyze `ts/paths.ts` - split into constants (shared) vs resolvers (daemon)
|
||||
- [ ] Move/split `ts/plugins.ts` - interfaces to shared, loaders to daemon
|
||||
|
||||
### Phase 5: Rename Classes
|
||||
- [ ] In `processmanager.ts`: Rename class `Tspm` → `ProcessManager`
|
||||
- [ ] Update all references to `Tspm` class to use `ProcessManager`
|
||||
- [ ] Update constructor in `tspm.daemon.ts` to use `ProcessManager`
|
||||
|
||||
### Phase 6: Update Imports - Daemon Files
|
||||
- [ ] Update imports in `ts/daemon/index.ts`
|
||||
- [ ] Update imports in `ts/daemon/tspm.daemon.ts`
|
||||
- [ ] Change `'./classes.tspm.js'` → `'./processmanager.js'`
|
||||
- [ ] Change `'./paths.js'` → appropriate shared/daemon path
|
||||
- [ ] Change `'./ipc.types.js'` → `'../shared/protocol/ipc.types.js'`
|
||||
- [ ] Update imports in `ts/daemon/processmanager.ts`
|
||||
- [ ] Change `'./classes.processmonitor.js'` → `'./processmonitor.js'`
|
||||
- [ ] Change `'./classes.processwrapper.js'` → `'./processwrapper.js'`
|
||||
- [ ] Change `'./classes.config.js'` → `'./tspm.config.js'`
|
||||
- [ ] Change `'./utils.errorhandler.js'` → `'../shared/common/utils.errorhandler.js'`
|
||||
- [ ] Update imports in `ts/daemon/processmonitor.ts`
|
||||
- [ ] Change `'./classes.processwrapper.js'` → `'./processwrapper.js'`
|
||||
- [ ] Update imports in `ts/daemon/processwrapper.ts`
|
||||
- [ ] Update imports in `ts/daemon/tspm.config.ts`
|
||||
|
||||
### Phase 7: Update Imports - Client Files
|
||||
- [ ] Update imports in `ts/client/tspm.ipcclient.ts`
|
||||
- [ ] Change `'./paths.js'` → appropriate shared/daemon path
|
||||
- [ ] Change `'./ipc.types.js'` → `'../shared/protocol/ipc.types.js'`
|
||||
- [ ] Update imports in `ts/client/tspm.servicemanager.ts`
|
||||
- [ ] Change `'./paths.js'` → appropriate shared/daemon path
|
||||
- [ ] Create exports in `ts/client/index.ts`
|
||||
- [ ] Export TspmIpcClient
|
||||
- [ ] Export TspmServiceManager
|
||||
|
||||
### Phase 8: Update Imports - CLI Files
|
||||
- [ ] Update imports in `ts/cli/index.ts`
|
||||
- [ ] Change `'../classes.ipcclient.js'` → `'../client/tspm.ipcclient.js'`
|
||||
- [ ] Update imports in `ts/cli/commands/service/enable.ts`
|
||||
- [ ] Change `'../../../classes.servicemanager.js'` → `'../../../client/tspm.servicemanager.js'`
|
||||
- [ ] Update imports in `ts/cli/commands/service/disable.ts`
|
||||
- [ ] Change `'../../../classes.servicemanager.js'` → `'../../../client/tspm.servicemanager.js'`
|
||||
- [ ] Update imports in `ts/cli/commands/daemon/index.ts`
|
||||
- [ ] Change `'../../../classes.daemon.js'` → `'../../../daemon/tspm.daemon.js'`
|
||||
- [ ] Change `'../../../classes.ipcclient.js'` → `'../../../client/tspm.ipcclient.js'`
|
||||
- [ ] Update imports in `ts/cli/commands/process/*.ts` files
|
||||
- [ ] Change all `'../../../classes.ipcclient.js'` → `'../../../client/tspm.ipcclient.js'`
|
||||
- [ ] Change all `'../../../classes.tspm.js'` → `'../../../shared/protocol/ipc.types.js'` (for types)
|
||||
- [ ] Update imports in `ts/cli/registration/index.ts`
|
||||
- [ ] Change `'../../classes.ipcclient.js'` → `'../../client/tspm.ipcclient.js'`
|
||||
|
||||
### Phase 9: Update Main Exports
|
||||
- [ ] Update `ts/index.ts`
|
||||
- [ ] Remove `export * from './classes.tspm.js'`
|
||||
- [ ] Remove `export * from './classes.processmonitor.js'`
|
||||
- [ ] Remove `export * from './classes.processwrapper.js'`
|
||||
- [ ] Remove `export * from './classes.daemon.js'`
|
||||
- [ ] Remove `export * from './classes.ipcclient.js'`
|
||||
- [ ] Remove `export * from './classes.servicemanager.js'`
|
||||
- [ ] Add `export * from './client/index.js'`
|
||||
- [ ] Add `export * from './shared/protocol/ipc.types.js'`
|
||||
- [ ] Add `export { startDaemon } from './daemon/index.js'`
|
||||
|
||||
### Phase 10: Update Package.json
|
||||
- [ ] Add exports map to package.json:
|
||||
```json
|
||||
"exports": {
|
||||
".": "./dist_ts/client/index.js",
|
||||
"./client": "./dist_ts/client/index.js",
|
||||
"./daemon": "./dist_ts/daemon/index.js",
|
||||
"./protocol": "./dist_ts/shared/protocol/ipc.types.js"
|
||||
}
|
||||
```
|
||||
|
||||
### Phase 11: TypeScript Configuration
|
||||
- [ ] Create `tsconfig.base.json` with common settings
|
||||
- [ ] Create `tsconfig.shared.json` for shared code
|
||||
- [ ] Create `tsconfig.client.json` with reference to shared
|
||||
- [ ] Create `tsconfig.daemon.json` with reference to shared
|
||||
- [ ] Update main `tsconfig.json` to use references
|
||||
|
||||
### Phase 12: Testing
|
||||
- [ ] Run `pnpm run build` and fix any compilation errors
|
||||
- [ ] Test daemon startup: `./cli.js daemon start`
|
||||
- [ ] Test process management: `./cli.js start "echo test"`
|
||||
- [ ] Test client commands: `./cli.js list`
|
||||
- [ ] Run existing tests: `pnpm test`
|
||||
- [ ] Update test imports if needed
|
||||
|
||||
### Phase 13: Documentation
|
||||
- [ ] Update README.md if needed
|
||||
- [ ] Document the new architecture in a comment at top of ts/index.ts
|
||||
- [ ] Add comments explaining the separation in each index.ts file
|
||||
|
||||
### Phase 14: Cleanup
|
||||
- [ ] Delete empty directories from old structure
|
||||
- [ ] Verify no broken imports remain
|
||||
- [ ] Run linter and fix any issues
|
||||
- [ ] Commit with message: "refactor(architecture): reorganize into daemon/client/shared structure"
|
||||
|
||||
## Benefits After Completion
|
||||
|
||||
### Immediate Benefits
|
||||
- **Clear separation**: Instantly obvious what runs where (daemon vs client)
|
||||
- **Smaller client bundles**: Client code won't accidentally include ProcessMonitor, ProcessWrapper, etc.
|
||||
- **Better testing**: Can test client and daemon independently
|
||||
- **Cleaner imports**: No more confusing `classes.` prefix on everything
|
||||
|
||||
### Architecture Benefits
|
||||
- **Enforced boundaries**: TypeScript project references prevent cross-imports
|
||||
- **Protocol as contract**: Client and daemon can evolve independently
|
||||
- **Version compatibility**: Protocol versioning allows client/daemon version skew
|
||||
- **Security**: Internal daemon errors don't leak to clients over IPC
|
||||
|
||||
### Future Benefits
|
||||
- **Browser support**: Clean client could potentially work in browser
|
||||
- **Embedded mode**: Could add option to run ProcessManager in-process
|
||||
- **Plugin system**: Clear boundary for plugin interfaces vs implementation
|
||||
- **Multi-language clients**: Other languages only need to implement IPC protocol
|
||||
|
||||
## Implementation Safeguards (from GPT-5 Review)
|
||||
|
||||
### Boundary Enforcement
|
||||
- **TypeScript project references**: Separate tsconfig files prevent illegal imports
|
||||
- **ESLint rules**: Use `import/no-restricted-paths` to catch violations
|
||||
- **Package.json exports**: Control what external consumers can import
|
||||
|
||||
### Keep Shared Minimal
|
||||
- **No Node.js APIs**: No fs, net, child_process in shared
|
||||
- **No environment access**: No process.env, no OS-specific code
|
||||
- **Pure functions only**: Shared utilities must be environment-agnostic
|
||||
- **Protocol-focused**: Mainly type definitions and constants
|
||||
|
||||
### Prevent Environment Bleed
|
||||
- **Split paths.ts**: Constants (shared) vs OS-specific resolution (daemon)
|
||||
- **Plugin interfaces only**: Loading/discovery stays in daemon
|
||||
- **No dynamic imports**: Keep shared statically analyzable
|
||||
|
||||
### Future-Proofing
|
||||
- **Protocol versioning**: Add version field for compatibility
|
||||
- **Error codes**: Standardized errors instead of string messages
|
||||
- **Capability negotiation**: Client can query daemon capabilities
|
||||
- **Subpath exports**: Different entry points for different use cases
|
@@ -97,7 +97,7 @@ tap.test('Daemon uptime calculation', async () => {
|
||||
const startTime = Date.now();
|
||||
|
||||
// Wait a bit
|
||||
await new Promise(resolve => setTimeout(resolve, 100));
|
||||
await new Promise((resolve) => setTimeout(resolve, 100));
|
||||
|
||||
const uptime = Date.now() - startTime;
|
||||
expect(uptime).toBeGreaterThanOrEqual(100);
|
||||
|
@@ -10,7 +10,7 @@ import { tspmIpcClient } from '../ts/classes.ipcclient.js';
|
||||
async function ensureDaemonStopped() {
|
||||
try {
|
||||
await tspmIpcClient.stopDaemon(false);
|
||||
await new Promise(resolve => setTimeout(resolve, 1000));
|
||||
await new Promise((resolve) => setTimeout(resolve, 1000));
|
||||
} catch (error) {
|
||||
// Ignore errors if daemon is not running
|
||||
}
|
||||
@@ -43,7 +43,7 @@ tap.test('Full daemon lifecycle test', async (tools) => {
|
||||
await tspmIpcClient.connect();
|
||||
|
||||
// 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
|
||||
status = await tspmIpcClient.getDaemonStatus();
|
||||
@@ -57,7 +57,7 @@ tap.test('Full daemon lifecycle test', async (tools) => {
|
||||
await tspmIpcClient.stopDaemon(true);
|
||||
|
||||
// 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
|
||||
status = await tspmIpcClient.getDaemonStatus();
|
||||
@@ -71,7 +71,7 @@ tap.test('Process management through daemon', async (tools) => {
|
||||
|
||||
// Ensure daemon is running
|
||||
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)
|
||||
let listResponse = await tspmIpcClient.request('list', {});
|
||||
@@ -88,7 +88,9 @@ tap.test('Process management through daemon', async (tools) => {
|
||||
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.status).toBeDefined();
|
||||
|
||||
@@ -96,12 +98,14 @@ tap.test('Process management through daemon', async (tools) => {
|
||||
listResponse = await tspmIpcClient.request('list', {});
|
||||
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?.id).toEqual('test-echo');
|
||||
|
||||
// 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.config).toBeDefined();
|
||||
expect(describeResponse.config.id).toEqual('test-echo');
|
||||
@@ -112,12 +116,16 @@ tap.test('Process management through daemon', async (tools) => {
|
||||
expect(stopResponse.message).toInclude('stopped successfully');
|
||||
|
||||
// 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);
|
||||
|
||||
// Test 7: Verify process is gone
|
||||
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();
|
||||
|
||||
// Cleanup: stop daemon
|
||||
@@ -131,7 +139,7 @@ tap.test('Batch operations through daemon', async (tools) => {
|
||||
|
||||
// Ensure daemon is running
|
||||
await tspmIpcClient.connect();
|
||||
await new Promise(resolve => setTimeout(resolve, 1000));
|
||||
await new Promise((resolve) => setTimeout(resolve, 1000));
|
||||
|
||||
// Add multiple test processes
|
||||
const testConfigs: tspm.IProcessConfig[] = [
|
||||
@@ -187,7 +195,7 @@ tap.test('Daemon error handling', async (tools) => {
|
||||
|
||||
// Ensure daemon is running
|
||||
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
|
||||
try {
|
||||
@@ -224,7 +232,7 @@ tap.test('Daemon heartbeat functionality', async (tools) => {
|
||||
|
||||
// Ensure daemon is running
|
||||
await tspmIpcClient.connect();
|
||||
await new Promise(resolve => setTimeout(resolve, 1000));
|
||||
await new Promise((resolve) => setTimeout(resolve, 1000));
|
||||
|
||||
// Test heartbeat
|
||||
const heartbeatResponse = await tspmIpcClient.request('heartbeat', {});
|
||||
@@ -242,7 +250,7 @@ tap.test('Daemon memory and CPU reporting', async (tools) => {
|
||||
|
||||
// Ensure daemon is running
|
||||
await tspmIpcClient.connect();
|
||||
await new Promise(resolve => setTimeout(resolve, 1000));
|
||||
await new Promise((resolve) => setTimeout(resolve, 1000));
|
||||
|
||||
// Get daemon status
|
||||
const status = await tspmIpcClient.getDaemonStatus();
|
||||
|
@@ -61,7 +61,10 @@ tap.test('IPC client daemon running check - stale PID', async () => {
|
||||
expect(isRunning).toEqual(false);
|
||||
|
||||
// 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);
|
||||
});
|
||||
|
||||
@@ -95,7 +98,9 @@ tap.test('IPC client singleton instance', async () => {
|
||||
expect(tspmIpcClient).toBeInstanceOf(TspmIpcClient);
|
||||
|
||||
// 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);
|
||||
});
|
||||
|
||||
@@ -111,7 +116,8 @@ tap.test('IPC client request method type safety', 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');
|
||||
});
|
||||
|
||||
|
@@ -3,6 +3,6 @@
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@git.zone/tspm',
|
||||
version: '2.0.0',
|
||||
version: '3.1.0',
|
||||
description: 'a no fuzz process manager'
|
||||
}
|
||||
|
@@ -4,23 +4,28 @@ import type { CliArguments } from '../../types.js';
|
||||
import { registerIpcCommand } from '../../registration/index.js';
|
||||
|
||||
export function registerRestartAllCommand(smartcli: plugins.smartcli.Smartcli) {
|
||||
registerIpcCommand(smartcli, 'restart-all', async (_argvArg: CliArguments) => {
|
||||
console.log('Restarting all processes...');
|
||||
const response = await tspmIpcClient.request('restartAll', {});
|
||||
registerIpcCommand(
|
||||
smartcli,
|
||||
'restart-all',
|
||||
async (_argvArg: CliArguments) => {
|
||||
console.log('Restarting all processes...');
|
||||
const response = await tspmIpcClient.request('restartAll', {});
|
||||
|
||||
if (response.restarted.length > 0) {
|
||||
console.log(`✓ Restarted ${response.restarted.length} processes:`);
|
||||
for (const id of response.restarted) {
|
||||
console.log(` - ${id}`);
|
||||
if (response.restarted.length > 0) {
|
||||
console.log(`✓ Restarted ${response.restarted.length} processes:`);
|
||||
for (const id of response.restarted) {
|
||||
console.log(` - ${id}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (response.failed.length > 0) {
|
||||
console.log(`✗ Failed to restart ${response.failed.length} processes:`);
|
||||
for (const failure of response.failed) {
|
||||
console.log(` - ${failure.id}: ${failure.error}`);
|
||||
if (response.failed.length > 0) {
|
||||
console.log(`✗ Failed to restart ${response.failed.length} processes:`);
|
||||
for (const failure of response.failed) {
|
||||
console.log(` - ${failure.id}: ${failure.error}`);
|
||||
}
|
||||
process.exitCode = 1; // Signal partial failure
|
||||
}
|
||||
process.exitCode = 1; // Signal partial failure
|
||||
}
|
||||
}, { actionLabel: 'restart all processes' });
|
||||
},
|
||||
{ actionLabel: 'restart all processes' },
|
||||
);
|
||||
}
|
@@ -4,23 +4,28 @@ import type { CliArguments } from '../../types.js';
|
||||
import { registerIpcCommand } from '../../registration/index.js';
|
||||
|
||||
export function registerStartAllCommand(smartcli: plugins.smartcli.Smartcli) {
|
||||
registerIpcCommand(smartcli, 'start-all', async (_argvArg: CliArguments) => {
|
||||
console.log('Starting all processes...');
|
||||
const response = await tspmIpcClient.request('startAll', {});
|
||||
registerIpcCommand(
|
||||
smartcli,
|
||||
'start-all',
|
||||
async (_argvArg: CliArguments) => {
|
||||
console.log('Starting all processes...');
|
||||
const response = await tspmIpcClient.request('startAll', {});
|
||||
|
||||
if (response.started.length > 0) {
|
||||
console.log(`✓ Started ${response.started.length} processes:`);
|
||||
for (const id of response.started) {
|
||||
console.log(` - ${id}`);
|
||||
if (response.started.length > 0) {
|
||||
console.log(`✓ Started ${response.started.length} processes:`);
|
||||
for (const id of response.started) {
|
||||
console.log(` - ${id}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (response.failed.length > 0) {
|
||||
console.log(`✗ Failed to start ${response.failed.length} processes:`);
|
||||
for (const failure of response.failed) {
|
||||
console.log(` - ${failure.id}: ${failure.error}`);
|
||||
if (response.failed.length > 0) {
|
||||
console.log(`✗ Failed to start ${response.failed.length} processes:`);
|
||||
for (const failure of response.failed) {
|
||||
console.log(` - ${failure.id}: ${failure.error}`);
|
||||
}
|
||||
process.exitCode = 1; // Signal partial failure
|
||||
}
|
||||
process.exitCode = 1; // Signal partial failure
|
||||
}
|
||||
}, { actionLabel: 'start all processes' });
|
||||
},
|
||||
{ actionLabel: 'start all processes' },
|
||||
);
|
||||
}
|
@@ -4,23 +4,28 @@ import type { CliArguments } from '../../types.js';
|
||||
import { registerIpcCommand } from '../../registration/index.js';
|
||||
|
||||
export function registerStopAllCommand(smartcli: plugins.smartcli.Smartcli) {
|
||||
registerIpcCommand(smartcli, 'stop-all', async (_argvArg: CliArguments) => {
|
||||
console.log('Stopping all processes...');
|
||||
const response = await tspmIpcClient.request('stopAll', {});
|
||||
registerIpcCommand(
|
||||
smartcli,
|
||||
'stop-all',
|
||||
async (_argvArg: CliArguments) => {
|
||||
console.log('Stopping all processes...');
|
||||
const response = await tspmIpcClient.request('stopAll', {});
|
||||
|
||||
if (response.stopped.length > 0) {
|
||||
console.log(`✓ Stopped ${response.stopped.length} processes:`);
|
||||
for (const id of response.stopped) {
|
||||
console.log(` - ${id}`);
|
||||
if (response.stopped.length > 0) {
|
||||
console.log(`✓ Stopped ${response.stopped.length} processes:`);
|
||||
for (const id of response.stopped) {
|
||||
console.log(` - ${id}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (response.failed.length > 0) {
|
||||
console.log(`✗ Failed to stop ${response.failed.length} processes:`);
|
||||
for (const failure of response.failed) {
|
||||
console.log(` - ${failure.id}: ${failure.error}`);
|
||||
if (response.failed.length > 0) {
|
||||
console.log(`✗ Failed to stop ${response.failed.length} processes:`);
|
||||
for (const failure of response.failed) {
|
||||
console.log(` - ${failure.id}: ${failure.error}`);
|
||||
}
|
||||
process.exitCode = 1; // Signal partial failure
|
||||
}
|
||||
process.exitCode = 1; // Signal partial failure
|
||||
}
|
||||
}, { actionLabel: 'stop all processes' });
|
||||
},
|
||||
{ actionLabel: 'stop all processes' },
|
||||
);
|
||||
}
|
@@ -37,9 +37,10 @@ export function registerDaemonCommand(smartcli: plugins.smartcli.Smartcli) {
|
||||
);
|
||||
|
||||
// Start daemon as a detached background process
|
||||
// Use 'inherit' for stdio to see any startup errors when debugging
|
||||
const daemonProcess = spawn(process.execPath, [daemonScript], {
|
||||
detached: true,
|
||||
stdio: 'ignore',
|
||||
stdio: process.env.TSPM_DEBUG === 'true' ? 'inherit' : 'ignore',
|
||||
env: {
|
||||
...process.env,
|
||||
TSPM_DAEMON_MODE: 'true',
|
||||
@@ -52,14 +53,23 @@ export function registerDaemonCommand(smartcli: plugins.smartcli.Smartcli) {
|
||||
console.log(`Started daemon with PID: ${daemonProcess.pid}`);
|
||||
|
||||
// Wait for daemon to be ready
|
||||
await new Promise(resolve => setTimeout(resolve, 2000));
|
||||
await new Promise((resolve) => setTimeout(resolve, 2000));
|
||||
|
||||
const newStatus = await tspmIpcClient.getDaemonStatus();
|
||||
if (newStatus) {
|
||||
console.log('✓ TSPM daemon started successfully');
|
||||
console.log(` PID: ${newStatus.pid}`);
|
||||
console.log('\nNote: This daemon will run until you stop it or logout.');
|
||||
console.log(
|
||||
'\nNote: This daemon will run until you stop it or logout.',
|
||||
);
|
||||
console.log('For automatic startup, use "tspm enable" instead.');
|
||||
} else {
|
||||
console.warn('\n⚠️ Warning: Daemon process started but is not responding.');
|
||||
console.log('The daemon may have crashed on startup.');
|
||||
console.log('\nTo debug, try:');
|
||||
console.log(' TSPM_DEBUG=true tspm daemon start');
|
||||
console.log('\nOr check if the socket file exists:');
|
||||
console.log(` ls -la ~/.tspm/tspm.sock`);
|
||||
}
|
||||
|
||||
// Disconnect from the daemon after starting
|
||||
|
@@ -17,7 +17,9 @@ export function registerDefaultCommand(smartcli: plugins.smartcli.Smartcli) {
|
||||
);
|
||||
console.log('Usage: tspm [command] [options]');
|
||||
console.log('\nService Management:');
|
||||
console.log(' enable Enable TSPM as system service (systemd)');
|
||||
console.log(
|
||||
' enable Enable TSPM as system service (systemd)',
|
||||
);
|
||||
console.log(' disable Disable TSPM system service');
|
||||
console.log('\nProcess Commands:');
|
||||
console.log(' start <script> Start a process');
|
||||
@@ -31,7 +33,9 @@ export function registerDefaultCommand(smartcli: plugins.smartcli.Smartcli) {
|
||||
console.log(' stop-all Stop all processes');
|
||||
console.log(' restart-all Restart all processes');
|
||||
console.log('\nDaemon Commands:');
|
||||
console.log(' daemon start Start daemon manually (current session)');
|
||||
console.log(
|
||||
' daemon start Start daemon manually (current session)',
|
||||
);
|
||||
console.log(' daemon stop Stop the daemon');
|
||||
console.log(' daemon status Show daemon status');
|
||||
console.log(
|
||||
@@ -85,7 +89,9 @@ export function registerDefaultCommand(smartcli: plugins.smartcli.Smartcli) {
|
||||
console.error('Error: TSPM daemon is not running.');
|
||||
console.log('\nTo start the daemon, run one of:');
|
||||
console.log(' tspm daemon start - Start for this session only');
|
||||
console.log(' tspm enable - Enable as system service (recommended)');
|
||||
console.log(
|
||||
' tspm enable - Enable as system service (recommended)',
|
||||
);
|
||||
}
|
||||
},
|
||||
error: (err) => {
|
||||
|
@@ -1,24 +1,29 @@
|
||||
import * as plugins from '../../../plugins.js';
|
||||
import { tspmIpcClient } from '../../../classes.ipcclient.js';
|
||||
import { tspmIpcClient } from '../../../client/tspm.ipcclient.js';
|
||||
import type { CliArguments } from '../../types.js';
|
||||
import { registerIpcCommand } from '../../registration/index.js';
|
||||
|
||||
export function registerDeleteCommand(smartcli: plugins.smartcli.Smartcli) {
|
||||
registerIpcCommand(smartcli, 'delete', async (argvArg: CliArguments) => {
|
||||
const id = argvArg._[1];
|
||||
if (!id) {
|
||||
console.error('Error: Please provide a process ID');
|
||||
console.log('Usage: tspm delete <id>');
|
||||
return;
|
||||
}
|
||||
registerIpcCommand(
|
||||
smartcli,
|
||||
'delete',
|
||||
async (argvArg: CliArguments) => {
|
||||
const id = argvArg._[1];
|
||||
if (!id) {
|
||||
console.error('Error: Please provide a process ID');
|
||||
console.log('Usage: tspm delete <id>');
|
||||
return;
|
||||
}
|
||||
|
||||
console.log(`Deleting process: ${id}`);
|
||||
const response = await tspmIpcClient.request('delete', { id });
|
||||
console.log(`Deleting process: ${id}`);
|
||||
const response = await tspmIpcClient.request('delete', { id });
|
||||
|
||||
if (response.success) {
|
||||
console.log(`✓ ${response.message}`);
|
||||
} else {
|
||||
console.error(`✗ Failed to delete process: ${response.message}`);
|
||||
}
|
||||
}, { actionLabel: 'delete process' });
|
||||
if (response.success) {
|
||||
console.log(`✓ ${response.message}`);
|
||||
} else {
|
||||
console.error(`✗ Failed to delete process: ${response.message}`);
|
||||
}
|
||||
},
|
||||
{ actionLabel: 'delete process' },
|
||||
);
|
||||
}
|
@@ -5,34 +5,45 @@ import { registerIpcCommand } from '../../registration/index.js';
|
||||
import { formatMemory } from '../../helpers/memory.js';
|
||||
|
||||
export function registerDescribeCommand(smartcli: plugins.smartcli.Smartcli) {
|
||||
registerIpcCommand(smartcli, 'describe', async (argvArg: CliArguments) => {
|
||||
const id = argvArg._[1];
|
||||
if (!id) {
|
||||
console.error('Error: Please provide a process ID');
|
||||
console.log('Usage: tspm describe <id>');
|
||||
return;
|
||||
}
|
||||
|
||||
const response = await tspmIpcClient.request('describe', { id });
|
||||
|
||||
console.log(`Process Details: ${id}`);
|
||||
console.log('─'.repeat(40));
|
||||
console.log(`Status: ${response.processInfo.status}`);
|
||||
console.log(`PID: ${response.processInfo.pid || 'N/A'}`);
|
||||
console.log(`Memory: ${formatMemory(response.processInfo.memory)}`);
|
||||
console.log(`CPU: ${response.processInfo.cpu ? response.processInfo.cpu.toFixed(1) + '%' : 'N/A'}`);
|
||||
console.log(`Uptime: ${response.processInfo.uptime ? Math.floor(response.processInfo.uptime / 1000) + 's' : 'N/A'}`);
|
||||
console.log(`Restarts: ${response.processInfo.restarts}`);
|
||||
console.log('\nConfiguration:');
|
||||
console.log(`Command: ${response.config.command}`);
|
||||
console.log(`Directory: ${response.config.projectDir}`);
|
||||
console.log(`Memory Limit: ${formatMemory(response.config.memoryLimitBytes)}`);
|
||||
console.log(`Auto-restart: ${response.config.autorestart}`);
|
||||
if (response.config.watch) {
|
||||
console.log(`Watch: enabled`);
|
||||
if (response.config.watchPaths) {
|
||||
console.log(`Watch Paths: ${response.config.watchPaths.join(', ')}`);
|
||||
registerIpcCommand(
|
||||
smartcli,
|
||||
'describe',
|
||||
async (argvArg: CliArguments) => {
|
||||
const id = argvArg._[1];
|
||||
if (!id) {
|
||||
console.error('Error: Please provide a process ID');
|
||||
console.log('Usage: tspm describe <id>');
|
||||
return;
|
||||
}
|
||||
}
|
||||
}, { actionLabel: 'describe process' });
|
||||
|
||||
const response = await tspmIpcClient.request('describe', { id });
|
||||
|
||||
console.log(`Process Details: ${id}`);
|
||||
console.log('─'.repeat(40));
|
||||
console.log(`Status: ${response.processInfo.status}`);
|
||||
console.log(`PID: ${response.processInfo.pid || 'N/A'}`);
|
||||
console.log(`Memory: ${formatMemory(response.processInfo.memory)}`);
|
||||
console.log(
|
||||
`CPU: ${response.processInfo.cpu ? response.processInfo.cpu.toFixed(1) + '%' : 'N/A'}`,
|
||||
);
|
||||
console.log(
|
||||
`Uptime: ${response.processInfo.uptime ? Math.floor(response.processInfo.uptime / 1000) + 's' : 'N/A'}`,
|
||||
);
|
||||
console.log(`Restarts: ${response.processInfo.restarts}`);
|
||||
console.log('\nConfiguration:');
|
||||
console.log(`Command: ${response.config.command}`);
|
||||
console.log(`Directory: ${response.config.projectDir}`);
|
||||
console.log(
|
||||
`Memory Limit: ${formatMemory(response.config.memoryLimitBytes)}`,
|
||||
);
|
||||
console.log(`Auto-restart: ${response.config.autorestart}`);
|
||||
if (response.config.watch) {
|
||||
console.log(`Watch: enabled`);
|
||||
if (response.config.watchPaths) {
|
||||
console.log(`Watch Paths: ${response.config.watchPaths.join(', ')}`);
|
||||
}
|
||||
}
|
||||
},
|
||||
{ actionLabel: 'describe process' },
|
||||
);
|
||||
}
|
@@ -1,37 +1,52 @@
|
||||
import * as plugins from '../../../plugins.js';
|
||||
import { tspmIpcClient } from '../../../classes.ipcclient.js';
|
||||
import { tspmIpcClient } from '../../../client/tspm.ipcclient.js';
|
||||
import type { CliArguments } from '../../types.js';
|
||||
import { registerIpcCommand } from '../../registration/index.js';
|
||||
import { pad } from '../../helpers/formatting.js';
|
||||
import { formatMemory } from '../../helpers/memory.js';
|
||||
|
||||
export function registerListCommand(smartcli: plugins.smartcli.Smartcli) {
|
||||
registerIpcCommand(smartcli, 'list', async (_argvArg: CliArguments) => {
|
||||
const response = await tspmIpcClient.request('list', {});
|
||||
const processes = response.processes;
|
||||
registerIpcCommand(
|
||||
smartcli,
|
||||
'list',
|
||||
async (_argvArg: CliArguments) => {
|
||||
const response = await tspmIpcClient.request('list', {});
|
||||
const processes = response.processes;
|
||||
|
||||
if (processes.length === 0) {
|
||||
console.log('No processes running.');
|
||||
return;
|
||||
}
|
||||
if (processes.length === 0) {
|
||||
console.log('No processes running.');
|
||||
return;
|
||||
}
|
||||
|
||||
console.log('Process List:');
|
||||
console.log('┌─────────┬─────────────┬───────────┬───────────┬──────────┬──────────┐');
|
||||
console.log('│ ID │ Name │ Status │ PID │ Memory │ Restarts │');
|
||||
console.log('├─────────┼─────────────┼───────────┼───────────┼──────────┼──────────┤');
|
||||
console.log('Process List:');
|
||||
console.log(
|
||||
'┌─────────┬─────────────┬───────────┬───────────┬──────────┬──────────┐',
|
||||
);
|
||||
console.log(
|
||||
'│ ID │ Name │ Status │ PID │ Memory │ Restarts │',
|
||||
);
|
||||
console.log(
|
||||
'├─────────┼─────────────┼───────────┼───────────┼──────────┼──────────┤',
|
||||
);
|
||||
|
||||
for (const proc of processes) {
|
||||
const statusColor =
|
||||
proc.status === 'online' ? '\x1b[32m' :
|
||||
proc.status === 'errored' ? '\x1b[31m' :
|
||||
'\x1b[33m';
|
||||
const resetColor = '\x1b[0m';
|
||||
for (const proc of processes) {
|
||||
const statusColor =
|
||||
proc.status === 'online'
|
||||
? '\x1b[32m'
|
||||
: proc.status === 'errored'
|
||||
? '\x1b[31m'
|
||||
: '\x1b[33m';
|
||||
const resetColor = '\x1b[0m';
|
||||
|
||||
console.log(
|
||||
`│ ${pad(proc.id, 7)} │ ${pad(proc.id, 11)} │ ${statusColor}${pad(proc.status, 9)}${resetColor} │ ${pad((proc.pid || '-').toString(), 9)} │ ${pad(formatMemory(proc.memory), 8)} │ ${pad(proc.restarts.toString(), 8)} │`,
|
||||
);
|
||||
}
|
||||
|
||||
console.log(
|
||||
`│ ${pad(proc.id, 7)} │ ${pad(proc.id, 11)} │ ${statusColor}${pad(proc.status, 9)}${resetColor} │ ${pad((proc.pid || '-').toString(), 9)} │ ${pad(formatMemory(proc.memory), 8)} │ ${pad(proc.restarts.toString(), 8)} │`,
|
||||
'└─────────┴─────────────┴───────────┴───────────┴──────────┴──────────┘',
|
||||
);
|
||||
}
|
||||
|
||||
console.log('└─────────┴─────────────┴───────────┴───────────┴──────────┴──────────┘');
|
||||
}, { actionLabel: 'list processes' });
|
||||
},
|
||||
{ actionLabel: 'list processes' },
|
||||
);
|
||||
}
|
@@ -7,67 +7,93 @@ import { formatLog } from '../../helpers/formatting.js';
|
||||
import { withStreamingLifecycle } from '../../helpers/lifecycle.js';
|
||||
|
||||
export function registerLogsCommand(smartcli: plugins.smartcli.Smartcli) {
|
||||
registerIpcCommand(smartcli, 'logs', async (argvArg: CliArguments) => {
|
||||
const id = argvArg._[1];
|
||||
if (!id) {
|
||||
console.error('Error: Please provide a process ID');
|
||||
console.log('Usage: tspm logs <id> [options]');
|
||||
console.log('\nOptions:');
|
||||
console.log(' --lines <n> Number of lines to show (default: 50)');
|
||||
console.log(' --follow Stream logs in real-time (like tail -f)');
|
||||
return;
|
||||
}
|
||||
registerIpcCommand(
|
||||
smartcli,
|
||||
'logs',
|
||||
async (argvArg: CliArguments) => {
|
||||
const id = argvArg._[1];
|
||||
if (!id) {
|
||||
console.error('Error: Please provide a process ID');
|
||||
console.log('Usage: tspm logs <id> [options]');
|
||||
console.log('\nOptions:');
|
||||
console.log(' --lines <n> Number of lines to show (default: 50)');
|
||||
console.log(' --follow Stream logs in real-time (like tail -f)');
|
||||
return;
|
||||
}
|
||||
|
||||
const lines = getNumber(argvArg, 'lines', 50);
|
||||
const follow = getBool(argvArg, 'follow', 'f');
|
||||
const lines = getNumber(argvArg, 'lines', 50);
|
||||
const follow = getBool(argvArg, 'follow', 'f');
|
||||
|
||||
const response = await tspmIpcClient.request('getLogs', { id, lines });
|
||||
const response = await tspmIpcClient.request('getLogs', { id, lines });
|
||||
|
||||
if (!follow) {
|
||||
// One-shot mode - auto-disconnect handled by registerIpcCommand
|
||||
console.log(`Logs for process: ${id} (last ${lines} lines)`);
|
||||
if (!follow) {
|
||||
// One-shot mode - auto-disconnect handled by registerIpcCommand
|
||||
console.log(`Logs for process: ${id} (last ${lines} lines)`);
|
||||
console.log('─'.repeat(60));
|
||||
for (const log of response.logs) {
|
||||
const timestamp = new Date(log.timestamp).toLocaleTimeString();
|
||||
const prefix =
|
||||
log.type === 'stdout'
|
||||
? '[OUT]'
|
||||
: log.type === 'stderr'
|
||||
? '[ERR]'
|
||||
: '[SYS]';
|
||||
console.log(`${timestamp} ${prefix} ${log.message}`);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Streaming mode
|
||||
console.log(`Logs for process: ${id} (streaming...)`);
|
||||
console.log('─'.repeat(60));
|
||||
|
||||
let lastSeq = 0;
|
||||
for (const log of response.logs) {
|
||||
const timestamp = new Date(log.timestamp).toLocaleTimeString();
|
||||
const prefix = log.type === 'stdout' ? '[OUT]' : log.type === 'stderr' ? '[ERR]' : '[SYS]';
|
||||
const prefix =
|
||||
log.type === 'stdout'
|
||||
? '[OUT]'
|
||||
: log.type === 'stderr'
|
||||
? '[ERR]'
|
||||
: '[SYS]';
|
||||
console.log(`${timestamp} ${prefix} ${log.message}`);
|
||||
if (log.seq !== undefined) lastSeq = Math.max(lastSeq, log.seq);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Streaming mode
|
||||
console.log(`Logs for process: ${id} (streaming...)`);
|
||||
console.log('─'.repeat(60));
|
||||
|
||||
let lastSeq = 0;
|
||||
for (const log of response.logs) {
|
||||
const timestamp = new Date(log.timestamp).toLocaleTimeString();
|
||||
const prefix = log.type === 'stdout' ? '[OUT]' : log.type === 'stderr' ? '[ERR]' : '[SYS]';
|
||||
console.log(`${timestamp} ${prefix} ${log.message}`);
|
||||
if (log.seq !== undefined) lastSeq = Math.max(lastSeq, log.seq);
|
||||
}
|
||||
|
||||
await withStreamingLifecycle(
|
||||
async () => {
|
||||
await tspmIpcClient.subscribe(id, (log: any) => {
|
||||
if (log.seq !== undefined && log.seq <= lastSeq) return;
|
||||
if (log.seq !== undefined && log.seq > lastSeq + 1) {
|
||||
console.log(`[WARNING] Log gap detected: expected seq ${lastSeq + 1}, got ${log.seq}`);
|
||||
}
|
||||
const timestamp = new Date(log.timestamp).toLocaleTimeString();
|
||||
const prefix = log.type === 'stdout' ? '[OUT]' : log.type === 'stderr' ? '[ERR]' : '[SYS]';
|
||||
console.log(`${timestamp} ${prefix} ${log.message}`);
|
||||
if (log.seq !== undefined) lastSeq = log.seq;
|
||||
});
|
||||
},
|
||||
async () => {
|
||||
console.log('\n\nStopping log stream...');
|
||||
try { await tspmIpcClient.unsubscribe(id); } catch {}
|
||||
try { await tspmIpcClient.disconnect(); } catch {}
|
||||
}
|
||||
);
|
||||
}, {
|
||||
actionLabel: 'get logs',
|
||||
keepAlive: (argv) => getBool(argv, 'follow', 'f')
|
||||
});
|
||||
await withStreamingLifecycle(
|
||||
async () => {
|
||||
await tspmIpcClient.subscribe(id, (log: any) => {
|
||||
if (log.seq !== undefined && log.seq <= lastSeq) return;
|
||||
if (log.seq !== undefined && log.seq > lastSeq + 1) {
|
||||
console.log(
|
||||
`[WARNING] Log gap detected: expected seq ${lastSeq + 1}, got ${log.seq}`,
|
||||
);
|
||||
}
|
||||
const timestamp = new Date(log.timestamp).toLocaleTimeString();
|
||||
const prefix =
|
||||
log.type === 'stdout'
|
||||
? '[OUT]'
|
||||
: log.type === 'stderr'
|
||||
? '[ERR]'
|
||||
: '[SYS]';
|
||||
console.log(`${timestamp} ${prefix} ${log.message}`);
|
||||
if (log.seq !== undefined) lastSeq = log.seq;
|
||||
});
|
||||
},
|
||||
async () => {
|
||||
console.log('\n\nStopping log stream...');
|
||||
try {
|
||||
await tspmIpcClient.unsubscribe(id);
|
||||
} catch {}
|
||||
try {
|
||||
await tspmIpcClient.disconnect();
|
||||
} catch {}
|
||||
},
|
||||
);
|
||||
},
|
||||
{
|
||||
actionLabel: 'get logs',
|
||||
keepAlive: (argv) => getBool(argv, 'follow', 'f'),
|
||||
},
|
||||
);
|
||||
}
|
@@ -1,23 +1,28 @@
|
||||
import * as plugins from '../../../plugins.js';
|
||||
import { tspmIpcClient } from '../../../classes.ipcclient.js';
|
||||
import { tspmIpcClient } from '../../../client/tspm.ipcclient.js';
|
||||
import type { CliArguments } from '../../types.js';
|
||||
import { registerIpcCommand } from '../../registration/index.js';
|
||||
|
||||
export function registerRestartCommand(smartcli: plugins.smartcli.Smartcli) {
|
||||
registerIpcCommand(smartcli, 'restart', async (argvArg: CliArguments) => {
|
||||
const id = argvArg._[1];
|
||||
if (!id) {
|
||||
console.error('Error: Please provide a process ID');
|
||||
console.log('Usage: tspm restart <id>');
|
||||
return;
|
||||
}
|
||||
registerIpcCommand(
|
||||
smartcli,
|
||||
'restart',
|
||||
async (argvArg: CliArguments) => {
|
||||
const id = argvArg._[1];
|
||||
if (!id) {
|
||||
console.error('Error: Please provide a process ID');
|
||||
console.log('Usage: tspm restart <id>');
|
||||
return;
|
||||
}
|
||||
|
||||
console.log(`Restarting process: ${id}`);
|
||||
const response = await tspmIpcClient.request('restart', { id });
|
||||
console.log(`Restarting process: ${id}`);
|
||||
const response = await tspmIpcClient.request('restart', { id });
|
||||
|
||||
console.log(`✓ Process restarted successfully`);
|
||||
console.log(` ID: ${response.processId}`);
|
||||
console.log(` PID: ${response.pid || 'N/A'}`);
|
||||
console.log(` Status: ${response.status}`);
|
||||
}, { actionLabel: 'restart process' });
|
||||
console.log(`✓ Process restarted successfully`);
|
||||
console.log(` ID: ${response.processId}`);
|
||||
console.log(` PID: ${response.pid || 'N/A'}`);
|
||||
console.log(` Status: ${response.status}`);
|
||||
},
|
||||
{ actionLabel: 'restart process' },
|
||||
);
|
||||
}
|
@@ -1,83 +1,121 @@
|
||||
import * as plugins from '../../../plugins.js';
|
||||
import { tspmIpcClient } from '../../../classes.ipcclient.js';
|
||||
import { tspmIpcClient } from '../../../client/tspm.ipcclient.js';
|
||||
import type { IProcessConfig } from '../../../classes.tspm.js';
|
||||
import type { CliArguments } from '../../types.js';
|
||||
import { parseMemoryString, formatMemory } from '../../helpers/memory.js';
|
||||
import { registerIpcCommand } from '../../registration/index.js';
|
||||
|
||||
export function registerStartCommand(smartcli: plugins.smartcli.Smartcli) {
|
||||
registerIpcCommand(smartcli, 'start', async (argvArg: CliArguments) => {
|
||||
const script = argvArg._[1];
|
||||
if (!script) {
|
||||
console.error('Error: Please provide a script to run');
|
||||
console.log('Usage: tspm start <script> [options]');
|
||||
console.log('\nOptions:');
|
||||
console.log(' --name <name> Name for the process');
|
||||
console.log(' --memory <size> Memory limit (e.g., "512MB", "2GB")');
|
||||
console.log(' --cwd <path> Working directory');
|
||||
console.log(' --watch Watch for file changes and restart');
|
||||
console.log(' --watch-paths <paths> Comma-separated paths to watch');
|
||||
console.log(' --autorestart Auto-restart on crash');
|
||||
return;
|
||||
}
|
||||
|
||||
const memoryLimit = argvArg.memory ? parseMemoryString(argvArg.memory) : 512 * 1024 * 1024;
|
||||
const projectDir = argvArg.cwd || process.cwd();
|
||||
|
||||
// Direct .ts support via tsx (bundled with TSPM)
|
||||
let actualCommand = script;
|
||||
let commandArgs: string[] | undefined = undefined;
|
||||
|
||||
if (script.endsWith('.ts')) {
|
||||
try {
|
||||
const tsxPath = await (async () => {
|
||||
const { createRequire } = await import('module');
|
||||
const require = createRequire(import.meta.url);
|
||||
return require.resolve('tsx/dist/cli.mjs');
|
||||
})();
|
||||
|
||||
const scriptPath = plugins.path.isAbsolute(script) ? script : plugins.path.join(projectDir, script);
|
||||
actualCommand = tsxPath;
|
||||
commandArgs = [scriptPath];
|
||||
} catch {
|
||||
actualCommand = 'tsx';
|
||||
commandArgs = [script];
|
||||
registerIpcCommand(
|
||||
smartcli,
|
||||
'start',
|
||||
async (argvArg: CliArguments) => {
|
||||
// Get all arguments after 'start' command
|
||||
const commandArgs = argvArg._.slice(1);
|
||||
if (commandArgs.length === 0) {
|
||||
console.error('Error: Please provide a command to run');
|
||||
console.log('Usage: tspm start <command> [options]');
|
||||
console.log('\nExamples:');
|
||||
console.log(' tspm start "npm run dev"');
|
||||
console.log(' tspm start pnpm start');
|
||||
console.log(' tspm start node server.js');
|
||||
console.log(' tspm start script.ts');
|
||||
console.log('\nOptions:');
|
||||
console.log(' --name <name> Name for the process');
|
||||
console.log(
|
||||
' --memory <size> Memory limit (e.g., "512MB", "2GB")',
|
||||
);
|
||||
console.log(' --cwd <path> Working directory');
|
||||
console.log(
|
||||
' --watch Watch for file changes and restart',
|
||||
);
|
||||
console.log(' --watch-paths <paths> Comma-separated paths to watch');
|
||||
console.log(' --autorestart Auto-restart on crash');
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
const name = argvArg.name || script;
|
||||
const watch = argvArg.watch || false;
|
||||
const autorestart = argvArg.autorestart !== false; // default true
|
||||
const watchPaths = argvArg.watchPaths
|
||||
? (typeof argvArg.watchPaths === 'string' ? (argvArg.watchPaths as string).split(',') : argvArg.watchPaths)
|
||||
: undefined;
|
||||
// Join all command parts to form the full command
|
||||
const script = commandArgs.join(' ');
|
||||
|
||||
const processConfig: IProcessConfig = {
|
||||
id: name.replace(/[^a-zA-Z0-9-_]/g, '_'),
|
||||
name,
|
||||
command: actualCommand,
|
||||
args: commandArgs,
|
||||
projectDir,
|
||||
memoryLimitBytes: memoryLimit,
|
||||
autorestart,
|
||||
watch,
|
||||
watchPaths,
|
||||
};
|
||||
const memoryLimit = argvArg.memory
|
||||
? parseMemoryString(argvArg.memory)
|
||||
: 512 * 1024 * 1024;
|
||||
const projectDir = argvArg.cwd || process.cwd();
|
||||
|
||||
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(', ')}`);
|
||||
}
|
||||
// Parse the command to determine if we need to handle .ts files
|
||||
let actualCommand: string;
|
||||
let processArgs: string[] | undefined = undefined;
|
||||
|
||||
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' });
|
||||
// Split the script to check if it's a single .ts file or a full command
|
||||
const scriptParts = script.split(' ');
|
||||
const firstPart = scriptParts[0];
|
||||
|
||||
// Check if this is a direct .ts file execution (single argument ending in .ts)
|
||||
if (scriptParts.length === 1 && firstPart.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(firstPart)
|
||||
? firstPart
|
||||
: plugins.path.join(projectDir, firstPart);
|
||||
actualCommand = tsxPath;
|
||||
processArgs = [scriptPath];
|
||||
} catch {
|
||||
actualCommand = 'tsx';
|
||||
processArgs = [firstPart];
|
||||
}
|
||||
} else {
|
||||
// For multi-word commands, use the entire script as the command
|
||||
// This handles cases like "pnpm start", "npm run dev", etc.
|
||||
actualCommand = script;
|
||||
processArgs = undefined;
|
||||
}
|
||||
|
||||
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: processArgs,
|
||||
projectDir,
|
||||
memoryLimitBytes: memoryLimit,
|
||||
autorestart,
|
||||
watch,
|
||||
watchPaths,
|
||||
};
|
||||
|
||||
console.log(`Starting process: ${name}`);
|
||||
console.log(
|
||||
` Command: ${script}${scriptParts.length === 1 && firstPart.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' },
|
||||
);
|
||||
}
|
@@ -1,24 +1,29 @@
|
||||
import * as plugins from '../../../plugins.js';
|
||||
import { tspmIpcClient } from '../../../classes.ipcclient.js';
|
||||
import { tspmIpcClient } from '../../../client/tspm.ipcclient.js';
|
||||
import type { CliArguments } from '../../types.js';
|
||||
import { registerIpcCommand } from '../../registration/index.js';
|
||||
|
||||
export function registerStopCommand(smartcli: plugins.smartcli.Smartcli) {
|
||||
registerIpcCommand(smartcli, 'stop', async (argvArg: CliArguments) => {
|
||||
const id = argvArg._[1];
|
||||
if (!id) {
|
||||
console.error('Error: Please provide a process ID');
|
||||
console.log('Usage: tspm stop <id>');
|
||||
return;
|
||||
}
|
||||
registerIpcCommand(
|
||||
smartcli,
|
||||
'stop',
|
||||
async (argvArg: CliArguments) => {
|
||||
const id = argvArg._[1];
|
||||
if (!id) {
|
||||
console.error('Error: Please provide a process ID');
|
||||
console.log('Usage: tspm stop <id>');
|
||||
return;
|
||||
}
|
||||
|
||||
console.log(`Stopping process: ${id}`);
|
||||
const response = await tspmIpcClient.request('stop', { id });
|
||||
console.log(`Stopping process: ${id}`);
|
||||
const response = await tspmIpcClient.request('stop', { id });
|
||||
|
||||
if (response.success) {
|
||||
console.log(`✓ ${response.message}`);
|
||||
} else {
|
||||
console.error(`✗ Failed to stop process: ${response.message}`);
|
||||
}
|
||||
}, { actionLabel: 'stop process' });
|
||||
if (response.success) {
|
||||
console.log(`✓ ${response.message}`);
|
||||
} else {
|
||||
console.error(`✗ Failed to stop process: ${response.message}`);
|
||||
}
|
||||
},
|
||||
{ actionLabel: 'stop process' },
|
||||
);
|
||||
}
|
@@ -19,7 +19,10 @@ export function registerDisableCommand(smartcli: plugins.smartcli.Smartcli) {
|
||||
console.log(' Use "tspm enable" to re-enable the service');
|
||||
} catch (error) {
|
||||
console.error('Error disabling service:', error.message);
|
||||
if (error.message.includes('permission') || error.message.includes('denied')) {
|
||||
if (
|
||||
error.message.includes('permission') ||
|
||||
error.message.includes('denied')
|
||||
) {
|
||||
console.log('\nNote: You may need to run this command with sudo');
|
||||
}
|
||||
process.exit(1);
|
||||
|
@@ -19,7 +19,10 @@ export function registerEnableCommand(smartcli: plugins.smartcli.Smartcli) {
|
||||
console.log(' Use "tspm disable" to remove the service');
|
||||
} catch (error) {
|
||||
console.error('Error enabling service:', error.message);
|
||||
if (error.message.includes('permission') || error.message.includes('denied')) {
|
||||
if (
|
||||
error.message.includes('permission') ||
|
||||
error.message.includes('denied')
|
||||
) {
|
||||
console.log('\nNote: You may need to run this command with sudo');
|
||||
}
|
||||
process.exit(1);
|
||||
|
@@ -2,15 +2,23 @@ import type { CliArguments } from '../types.js';
|
||||
|
||||
// Argument parsing helpers
|
||||
export const getBool = (argv: CliArguments, ...keys: string[]) =>
|
||||
keys.some(k => Boolean((argv as any)[k]));
|
||||
keys.some((k) => Boolean((argv as any)[k]));
|
||||
|
||||
export const getNumber = (argv: CliArguments, key: string, fallback: number) => {
|
||||
export const getNumber = (
|
||||
argv: CliArguments,
|
||||
key: string,
|
||||
fallback: number,
|
||||
) => {
|
||||
const v = (argv as any)[key];
|
||||
const n = typeof v === 'string' ? Number(v) : v;
|
||||
return Number.isFinite(n) ? n : fallback;
|
||||
};
|
||||
|
||||
export const getString = (argv: CliArguments, key: string, fallback?: string) => {
|
||||
export const getString = (
|
||||
argv: CliArguments,
|
||||
key: string,
|
||||
fallback?: string,
|
||||
) => {
|
||||
const v = (argv as any)[key];
|
||||
return typeof v === 'string' ? v : fallback;
|
||||
};
|
@@ -1,12 +1,16 @@
|
||||
// Helper function to handle daemon connection errors
|
||||
export function handleDaemonError(error: any, action: string): void {
|
||||
if (error.message?.includes('daemon is not running') ||
|
||||
error.message?.includes('Not connected') ||
|
||||
error.message?.includes('ECONNREFUSED')) {
|
||||
if (
|
||||
error.message?.includes('daemon is not running') ||
|
||||
error.message?.includes('Not connected') ||
|
||||
error.message?.includes('ECONNREFUSED')
|
||||
) {
|
||||
console.error(`Error: Cannot ${action} - TSPM daemon is not running.`);
|
||||
console.log('\nTo start the daemon, run one of:');
|
||||
console.log(' tspm daemon start - Start for this session only');
|
||||
console.log(' tspm enable - Enable as system service (recommended)');
|
||||
console.log(
|
||||
' tspm enable - Enable as system service (recommended)',
|
||||
);
|
||||
} else {
|
||||
console.error(`Error ${action}:`, error.message);
|
||||
}
|
||||
|
@@ -7,11 +7,12 @@ export function pad(str: string, length: number): string {
|
||||
|
||||
// Helper for unknown errors
|
||||
export const unknownError = (err: any) =>
|
||||
(err?.message && typeof err.message === 'string') ? err.message : String(err);
|
||||
err?.message && typeof err.message === 'string' ? err.message : String(err);
|
||||
|
||||
// Helper function to format log entries
|
||||
export function formatLog(log: any): string {
|
||||
const timestamp = new Date(log.timestamp).toLocaleTimeString();
|
||||
const prefix = log.type === 'stdout' ? '[OUT]' : log.type === 'stderr' ? '[ERR]' : '[SYS]';
|
||||
const prefix =
|
||||
log.type === 'stdout' ? '[OUT]' : log.type === 'stderr' ? '[ERR]' : '[SYS]';
|
||||
return `${timestamp} ${prefix} ${log.message}`;
|
||||
}
|
@@ -1,6 +1,6 @@
|
||||
import * as plugins from '../plugins.js';
|
||||
import * as paths from '../paths.js';
|
||||
import { Logger, LogLevel } from '../utils.errorhandler.js';
|
||||
import { Logger, LogLevel } from '../shared/common/utils.errorhandler.js';
|
||||
|
||||
// Import command registration functions
|
||||
import { registerDefaultCommand } from './commands/default.js';
|
||||
|
@@ -4,15 +4,22 @@ import { tspmIpcClient } from '../../classes.ipcclient.js';
|
||||
* Preflight the daemon if required. Uses getDaemonStatus() which is safe and cheap:
|
||||
* it only connects if the PID file is valid.
|
||||
*/
|
||||
export async function ensureDaemonOrHint(requireDaemon: boolean | undefined, actionLabel?: string): Promise<boolean> {
|
||||
export async function ensureDaemonOrHint(
|
||||
requireDaemon: boolean | undefined,
|
||||
actionLabel?: string,
|
||||
): Promise<boolean> {
|
||||
if (requireDaemon === false) return true; // command does not require daemon
|
||||
const status = await tspmIpcClient.getDaemonStatus();
|
||||
if (!status) {
|
||||
// Same hint as handleDaemonError, but early and consistent
|
||||
console.error(`Error: Cannot ${actionLabel || 'perform action'} - TSPM daemon is not running.`);
|
||||
console.error(
|
||||
`Error: Cannot ${actionLabel || 'perform action'} - TSPM daemon is not running.`,
|
||||
);
|
||||
console.log('\nTo start the daemon, run one of:');
|
||||
console.log(' tspm daemon start - Start for this session only');
|
||||
console.log(' tspm enable - Enable as system service (recommended)');
|
||||
console.log(
|
||||
' tspm enable - Enable as system service (recommended)',
|
||||
);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
|
@@ -1,5 +1,9 @@
|
||||
import * as plugins from '../../plugins.js';
|
||||
import type { CliArguments, CommandAction, IpcCommandOptions } from '../types.js';
|
||||
import type {
|
||||
CliArguments,
|
||||
CommandAction,
|
||||
IpcCommandOptions,
|
||||
} from '../types.js';
|
||||
import { handleDaemonError } from '../helpers/errors.js';
|
||||
import { unknownError } from '../helpers/formatting.js';
|
||||
import { runIpcCommand } from '../utils/ipc.js';
|
||||
@@ -15,7 +19,7 @@ export function registerIpcCommand(
|
||||
smartcli: plugins.smartcli.Smartcli,
|
||||
name: string,
|
||||
action: CommandAction,
|
||||
opts: IpcCommandOptions = {}
|
||||
opts: IpcCommandOptions = {},
|
||||
) {
|
||||
const { actionLabel = name, keepAlive = false, requireDaemon = true } = opts;
|
||||
|
||||
@@ -29,7 +33,8 @@ export function registerIpcCommand(
|
||||
}
|
||||
|
||||
// Evaluate keepAlive - can be boolean or function
|
||||
const shouldKeepAlive = typeof keepAlive === 'function' ? keepAlive(argv) : keepAlive;
|
||||
const shouldKeepAlive =
|
||||
typeof keepAlive === 'function' ? keepAlive(argv) : keepAlive;
|
||||
|
||||
if (shouldKeepAlive) {
|
||||
// Let action manage its own connection/cleanup lifecycle
|
||||
@@ -51,7 +56,10 @@ export function registerIpcCommand(
|
||||
},
|
||||
error: (err) => {
|
||||
// Fallback error path (should be rare with try/catch in next)
|
||||
console.error(`Unexpected error in command "${name}":`, unknownError(err));
|
||||
console.error(
|
||||
`Unexpected error in command "${name}":`,
|
||||
unknownError(err),
|
||||
);
|
||||
process.exit(1);
|
||||
},
|
||||
complete: () => {},
|
||||
@@ -66,7 +74,7 @@ export function registerLocalCommand(
|
||||
smartcli: plugins.smartcli.Smartcli,
|
||||
name: string,
|
||||
action: (argv: CliArguments) => Promise<void>,
|
||||
opts: { actionLabel?: string } = {}
|
||||
opts: { actionLabel?: string } = {},
|
||||
) {
|
||||
const { actionLabel = name } = opts;
|
||||
smartcli.addCommand(name).subscribe({
|
||||
@@ -79,7 +87,10 @@ export function registerLocalCommand(
|
||||
}
|
||||
},
|
||||
error: (err) => {
|
||||
console.error(`Unexpected error in command "${name}":`, unknownError(err));
|
||||
console.error(
|
||||
`Unexpected error in command "${name}":`,
|
||||
unknownError(err),
|
||||
);
|
||||
process.exit(1);
|
||||
},
|
||||
complete: () => {},
|
||||
|
@@ -14,7 +14,7 @@ export interface CliArguments {
|
||||
export type CommandAction = (argv: CliArguments) => Promise<void>;
|
||||
|
||||
export interface IpcCommandOptions {
|
||||
actionLabel?: string; // used in error message, e.g. "start process"
|
||||
keepAlive?: boolean | ((argv: CliArguments) => boolean); // true for streaming commands (don't auto-disconnect), or function to determine at runtime
|
||||
requireDaemon?: boolean; // default true for IPC-bound commands
|
||||
actionLabel?: string; // used in error message, e.g. "start process"
|
||||
keepAlive?: boolean | ((argv: CliArguments) => boolean); // true for streaming commands (don't auto-disconnect), or function to determine at runtime
|
||||
requireDaemon?: boolean; // default true for IPC-bound commands
|
||||
}
|
8
ts/client/index.ts
Normal file
8
ts/client/index.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
/**
|
||||
* Client-side exports for TSPM
|
||||
* These are the only components that client applications should use
|
||||
* They only communicate with the daemon via IPC, never directly manage processes
|
||||
*/
|
||||
|
||||
export * from './tspm.ipcclient.js';
|
||||
export * from './tspm.servicemanager.js';
|
@@ -1,11 +1,11 @@
|
||||
import * as plugins from './plugins.js';
|
||||
import * as paths from './paths.js';
|
||||
import * as plugins from '../plugins.js';
|
||||
import * as paths from '../paths.js';
|
||||
|
||||
import type {
|
||||
IpcMethodMap,
|
||||
RequestForMethod,
|
||||
ResponseForMethod,
|
||||
} from './ipc.types.js';
|
||||
} from '../shared/protocol/ipc.types.js';
|
||||
|
||||
/**
|
||||
* IPC client for communicating with the TSPM daemon
|
||||
@@ -36,9 +36,9 @@ export class TspmIpcClient {
|
||||
if (!daemonRunning) {
|
||||
throw new Error(
|
||||
'TSPM daemon is not running.\n\n' +
|
||||
'To start the daemon, run one of:\n' +
|
||||
' tspm daemon start - Start daemon for this session\n' +
|
||||
' tspm enable - Enable daemon as system service (recommended)\n'
|
||||
'To start the daemon, run one of:\n' +
|
||||
' tspm daemon start - Start daemon for this session\n' +
|
||||
' tspm enable - Enable daemon as system service (recommended)\n',
|
||||
);
|
||||
}
|
||||
|
||||
@@ -59,7 +59,7 @@ export class TspmIpcClient {
|
||||
heartbeatInterval: 5000,
|
||||
heartbeatTimeout: 20000,
|
||||
heartbeatInitialGracePeriodMs: 10000,
|
||||
heartbeatThrowOnTimeout: false // Don't throw, emit events instead
|
||||
heartbeatThrowOnTimeout: false, // Don't throw, emit events instead
|
||||
});
|
||||
|
||||
// Connect to the daemon
|
||||
@@ -121,7 +121,10 @@ export class TspmIpcClient {
|
||||
/**
|
||||
* Subscribe to log updates for a specific process
|
||||
*/
|
||||
public async subscribe(processId: string, handler: (log: any) => void): Promise<void> {
|
||||
public async subscribe(
|
||||
processId: string,
|
||||
handler: (log: any) => void,
|
||||
): Promise<void> {
|
||||
if (!this.ipcClient || !this.isConnected) {
|
||||
throw new Error('Not connected to daemon');
|
||||
}
|
||||
@@ -184,8 +187,6 @@ export class TspmIpcClient {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Stop the daemon
|
||||
*/
|
@@ -1,5 +1,5 @@
|
||||
import * as plugins from './plugins.js';
|
||||
import * as paths from './paths.js';
|
||||
import * as plugins from '../plugins.js';
|
||||
import * as paths from '../paths.js';
|
||||
|
||||
/**
|
||||
* Manages TSPM daemon as a systemd service via smartdaemon
|
||||
@@ -25,7 +25,7 @@ export class TspmServiceManager {
|
||||
description: 'TSPM Process Manager Daemon',
|
||||
command: `${process.execPath} ${cliPath} daemon start-service`,
|
||||
workingDir: process.env.HOME || process.cwd(),
|
||||
version: '1.0.0'
|
||||
version: '1.0.0',
|
||||
});
|
||||
}
|
||||
return this.service;
|
||||
@@ -82,13 +82,13 @@ export class TspmServiceManager {
|
||||
return {
|
||||
enabled: true, // Would need to check systemctl is-enabled
|
||||
running: true, // Would need to check systemctl is-active
|
||||
status: 'active'
|
||||
status: 'active',
|
||||
};
|
||||
} catch (error) {
|
||||
return {
|
||||
enabled: false,
|
||||
running: false,
|
||||
status: 'inactive'
|
||||
status: 'inactive',
|
||||
};
|
||||
}
|
||||
}
|
@@ -1,6 +1,6 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
import { startDaemon } from './classes.daemon.js';
|
||||
import { startDaemon } from './tspm.daemon.js';
|
||||
|
||||
// Start the daemon
|
||||
startDaemon().catch((error) => {
|
@@ -1,19 +1,19 @@
|
||||
import * as plugins from './plugins.js';
|
||||
import * as plugins from '../plugins.js';
|
||||
import { EventEmitter } from 'events';
|
||||
import * as paths from './paths.js';
|
||||
import * as paths from '../paths.js';
|
||||
import {
|
||||
ProcessMonitor,
|
||||
type IMonitorConfig,
|
||||
} from './classes.processmonitor.js';
|
||||
import { type IProcessLog } from './classes.processwrapper.js';
|
||||
import { TspmConfig } from './classes.config.js';
|
||||
} from './processmonitor.js';
|
||||
import { type IProcessLog } from './processwrapper.js';
|
||||
import { TspmConfig } from './tspm.config.js';
|
||||
import {
|
||||
Logger,
|
||||
ProcessError,
|
||||
ConfigError,
|
||||
ValidationError,
|
||||
handleError,
|
||||
} from './utils.errorhandler.js';
|
||||
} from '../shared/common/utils.errorhandler.js';
|
||||
|
||||
export interface IProcessConfig extends IMonitorConfig {
|
||||
id: string; // Unique identifier for the process
|
||||
@@ -32,9 +32,7 @@ export interface IProcessInfo {
|
||||
restarts: number;
|
||||
}
|
||||
|
||||
|
||||
|
||||
export class Tspm extends EventEmitter {
|
||||
export class ProcessManager extends EventEmitter {
|
||||
public processes: Map<string, ProcessMonitor> = new Map();
|
||||
public processConfigs: Map<string, IProcessConfig> = new Map();
|
||||
public processInfo: Map<string, IProcessInfo> = new Map();
|
@@ -1,7 +1,7 @@
|
||||
import * as plugins from './plugins.js';
|
||||
import * as plugins from '../plugins.js';
|
||||
import { EventEmitter } from 'events';
|
||||
import { ProcessWrapper, type IProcessLog } from './classes.processwrapper.js';
|
||||
import { Logger, ProcessError, handleError } from './utils.errorhandler.js';
|
||||
import { ProcessWrapper, type IProcessLog } from './processwrapper.js';
|
||||
import { Logger, ProcessError, handleError } from '../shared/common/utils.errorhandler.js';
|
||||
|
||||
export interface IMonitorConfig {
|
||||
name?: string; // Optional name to identify the instance
|
@@ -1,6 +1,6 @@
|
||||
import * as plugins from './plugins.js';
|
||||
import * as plugins from '../plugins.js';
|
||||
import { EventEmitter } from 'events';
|
||||
import { Logger, ProcessError, handleError } from './utils.errorhandler.js';
|
||||
import { Logger, ProcessError, handleError } from '../shared/common/utils.errorhandler.js';
|
||||
|
||||
export interface IProcessWrapperOptions {
|
||||
command: string;
|
@@ -1,4 +1,4 @@
|
||||
import * as plugins from './plugins.js';
|
||||
import * as plugins from '../plugins.js';
|
||||
|
||||
export class TspmConfig {
|
||||
public npmextraInstance = new plugins.npmextra.KeyValueStore({
|
@@ -1,19 +1,19 @@
|
||||
import * as plugins from './plugins.js';
|
||||
import * as paths from './paths.js';
|
||||
import { Tspm } from './classes.tspm.js';
|
||||
import * as plugins from '../plugins.js';
|
||||
import * as paths from '../paths.js';
|
||||
import { ProcessManager } from './processmanager.js';
|
||||
import type {
|
||||
IpcMethodMap,
|
||||
RequestForMethod,
|
||||
ResponseForMethod,
|
||||
DaemonStatusResponse,
|
||||
HeartbeatResponse,
|
||||
} from './ipc.types.js';
|
||||
} from '../shared/protocol/ipc.types.js';
|
||||
|
||||
/**
|
||||
* Central daemon server that manages all TSPM processes
|
||||
*/
|
||||
export class TspmDaemon {
|
||||
private tspmInstance: Tspm;
|
||||
private tspmInstance: ProcessManager;
|
||||
private ipcServer: plugins.smartipc.IpcServer;
|
||||
private startTime: number;
|
||||
private isShuttingDown: boolean = false;
|
||||
@@ -22,7 +22,7 @@ export class TspmDaemon {
|
||||
private daemonPidFile: string;
|
||||
|
||||
constructor() {
|
||||
this.tspmInstance = new Tspm();
|
||||
this.tspmInstance = new ProcessManager();
|
||||
this.socketPath = plugins.path.join(paths.tspmDir, 'tspm.sock');
|
||||
this.daemonPidFile = plugins.path.join(paths.tspmDir, 'daemon.pid');
|
||||
this.startTime = Date.now();
|
||||
@@ -34,6 +34,10 @@ export class TspmDaemon {
|
||||
public async start(): Promise<void> {
|
||||
console.log('Starting TSPM daemon...');
|
||||
|
||||
// Ensure the TSPM directory exists
|
||||
const fs = await import('fs/promises');
|
||||
await fs.mkdir(paths.tspmDir, { recursive: true });
|
||||
|
||||
// Check if another daemon is already running
|
||||
if (await this.isDaemonRunning()) {
|
||||
throw new Error('Another TSPM daemon instance is already running');
|
||||
@@ -43,12 +47,12 @@ export class TspmDaemon {
|
||||
this.ipcServer = plugins.smartipc.SmartIpc.createServer({
|
||||
id: 'tspm-daemon',
|
||||
socketPath: this.socketPath,
|
||||
autoCleanupSocketFile: true, // Clean up stale sockets
|
||||
socketMode: 0o600, // Set proper permissions
|
||||
autoCleanupSocketFile: true, // Clean up stale sockets
|
||||
socketMode: 0o600, // Set proper permissions
|
||||
heartbeat: true,
|
||||
heartbeatInterval: 5000,
|
||||
heartbeatTimeout: 20000,
|
||||
heartbeatInitialGracePeriodMs: 10000 // Grace period for startup
|
||||
heartbeatInitialGracePeriodMs: 10000, // Grace period for startup
|
||||
});
|
||||
|
||||
// Register message handlers
|
||||
@@ -122,19 +126,22 @@ export class TspmDaemon {
|
||||
},
|
||||
);
|
||||
|
||||
this.ipcServer.onMessage('restart', async (request: RequestForMethod<'restart'>) => {
|
||||
try {
|
||||
await this.tspmInstance.restart(request.id);
|
||||
const processInfo = this.tspmInstance.processInfo.get(request.id);
|
||||
return {
|
||||
processId: request.id,
|
||||
pid: processInfo?.pid,
|
||||
status: processInfo?.status || 'stopped',
|
||||
};
|
||||
} catch (error) {
|
||||
throw new Error(`Failed to restart process: ${error.message}`);
|
||||
}
|
||||
});
|
||||
this.ipcServer.onMessage(
|
||||
'restart',
|
||||
async (request: RequestForMethod<'restart'>) => {
|
||||
try {
|
||||
await this.tspmInstance.restart(request.id);
|
||||
const processInfo = this.tspmInstance.processInfo.get(request.id);
|
||||
return {
|
||||
processId: request.id,
|
||||
pid: processInfo?.pid,
|
||||
status: processInfo?.status || 'stopped',
|
||||
};
|
||||
} catch (error) {
|
||||
throw new Error(`Failed to restart process: ${error.message}`);
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
this.ipcServer.onMessage(
|
||||
'delete',
|
||||
@@ -160,124 +167,148 @@ export class TspmDaemon {
|
||||
},
|
||||
);
|
||||
|
||||
this.ipcServer.onMessage('describe', async (request: RequestForMethod<'describe'>) => {
|
||||
const processInfo = await this.tspmInstance.describe(request.id);
|
||||
const config = this.tspmInstance.processConfigs.get(request.id);
|
||||
this.ipcServer.onMessage(
|
||||
'describe',
|
||||
async (request: RequestForMethod<'describe'>) => {
|
||||
const processInfo = await this.tspmInstance.describe(request.id);
|
||||
const config = this.tspmInstance.processConfigs.get(request.id);
|
||||
|
||||
if (!processInfo || !config) {
|
||||
throw new Error(`Process ${request.id} not found`);
|
||||
}
|
||||
if (!processInfo || !config) {
|
||||
throw new Error(`Process ${request.id} not found`);
|
||||
}
|
||||
|
||||
return {
|
||||
processInfo,
|
||||
config,
|
||||
};
|
||||
});
|
||||
return {
|
||||
processInfo,
|
||||
config,
|
||||
};
|
||||
},
|
||||
);
|
||||
|
||||
this.ipcServer.onMessage('getLogs', async (request: RequestForMethod<'getLogs'>) => {
|
||||
const logs = await this.tspmInstance.getLogs(request.id);
|
||||
return { logs };
|
||||
});
|
||||
this.ipcServer.onMessage(
|
||||
'getLogs',
|
||||
async (request: RequestForMethod<'getLogs'>) => {
|
||||
const logs = await this.tspmInstance.getLogs(request.id);
|
||||
return { logs };
|
||||
},
|
||||
);
|
||||
|
||||
// Batch operations handlers
|
||||
this.ipcServer.onMessage('startAll', async (request: RequestForMethod<'startAll'>) => {
|
||||
const started: string[] = [];
|
||||
const failed: Array<{ id: string; error: string }> = [];
|
||||
this.ipcServer.onMessage(
|
||||
'startAll',
|
||||
async (request: RequestForMethod<'startAll'>) => {
|
||||
const started: string[] = [];
|
||||
const failed: Array<{ id: string; error: string }> = [];
|
||||
|
||||
await this.tspmInstance.startAll();
|
||||
await this.tspmInstance.startAll();
|
||||
|
||||
// Get status of all processes
|
||||
for (const [id, processInfo] of this.tspmInstance.processInfo) {
|
||||
if (processInfo.status === 'online') {
|
||||
started.push(id);
|
||||
} else {
|
||||
failed.push({ id, error: 'Failed to start' });
|
||||
// Get status of all processes
|
||||
for (const [id, processInfo] of this.tspmInstance.processInfo) {
|
||||
if (processInfo.status === 'online') {
|
||||
started.push(id);
|
||||
} else {
|
||||
failed.push({ id, error: 'Failed to start' });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return { started, failed };
|
||||
});
|
||||
return { started, failed };
|
||||
},
|
||||
);
|
||||
|
||||
this.ipcServer.onMessage('stopAll', async (request: RequestForMethod<'stopAll'>) => {
|
||||
const stopped: string[] = [];
|
||||
const failed: Array<{ id: string; error: string }> = [];
|
||||
this.ipcServer.onMessage(
|
||||
'stopAll',
|
||||
async (request: RequestForMethod<'stopAll'>) => {
|
||||
const stopped: string[] = [];
|
||||
const failed: Array<{ id: string; error: string }> = [];
|
||||
|
||||
await this.tspmInstance.stopAll();
|
||||
await this.tspmInstance.stopAll();
|
||||
|
||||
// Get status of all processes
|
||||
for (const [id, processInfo] of this.tspmInstance.processInfo) {
|
||||
if (processInfo.status === 'stopped') {
|
||||
stopped.push(id);
|
||||
} else {
|
||||
failed.push({ id, error: 'Failed to stop' });
|
||||
// Get status of all processes
|
||||
for (const [id, processInfo] of this.tspmInstance.processInfo) {
|
||||
if (processInfo.status === 'stopped') {
|
||||
stopped.push(id);
|
||||
} else {
|
||||
failed.push({ id, error: 'Failed to stop' });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return { stopped, failed };
|
||||
});
|
||||
return { stopped, failed };
|
||||
},
|
||||
);
|
||||
|
||||
this.ipcServer.onMessage('restartAll', async (request: RequestForMethod<'restartAll'>) => {
|
||||
const restarted: string[] = [];
|
||||
const failed: Array<{ id: string; error: string }> = [];
|
||||
this.ipcServer.onMessage(
|
||||
'restartAll',
|
||||
async (request: RequestForMethod<'restartAll'>) => {
|
||||
const restarted: string[] = [];
|
||||
const failed: Array<{ id: string; error: string }> = [];
|
||||
|
||||
await this.tspmInstance.restartAll();
|
||||
await this.tspmInstance.restartAll();
|
||||
|
||||
// Get status of all processes
|
||||
for (const [id, processInfo] of this.tspmInstance.processInfo) {
|
||||
if (processInfo.status === 'online') {
|
||||
restarted.push(id);
|
||||
} else {
|
||||
failed.push({ id, error: 'Failed to restart' });
|
||||
// Get status of all processes
|
||||
for (const [id, processInfo] of this.tspmInstance.processInfo) {
|
||||
if (processInfo.status === 'online') {
|
||||
restarted.push(id);
|
||||
} else {
|
||||
failed.push({ id, error: 'Failed to restart' });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return { restarted, failed };
|
||||
});
|
||||
return { restarted, failed };
|
||||
},
|
||||
);
|
||||
|
||||
// Daemon management handlers
|
||||
this.ipcServer.onMessage('daemon:status', async (request: RequestForMethod<'daemon:status'>) => {
|
||||
const memUsage = process.memoryUsage();
|
||||
return {
|
||||
status: 'running',
|
||||
pid: process.pid,
|
||||
uptime: Date.now() - this.startTime,
|
||||
processCount: this.tspmInstance.processes.size,
|
||||
memoryUsage: memUsage.heapUsed,
|
||||
cpuUsage: process.cpuUsage().user / 1000000, // Convert to seconds
|
||||
};
|
||||
});
|
||||
|
||||
this.ipcServer.onMessage('daemon:shutdown', async (request: RequestForMethod<'daemon:shutdown'>) => {
|
||||
if (this.isShuttingDown) {
|
||||
this.ipcServer.onMessage(
|
||||
'daemon:status',
|
||||
async (request: RequestForMethod<'daemon:status'>) => {
|
||||
const memUsage = process.memoryUsage();
|
||||
return {
|
||||
success: false,
|
||||
message: 'Daemon is already shutting down',
|
||||
status: 'running',
|
||||
pid: process.pid,
|
||||
uptime: Date.now() - this.startTime,
|
||||
processCount: this.tspmInstance.processes.size,
|
||||
memoryUsage: memUsage.heapUsed,
|
||||
cpuUsage: process.cpuUsage().user / 1000000, // Convert to seconds
|
||||
};
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
// Schedule shutdown
|
||||
const graceful = request.graceful !== false;
|
||||
const timeout = request.timeout || 10000;
|
||||
this.ipcServer.onMessage(
|
||||
'daemon:shutdown',
|
||||
async (request: RequestForMethod<'daemon:shutdown'>) => {
|
||||
if (this.isShuttingDown) {
|
||||
return {
|
||||
success: false,
|
||||
message: 'Daemon is already shutting down',
|
||||
};
|
||||
}
|
||||
|
||||
if (graceful) {
|
||||
setTimeout(() => this.shutdown(true), 100);
|
||||
} else {
|
||||
setTimeout(() => this.shutdown(false), 100);
|
||||
}
|
||||
// Schedule shutdown
|
||||
const graceful = request.graceful !== false;
|
||||
const timeout = request.timeout || 10000;
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: `Daemon will shutdown ${graceful ? 'gracefully' : 'immediately'} in ${timeout}ms`,
|
||||
};
|
||||
});
|
||||
if (graceful) {
|
||||
setTimeout(() => this.shutdown(true), 100);
|
||||
} else {
|
||||
setTimeout(() => this.shutdown(false), 100);
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: `Daemon will shutdown ${graceful ? 'gracefully' : 'immediately'} in ${timeout}ms`,
|
||||
};
|
||||
},
|
||||
);
|
||||
|
||||
// Heartbeat handler
|
||||
this.ipcServer.onMessage('heartbeat', async (request: RequestForMethod<'heartbeat'>) => {
|
||||
return {
|
||||
timestamp: Date.now(),
|
||||
status: this.isShuttingDown ? 'degraded' : 'healthy',
|
||||
};
|
||||
});
|
||||
this.ipcServer.onMessage(
|
||||
'heartbeat',
|
||||
async (request: RequestForMethod<'heartbeat'>) => {
|
||||
return {
|
||||
timestamp: Date.now(),
|
||||
status: this.isShuttingDown ? 'degraded' : 'healthy',
|
||||
};
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
26
ts/shared/protocol/error.codes.ts
Normal file
26
ts/shared/protocol/error.codes.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
/**
|
||||
* Standardized error codes for IPC communication
|
||||
* These are used instead of string messages for better error handling
|
||||
*/
|
||||
export enum ErrorCode {
|
||||
// General errors
|
||||
UNKNOWN_ERROR = 'UNKNOWN_ERROR',
|
||||
INVALID_REQUEST = 'INVALID_REQUEST',
|
||||
|
||||
// Process errors
|
||||
PROCESS_NOT_FOUND = 'PROCESS_NOT_FOUND',
|
||||
PROCESS_ALREADY_EXISTS = 'PROCESS_ALREADY_EXISTS',
|
||||
PROCESS_START_FAILED = 'PROCESS_START_FAILED',
|
||||
PROCESS_STOP_FAILED = 'PROCESS_STOP_FAILED',
|
||||
|
||||
// Daemon errors
|
||||
DAEMON_NOT_RUNNING = 'DAEMON_NOT_RUNNING',
|
||||
DAEMON_ALREADY_RUNNING = 'DAEMON_ALREADY_RUNNING',
|
||||
|
||||
// Memory errors
|
||||
MEMORY_LIMIT_EXCEEDED = 'MEMORY_LIMIT_EXCEEDED',
|
||||
|
||||
// Config errors
|
||||
CONFIG_INVALID = 'CONFIG_INVALID',
|
||||
CONFIG_SAVE_FAILED = 'CONFIG_SAVE_FAILED',
|
||||
}
|
@@ -1,7 +1,4 @@
|
||||
import type {
|
||||
IProcessConfig,
|
||||
IProcessInfo,
|
||||
} from './classes.tspm.js';
|
||||
import type { IProcessConfig, IProcessInfo } from './classes.tspm.js';
|
||||
import type { IProcessLog } from './classes.processwrapper.js';
|
||||
|
||||
// Base message types
|
5
ts/shared/protocol/protocol.version.ts
Normal file
5
ts/shared/protocol/protocol.version.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
/**
|
||||
* Protocol version for client-daemon communication
|
||||
* This allows for version compatibility checks between client and daemon
|
||||
*/
|
||||
export const PROTOCOL_VERSION = '1.0.0';
|
Reference in New Issue
Block a user