diff options
| author | diogo464 <[email protected]> | 2025-06-19 08:52:20 +0100 |
|---|---|---|
| committer | diogo464 <[email protected]> | 2025-06-19 08:52:20 +0100 |
| commit | 39b3d9bfd499e131fd8a9bd1bf0021b62ec18c53 (patch) | |
| tree | 9975c7d92f28ed19edc370c7e11473f56334629c /IMPLEMENTATION_PLAN.md | |
Initial implementation of demon CLI tool
Implement complete daemon process management CLI with the following features:
- demon run: spawn background processes with stdout/stderr redirection
- demon stop: graceful process termination with SIGTERM/SIGKILL timeout
- demon tail: real-time file watching and log tailing
- demon cat: display log file contents
- demon list: show all managed processes with status
- demon status: detailed process information
- demon clean: remove orphaned files from dead processes
Technical implementation:
- Uses clap for CLI with enum-based subcommands
- Structured logging with tracing crate
- File watching with notify crate for efficient tailing
- Process management with proper signal handling
- Creates .pid, .stdout, .stderr files in working directory
- Comprehensive error handling and edge case coverage
🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <[email protected]>
Diffstat (limited to 'IMPLEMENTATION_PLAN.md')
| -rw-r--r-- | IMPLEMENTATION_PLAN.md | 175 |
1 files changed, 175 insertions, 0 deletions
diff --git a/IMPLEMENTATION_PLAN.md b/IMPLEMENTATION_PLAN.md new file mode 100644 index 0000000..eaa1e64 --- /dev/null +++ b/IMPLEMENTATION_PLAN.md | |||
| @@ -0,0 +1,175 @@ | |||
| 1 | # Demon CLI Implementation Plan | ||
| 2 | |||
| 3 | ## Project Overview | ||
| 4 | A CLI tool named `demon` for spawning and managing background processes with stdout/stderr redirection. | ||
| 5 | |||
| 6 | ## Requirements Summary | ||
| 7 | - **Files created**: `<id>.pid`, `<id>.stdout`, `<id>.stderr` in working directory | ||
| 8 | - **Platform**: Linux only (maybe macOS later) | ||
| 9 | - **File location**: Working directory (add to .gitignore) | ||
| 10 | - **Signal handling**: SIGTERM then SIGKILL with configurable timeout | ||
| 11 | - **Logging**: Tool logs to stderr, process output to stdout | ||
| 12 | - **Concurrency**: Single process tail for now | ||
| 13 | |||
| 14 | ## CLI Structure | ||
| 15 | ``` | ||
| 16 | demon run --id <identifier> <command...> | ||
| 17 | demon stop --id <id> [--timeout <seconds>] | ||
| 18 | demon tail [--stdout] [--stderr] --id <id> | ||
| 19 | demon cat [--stdout] [--stderr] --id <id> | ||
| 20 | demon list | ||
| 21 | demon status --id <id> | ||
| 22 | demon clean | ||
| 23 | ``` | ||
| 24 | |||
| 25 | ## Implementation Progress | ||
| 26 | |||
| 27 | ### ✅ Phase 1: Project Setup | ||
| 28 | - [x] Add dependencies: clap, tracing, tracing-subscriber, notify | ||
| 29 | - [x] Create CLI structure with Commands enum and Args structs | ||
| 30 | |||
| 31 | ### 🔄 Phase 2: Core Process Management | ||
| 32 | - [ ] **CURRENT**: Implement `demon run` | ||
| 33 | - Check if process already running via PID file | ||
| 34 | - Spawn process with stdout/stderr redirection to files | ||
| 35 | - Write PID to `.pid` file | ||
| 36 | - Truncate log files when starting new process | ||
| 37 | - Detach process so parent can exit | ||
| 38 | - [ ] Implement `demon stop` | ||
| 39 | - Read PID from file | ||
| 40 | - Send SIGTERM first | ||
| 41 | - Wait for timeout, then send SIGKILL | ||
| 42 | - Clean up PID file | ||
| 43 | - Handle already-dead processes gracefully | ||
| 44 | |||
| 45 | ### 📋 Phase 3: File Operations | ||
| 46 | - [ ] Implement `demon cat` | ||
| 47 | - Read and display `.stdout` and/or `.stderr` files | ||
| 48 | - Handle file selection flags properly | ||
| 49 | - Error handling for missing files | ||
| 50 | - [ ] Implement `demon tail` | ||
| 51 | - Use `notify` crate for file watching | ||
| 52 | - Support both stdout and stderr simultaneously | ||
| 53 | - Handle file rotation/truncation | ||
| 54 | - Clean shutdown on Ctrl+C | ||
| 55 | |||
| 56 | ### 📋 Phase 4: Additional Commands | ||
| 57 | - [ ] Implement `demon list` | ||
| 58 | - Scan working directory for `.pid` files | ||
| 59 | - Check which processes are actually running | ||
| 60 | - Display process info | ||
| 61 | - [ ] Implement `demon status` | ||
| 62 | - Check if specific process is running | ||
| 63 | - Display process info | ||
| 64 | - [ ] Implement `demon clean` | ||
| 65 | - Find orphaned files (PID exists but process dead) | ||
| 66 | - Remove orphaned `.pid`, `.stdout`, `.stderr` files | ||
| 67 | |||
| 68 | ### 📋 Phase 5: Error Handling & Polish | ||
| 69 | - [ ] Robust error handling throughout | ||
| 70 | - [ ] Proper cleanup on failures | ||
| 71 | - [ ] Input validation | ||
| 72 | - [ ] Help text and documentation | ||
| 73 | |||
| 74 | ## Technical Implementation Details | ||
| 75 | |||
| 76 | ### Process Spawning (demon run) | ||
| 77 | ```rust | ||
| 78 | // 1. Check if <id>.pid exists and process is running | ||
| 79 | // 2. Truncate/create <id>.stdout and <id>.stderr files | ||
| 80 | // 3. Spawn process with: | ||
| 81 | // - stdout redirected to <id>.stdout | ||
| 82 | // - stderr redirected to <id>.stderr | ||
| 83 | // - stdin redirected to /dev/null | ||
| 84 | // 4. Write PID to <id>.pid file | ||
| 85 | // 5. Don't call .wait() - let process run detached | ||
| 86 | ``` | ||
| 87 | |||
| 88 | ### Process Stopping (demon stop) | ||
| 89 | ```rust | ||
| 90 | // 1. Read PID from <id>.pid file | ||
| 91 | // 2. Send SIGTERM to process | ||
| 92 | // 3. Wait for timeout (default 10s) | ||
| 93 | // 4. If still running, send SIGKILL | ||
| 94 | // 5. Remove <id>.pid file | ||
| 95 | // 6. Handle process already dead gracefully | ||
| 96 | ``` | ||
| 97 | |||
| 98 | ### File Tailing (demon tail) | ||
| 99 | ```rust | ||
| 100 | // 1. Use notify crate to watch file changes | ||
| 101 | // 2. When files change, read new content and print | ||
| 102 | // 3. Handle both stdout and stderr based on flags | ||
| 103 | // 4. Default: show both if neither flag specified | ||
| 104 | // 5. Graceful shutdown on Ctrl+C | ||
| 105 | ``` | ||
| 106 | |||
| 107 | ### File Listing (demon list) | ||
| 108 | ```rust | ||
| 109 | // 1. Glob for *.pid files in current directory | ||
| 110 | // 2. For each PID file, check if process is running | ||
| 111 | // 3. Display: ID, PID, Status, Command (if available) | ||
| 112 | ``` | ||
| 113 | |||
| 114 | ## Dependencies Used | ||
| 115 | - `clap` (derive feature) - CLI argument parsing | ||
| 116 | - `tracing` + `tracing-subscriber` - Structured logging | ||
| 117 | - `notify` - File system notifications for tail | ||
| 118 | - Standard library for process management | ||
| 119 | |||
| 120 | ## File Naming Convention | ||
| 121 | - PID file: `<id>.pid` | ||
| 122 | - Stdout log: `<id>.stdout` | ||
| 123 | - Stderr log: `<id>.stderr` | ||
| 124 | |||
| 125 | ## Error Handling Strategy | ||
| 126 | - Use `Result<(), Box<dyn std::error::Error>>` for main functions | ||
| 127 | - Log errors using `tracing::error!` | ||
| 128 | - Exit with code 1 on errors | ||
| 129 | - Provide descriptive error messages | ||
| 130 | |||
| 131 | ## Testing Strategy | ||
| 132 | - Manual testing with simple commands (sleep, echo, etc.) | ||
| 133 | - Test edge cases: process crashes, missing files, etc. | ||
| 134 | - Test signal handling and cleanup | ||
| 135 | |||
| 136 | ## Current Status | ||
| 137 | - ✅ All core functionality implemented and tested | ||
| 138 | - ✅ CLI structure with proper subcommands and arguments | ||
| 139 | - ✅ Process spawning and management working correctly | ||
| 140 | - ✅ File watching and real-time tailing functional | ||
| 141 | - ✅ Error handling and edge cases covered | ||
| 142 | - ✅ Clean up functionality for orphaned files | ||
| 143 | |||
| 144 | ## Implementation Complete! | ||
| 145 | |||
| 146 | All planned features have been successfully implemented: | ||
| 147 | |||
| 148 | 1. **`demon run`** - ✅ Spawns background processes with file redirection | ||
| 149 | 2. **`demon stop`** - ✅ Graceful termination with SIGTERM/SIGKILL timeout | ||
| 150 | 3. **`demon tail`** - ✅ Real-time file watching with notify crate | ||
| 151 | 4. **`demon cat`** - ✅ Display log file contents | ||
| 152 | 5. **`demon list`** - ✅ Show all managed processes with status | ||
| 153 | 6. **`demon status`** - ✅ Detailed status of specific process | ||
| 154 | 7. **`demon clean`** - ✅ Remove orphaned files from dead processes | ||
| 155 | |||
| 156 | ## Testing Summary | ||
| 157 | |||
| 158 | All commands have been tested and work correctly: | ||
| 159 | - Process spawning and detachment | ||
| 160 | - Signal handling (SIGTERM → SIGKILL) | ||
| 161 | - File redirection (stdout/stderr) | ||
| 162 | - Duplicate process detection | ||
| 163 | - File watching and real-time updates | ||
| 164 | - Orphan cleanup | ||
| 165 | - Error handling for edge cases | ||
| 166 | |||
| 167 | ## Final Architecture | ||
| 168 | |||
| 169 | The implementation follows the planned modular structure: | ||
| 170 | - **CLI Interface**: Uses clap with enum-based subcommands ✅ | ||
| 171 | - **Process Manager**: Handles spawning, tracking, and termination ✅ | ||
| 172 | - **File Operations**: Manages PID files and log redirection ✅ | ||
| 173 | - **Output Display**: Implements both cat and tail functionality ✅ | ||
| 174 | |||
| 175 | The tool is ready for production use! \ No newline at end of file | ||
