aboutsummaryrefslogtreecommitdiff
path: root/IMPLEMENTATION_PLAN.md
blob: eaa1e64dae440edb4b25122ffdd015f78bc2cf74 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# Demon CLI Implementation Plan

## Project Overview
A CLI tool named `demon` for spawning and managing background processes with stdout/stderr redirection.

## Requirements Summary
- **Files created**: `<id>.pid`, `<id>.stdout`, `<id>.stderr` in working directory
- **Platform**: Linux only (maybe macOS later)
- **File location**: Working directory (add to .gitignore)
- **Signal handling**: SIGTERM then SIGKILL with configurable timeout
- **Logging**: Tool logs to stderr, process output to stdout
- **Concurrency**: Single process tail for now

## CLI Structure
```
demon run --id <identifier> <command...>
demon stop --id <id> [--timeout <seconds>]
demon tail [--stdout] [--stderr] --id <id>
demon cat [--stdout] [--stderr] --id <id>
demon list
demon status --id <id>
demon clean
```

## Implementation Progress

### ✅ Phase 1: Project Setup
- [x] Add dependencies: clap, tracing, tracing-subscriber, notify
- [x] Create CLI structure with Commands enum and Args structs

### 🔄 Phase 2: Core Process Management
- [ ] **CURRENT**: Implement `demon run`
  - Check if process already running via PID file
  - Spawn process with stdout/stderr redirection to files
  - Write PID to `.pid` file
  - Truncate log files when starting new process
  - Detach process so parent can exit
- [ ] Implement `demon stop`
  - Read PID from file
  - Send SIGTERM first
  - Wait for timeout, then send SIGKILL
  - Clean up PID file
  - Handle already-dead processes gracefully

### 📋 Phase 3: File Operations
- [ ] Implement `demon cat`
  - Read and display `.stdout` and/or `.stderr` files
  - Handle file selection flags properly
  - Error handling for missing files
- [ ] Implement `demon tail`
  - Use `notify` crate for file watching
  - Support both stdout and stderr simultaneously
  - Handle file rotation/truncation
  - Clean shutdown on Ctrl+C

### 📋 Phase 4: Additional Commands
- [ ] Implement `demon list`
  - Scan working directory for `.pid` files
  - Check which processes are actually running
  - Display process info
- [ ] Implement `demon status`
  - Check if specific process is running
  - Display process info
- [ ] Implement `demon clean`
  - Find orphaned files (PID exists but process dead)
  - Remove orphaned `.pid`, `.stdout`, `.stderr` files

### 📋 Phase 5: Error Handling & Polish
- [ ] Robust error handling throughout
- [ ] Proper cleanup on failures
- [ ] Input validation
- [ ] Help text and documentation

## Technical Implementation Details

### Process Spawning (demon run)
```rust
// 1. Check if <id>.pid exists and process is running
// 2. Truncate/create <id>.stdout and <id>.stderr files
// 3. Spawn process with:
//    - stdout redirected to <id>.stdout
//    - stderr redirected to <id>.stderr
//    - stdin redirected to /dev/null
// 4. Write PID to <id>.pid file
// 5. Don't call .wait() - let process run detached
```

### Process Stopping (demon stop)
```rust
// 1. Read PID from <id>.pid file
// 2. Send SIGTERM to process
// 3. Wait for timeout (default 10s)
// 4. If still running, send SIGKILL
// 5. Remove <id>.pid file
// 6. Handle process already dead gracefully
```

### File Tailing (demon tail)
```rust
// 1. Use notify crate to watch file changes
// 2. When files change, read new content and print
// 3. Handle both stdout and stderr based on flags
// 4. Default: show both if neither flag specified
// 5. Graceful shutdown on Ctrl+C
```

### File Listing (demon list)
```rust
// 1. Glob for *.pid files in current directory
// 2. For each PID file, check if process is running
// 3. Display: ID, PID, Status, Command (if available)
```

## Dependencies Used
- `clap` (derive feature) - CLI argument parsing
- `tracing` + `tracing-subscriber` - Structured logging
- `notify` - File system notifications for tail
- Standard library for process management

## File Naming Convention
- PID file: `<id>.pid`
- Stdout log: `<id>.stdout`  
- Stderr log: `<id>.stderr`

## Error Handling Strategy
- Use `Result<(), Box<dyn std::error::Error>>` for main functions
- Log errors using `tracing::error!`
- Exit with code 1 on errors
- Provide descriptive error messages

## Testing Strategy
- Manual testing with simple commands (sleep, echo, etc.)
- Test edge cases: process crashes, missing files, etc.
- Test signal handling and cleanup

## Current Status
- ✅ All core functionality implemented and tested
- ✅ CLI structure with proper subcommands and arguments
- ✅ Process spawning and management working correctly
- ✅ File watching and real-time tailing functional
- ✅ Error handling and edge cases covered
- ✅ Clean up functionality for orphaned files

## Implementation Complete!

All planned features have been successfully implemented:

1. **`demon run`** - ✅ Spawns background processes with file redirection
2. **`demon stop`** - ✅ Graceful termination with SIGTERM/SIGKILL timeout
3. **`demon tail`** - ✅ Real-time file watching with notify crate
4. **`demon cat`** - ✅ Display log file contents
5. **`demon list`** - ✅ Show all managed processes with status
6. **`demon status`** - ✅ Detailed status of specific process
7. **`demon clean`** - ✅ Remove orphaned files from dead processes

## Testing Summary

All commands have been tested and work correctly:
- Process spawning and detachment
- Signal handling (SIGTERM → SIGKILL)
- File redirection (stdout/stderr)
- Duplicate process detection
- File watching and real-time updates
- Orphan cleanup
- Error handling for edge cases

## Final Architecture

The implementation follows the planned modular structure:
- **CLI Interface**: Uses clap with enum-based subcommands ✅
- **Process Manager**: Handles spawning, tracking, and termination ✅
- **File Operations**: Manages PID files and log redirection ✅
- **Output Display**: Implements both cat and tail functionality ✅

The tool is ready for production use!