BREAKING CHANGE(daemon): Refactor daemon lifecycle and service management: remove IPC auto-spawn, add TspmServiceManager and CLI enable/disable

This commit is contained in:
2025-08-28 10:39:35 +00:00
parent 35b6a6a8d0
commit d33a001edc
8 changed files with 291 additions and 133 deletions

View File

@@ -1,56 +1,48 @@
# TSPM Real-Time Log Streaming Implementation Plan
# TSPM SmartDaemon Service Management Refactor
## Overview
Implementing real-time log streaming (tailing) functionality for TSPM using SmartIPC's pub/sub capabilities.
## 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.
## Approach: Hybrid Request + Subscribe
1. Initial getLogs request to fetch historical logs up to current point
2. Subscribe to pub/sub channel for real-time updates
3. Use sequence numbers to detect and handle gaps/duplicates
4. Per-process topics for granular subscriptions
## Solution
Refactor to use SmartDaemon for proper systemd service integration.
## Implementation Tasks
### Core Changes
- [x] Update IProcessLog interface with seq and runId fields
- [x] Add nextSeq and runId fields to ProcessWrapper class
- [x] Update addLog() methods to include sequencing
- [x] Implement pub/sub publishing in daemon
### 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
### IPC Client Updates
- [x] Add subscribe/unsubscribe methods to TspmIpcClient
- [ ] Implement log streaming handler
- [ ] Add connection state management for subscriptions
### 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
### CLI Enhancement
- [x] Add --follow flag to logs command
- [x] Implement streaming output with proper formatting
- [x] Handle Ctrl+C gracefully to unsubscribe
### 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
### Reliability Features
- [x] Add backpressure handling (drop oldest when buffer full)
- [x] Implement gap detection and recovery
- [x] Add process restart detection via runId
### Phase 4: Update Documentation
- [x] Update help text in CLI
- [ ] Update command descriptions
- [x] Add service management section
### Testing
- [x] Test basic log streaming
- [x] Test gap recovery
- [x] Test high-volume logging scenarios
- [x] Test process restart handling
### 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
## Technical Details
### Sequence Numbering
- Each log entry gets incrementing seq number per process
- runId changes on process restart
- Client tracks lastSeq to detect gaps
### Topic Structure
- Format: `logs.<processId>`
- Daemon publishes to topic on new log entries
- Clients subscribe to specific process topics
### Backpressure Strategy
- Circular buffer of 10,000 entries per process
- Drop oldest entries when buffer full
- Client can detect gaps via sequence numbers
## 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