aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 8e2efc0cfa8c24b9f8d59d4f0e047e6d43689d9b (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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
use clap::{Parser, Subcommand, Args};
use std::fs::File;
use std::io::{Write, Read, Seek, SeekFrom};
use std::process::{Command, Stdio};
use std::thread;
use std::time::Duration;
use std::path::Path;
use notify::{Config, Event, EventKind, RecommendedWatcher, RecursiveMode, Watcher};
use std::sync::mpsc::channel;
use glob::glob;
use anyhow::{Result, Context};

#[derive(Parser)]
#[command(name = "demon")]
#[command(about = "A daemon process management CLI", long_about = None)]
#[command(version)]
#[command(propagate_version = true)]
struct Cli {
    #[command(subcommand)]
    command: Commands,
}

#[derive(Subcommand)]
enum Commands {
    /// Spawn a background process and redirect stdout/stderr to files
    Run(RunArgs),
    
    /// Stop a running daemon process
    Stop(StopArgs),
    
    /// Tail daemon logs in real-time
    Tail(TailArgs),
    
    /// Display daemon log contents
    Cat(CatArgs),
    
    /// List all running daemon processes
    List(ListArgs),
    
    /// Check status of a daemon process
    Status(StatusArgs),
    
    /// Clean up orphaned pid and log files
    Clean,
}

#[derive(Args)]
struct RunArgs {
    /// Process identifier
    #[arg(long)]
    id: String,
    
    /// Command and arguments to execute
    command: Vec<String>,
}

#[derive(Args)]
struct StopArgs {
    /// Process identifier
    #[arg(long)]
    id: String,
    
    /// Timeout in seconds before sending SIGKILL after SIGTERM
    #[arg(long, default_value = "10")]
    timeout: u64,
}

#[derive(Args)]
struct TailArgs {
    /// Process identifier
    #[arg(long)]
    id: String,
    
    /// Only tail stdout
    #[arg(long)]
    stdout: bool,
    
    /// Only tail stderr
    #[arg(long)]
    stderr: bool,
}

#[derive(Args)]
struct CatArgs {
    /// Process identifier
    #[arg(long)]
    id: String,
    
    /// Only show stdout
    #[arg(long)]
    stdout: bool,
    
    /// Only show stderr
    #[arg(long)]
    stderr: bool,
}

#[derive(Args)]
struct ListArgs {
    /// Quiet mode - output only process data without headers
    #[arg(short, long)]
    quiet: bool,
}

#[derive(Args)]
struct StatusArgs {
    /// Process identifier
    #[arg(long)]
    id: String,
}

fn main() {
    tracing_subscriber::fmt()
        .with_writer(std::io::stderr)
        .init();

    let cli = Cli::parse();
    
    if let Err(e) = run_command(cli.command) {
        tracing::error!("Error: {}", e);
        std::process::exit(1);
    }
}

fn run_command(command: Commands) -> Result<()> {
    match command {
        Commands::Run(args) => {
            if args.command.is_empty() {
                return Err(anyhow::anyhow!("Command cannot be empty"));
            }
            run_daemon(&args.id, &args.command)
        }
        Commands::Stop(args) => {
            stop_daemon(&args.id, args.timeout)
        }
        Commands::Tail(args) => {
            let show_stdout = !args.stderr || args.stdout;
            let show_stderr = !args.stdout || args.stderr;
            tail_logs(&args.id, show_stdout, show_stderr)
        }
        Commands::Cat(args) => {
            let show_stdout = !args.stderr || args.stdout;
            let show_stderr = !args.stdout || args.stderr;
            cat_logs(&args.id, show_stdout, show_stderr)
        }
        Commands::List(args) => {
            list_daemons(args.quiet)
        }
        Commands::Status(args) => {
            status_daemon(&args.id)
        }
        Commands::Clean => {
            clean_orphaned_files()
        }
    }
}

fn run_daemon(id: &str, command: &[String]) -> Result<()> {
    let pid_file = format!("{}.pid", id);
    let stdout_file = format!("{}.stdout", id);
    let stderr_file = format!("{}.stderr", id);
    
    // Check if process is already running
    if is_process_running(&pid_file)? {
        return Err(anyhow::anyhow!("Process '{}' is already running", id));
    }
    
    tracing::info!("Starting daemon '{}' with command: {:?}", id, command);
    
    // Truncate/create output files
    File::create(&stdout_file)?;
    File::create(&stderr_file)?;
    
    // Open files for redirection
    let stdout_redirect = File::create(&stdout_file)?;
    let stderr_redirect = File::create(&stderr_file)?;
    
    // Spawn the process
    let program = &command[0];
    let args = if command.len() > 1 { &command[1..] } else { &[] };
    
    let child = Command::new(program)
        .args(args)
        .stdout(Stdio::from(stdout_redirect))
        .stderr(Stdio::from(stderr_redirect))
        .stdin(Stdio::null())
        .spawn()
        .with_context(|| format!("Failed to start process '{}' with args {:?}", program, args))?;
    
    // Write PID to file
    let mut pid_file_handle = File::create(&pid_file)?;
    writeln!(pid_file_handle, "{}", child.id())?;
    
    // Don't wait for the child - let it run detached
    std::mem::forget(child);
    
    println!("Started daemon '{}' with PID written to {}", id, pid_file);
    
    Ok(())
}

fn is_process_running(pid_file: &str) -> Result<bool> {
    // Try to read the PID file
    let mut file = match File::open(pid_file) {
        Ok(f) => f,
        Err(_) => return Ok(false), // No PID file means no running process
    };
    
    let mut contents = String::new();
    file.read_to_string(&mut contents)?;
    
    let pid: u32 = match contents.trim().parse() {
        Ok(p) => p,
        Err(_) => return Ok(false), // Invalid PID file
    };
    
    // Check if process is still running using kill -0
    let output = Command::new("kill")
        .args(&["-0", &pid.to_string()])
        .output()?;
    
    Ok(output.status.success())
}

fn stop_daemon(id: &str, timeout: u64) -> Result<()> {
    let pid_file = format!("{}.pid", id);
    
    // Check if PID file exists
    let mut file = match File::open(&pid_file) {
        Ok(f) => f,
        Err(_) => {
            println!("Process '{}' is not running (no PID file found)", id);
            return Ok(());
        }
    };
    
    // Read PID
    let mut contents = String::new();
    file.read_to_string(&mut contents)?;
    
    let pid: u32 = match contents.trim().parse() {
        Ok(p) => p,
        Err(_) => {
            println!("Process '{}': invalid PID file, removing it", id);
            std::fs::remove_file(&pid_file)?;
            return Ok(());
        }
    };
    
    tracing::info!("Stopping daemon '{}' (PID: {}) with timeout {}s", id, pid, timeout);
    
    // Check if process is running
    if !is_process_running_by_pid(pid) {
        println!("Process '{}' (PID: {}) is not running, cleaning up PID file", id, pid);
        std::fs::remove_file(&pid_file)?;
        return Ok(());
    }
    
    // Send SIGTERM
    tracing::info!("Sending SIGTERM to PID {}", pid);
    let output = Command::new("kill")
        .args(&["-TERM", &pid.to_string()])
        .output()?;
    
    if !output.status.success() {
        return Err(anyhow::anyhow!("Failed to send SIGTERM to PID {}", pid));
    }
    
    // Wait for the process to terminate
    for i in 0..timeout {
        if !is_process_running_by_pid(pid) {
            println!("Process '{}' (PID: {}) terminated gracefully", id, pid);
            std::fs::remove_file(&pid_file)?;
            return Ok(());
        }
        
        if i == 0 {
            tracing::info!("Waiting for process to terminate gracefully...");
        }
        
        thread::sleep(Duration::from_secs(1));
    }
    
    // Process didn't terminate, send SIGKILL
    tracing::warn!("Process {} didn't terminate after {}s, sending SIGKILL", pid, timeout);
    let output = Command::new("kill")
        .args(&["-KILL", &pid.to_string()])
        .output()?;
    
    if !output.status.success() {
        return Err(anyhow::anyhow!("Failed to send SIGKILL to PID {}", pid));
    }
    
    // Wait a bit more for SIGKILL to take effect
    thread::sleep(Duration::from_secs(1));
    
    if is_process_running_by_pid(pid) {
        return Err(anyhow::anyhow!("Process {} is still running after SIGKILL", pid));
    }
    
    println!("Process '{}' (PID: {}) terminated forcefully", id, pid);
    std::fs::remove_file(&pid_file)?;
    
    Ok(())
}

fn is_process_running_by_pid(pid: u32) -> bool {
    let output = Command::new("kill")
        .args(&["-0", &pid.to_string()])
        .output();
    
    match output {
        Ok(output) => output.status.success(),
        Err(_) => false,
    }
}

fn cat_logs(id: &str, show_stdout: bool, show_stderr: bool) -> Result<()> {
    let stdout_file = format!("{}.stdout", id);
    let stderr_file = format!("{}.stderr", id);
    
    let mut files_found = false;
    
    if show_stdout {
        if let Ok(contents) = std::fs::read_to_string(&stdout_file) {
            if !contents.is_empty() {
                files_found = true;
                if show_stderr {
                    println!("==> {} <==", stdout_file);
                }
                print!("{}", contents);
            }
        } else {
            tracing::warn!("Could not read {}", stdout_file);
        }
    }
    
    if show_stderr {
        if let Ok(contents) = std::fs::read_to_string(&stderr_file) {
            if !contents.is_empty() {
                files_found = true;
                if show_stdout {
                    println!("==> {} <==", stderr_file);
                }
                print!("{}", contents);
            }
        } else {
            tracing::warn!("Could not read {}", stderr_file);
        }
    }
    
    if !files_found {
        println!("No log files found for daemon '{}'", id);
    }
    
    Ok(())
}

fn tail_logs(id: &str, show_stdout: bool, show_stderr: bool) -> Result<()> {
    let stdout_file = format!("{}.stdout", id);
    let stderr_file = format!("{}.stderr", id);
    
    // First, display existing content and set up initial positions
    let mut file_positions: std::collections::HashMap<String, u64> = std::collections::HashMap::new();
    
    if show_stdout && Path::new(&stdout_file).exists() {
        let mut file = File::open(&stdout_file)?;
        let initial_content = read_file_content(&mut file)?;
        if !initial_content.is_empty() {
            if show_stderr {
                println!("==> {} <==", stdout_file);
            }
            print!("{}", initial_content);
        }
        let position = file.seek(SeekFrom::Current(0))?;
        file_positions.insert(stdout_file.clone(), position);
    }
    
    if show_stderr && Path::new(&stderr_file).exists() {
        let mut file = File::open(&stderr_file)?;
        let initial_content = read_file_content(&mut file)?;
        if !initial_content.is_empty() {
            if show_stdout && file_positions.len() > 0 {
                println!("\n==> {} <==", stderr_file);
            } else if show_stdout {
                println!("==> {} <==", stderr_file);
            }
            print!("{}", initial_content);
        }
        let position = file.seek(SeekFrom::Current(0))?;
        file_positions.insert(stderr_file.clone(), position);
    }
    
    if file_positions.is_empty() {
        println!("No log files found for daemon '{}'. Watching for new files...", id);
    }
    
    tracing::info!("Watching for changes to log files... Press Ctrl+C to stop.");
    
    // Set up file watcher
    let (tx, rx) = channel();
    let mut watcher = RecommendedWatcher::new(tx, Config::default())?;
    
    // Watch the current directory for new files and changes
    watcher.watch(Path::new("."), RecursiveMode::NonRecursive)?;
    
    // Handle Ctrl+C gracefully
    let running = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(true));
    let r = running.clone();
    
    ctrlc::set_handler(move || {
        r.store(false, std::sync::atomic::Ordering::SeqCst);
    })?;
    
    while running.load(std::sync::atomic::Ordering::SeqCst) {
        match rx.recv_timeout(Duration::from_millis(100)) {
            Ok(res) => {
                match res {
                    Ok(Event {
                        kind: EventKind::Modify(_),
                        paths,
                        ..
                    }) => {
                        for path in paths {
                            let path_str = path.to_string_lossy().to_string();
                            
                            if (show_stdout && path_str == stdout_file) || 
                               (show_stderr && path_str == stderr_file) {
                                
                                if let Err(e) = handle_file_change(&path_str, &mut file_positions, show_stdout && show_stderr) {
                                    tracing::error!("Error handling file change: {}", e);
                                }
                            }
                        }
                    }
                    Ok(Event {
                        kind: EventKind::Create(_),
                        paths,
                        ..
                    }) => {
                        // Handle file creation
                        for path in paths {
                            let path_str = path.to_string_lossy().to_string();
                            
                            if (show_stdout && path_str == stdout_file) || 
                               (show_stderr && path_str == stderr_file) {
                                
                                tracing::info!("New file detected: {}", path_str);
                                file_positions.insert(path_str.clone(), 0);
                                
                                if let Err(e) = handle_file_change(&path_str, &mut file_positions, show_stdout && show_stderr) {
                                    tracing::error!("Error handling new file: {}", e);
                                }
                            }
                        }
                    }
                    Ok(_) => {} // Ignore other events
                    Err(e) => tracing::error!("Watch error: {:?}", e),
                }
            }
            Err(std::sync::mpsc::RecvTimeoutError::Timeout) => {
                // Timeout is normal, just continue
            }
            Err(e) => {
                tracing::error!("Receive error: {}", e);
                break;
            }
        }
    }
    
    println!("\nTailing stopped.");
    Ok(())
}

fn read_file_content(file: &mut File) -> Result<String> {
    let mut content = String::new();
    file.read_to_string(&mut content)?;
    Ok(content)
}

fn handle_file_change(
    file_path: &str, 
    positions: &mut std::collections::HashMap<String, u64>,
    show_headers: bool
) -> Result<()> {
    let mut file = File::open(file_path)?;
    let current_pos = positions.get(file_path).copied().unwrap_or(0);
    
    // Seek to the last read position
    file.seek(SeekFrom::Start(current_pos))?;
    
    // Read new content
    let mut new_content = String::new();
    file.read_to_string(&mut new_content)?;
    
    if !new_content.is_empty() {
        if show_headers {
            println!("==> {} <==", file_path);
        }
        print!("{}", new_content);
        std::io::Write::flush(&mut std::io::stdout())?;
        
        // Update position
        let new_pos = file.seek(SeekFrom::Current(0))?;
        positions.insert(file_path.to_string(), new_pos);
    }
    
    Ok(())
}

fn list_daemons(quiet: bool) -> Result<()> {
    if !quiet {
        println!("{:<20} {:<8} {:<10} {}", "ID", "PID", "STATUS", "COMMAND");
        println!("{}", "-".repeat(50));
    }
    
    let mut found_any = false;
    
    // Find all .pid files in current directory
    for entry in glob("*.pid")? {
        match entry {
            Ok(path) => {
                found_any = true;
                let path_str = path.to_string_lossy();
                
                // Extract ID from filename (remove .pid extension)
                let id = path_str.strip_suffix(".pid").unwrap_or(&path_str);
                
                // Read PID from file
                match std::fs::read_to_string(&path) {
                    Ok(contents) => {
                        let pid_str = contents.trim();
                        match pid_str.parse::<u32>() {
                            Ok(pid) => {
                                let status = if is_process_running_by_pid(pid) {
                                    "RUNNING"
                                } else {
                                    "DEAD"
                                };
                                
                                if quiet {
                                    println!("{}:{}:{}", id, pid, status);
                                } else {
                                    // Try to read command from a hypothetical command file
                                    // For now, we'll just show "N/A" since we don't store the command
                                    let command = "N/A";
                                    println!("{:<20} {:<8} {:<10} {}", id, pid, status, command);
                                }
                            }
                            Err(_) => {
                                if quiet {
                                    println!("{}:INVALID:ERROR", id);
                                } else {
                                    println!("{:<20} {:<8} {:<10} {}", id, "INVALID", "ERROR", "Invalid PID file");
                                }
                            }
                        }
                    }
                    Err(e) => {
                        if quiet {
                            println!("{}:ERROR:ERROR", id);
                        } else {
                            println!("{:<20} {:<8} {:<10} {}", id, "ERROR", "ERROR", format!("Cannot read: {}", e));
                        }
                    }
                }
            }
            Err(e) => {
                tracing::warn!("Error reading glob entry: {}", e);
            }
        }
    }
    
    if !found_any && !quiet {
        println!("No daemon processes found.");
    }
    
    Ok(())
}

fn status_daemon(id: &str) -> Result<()> {
    let pid_file = format!("{}.pid", id);
    let stdout_file = format!("{}.stdout", id);
    let stderr_file = format!("{}.stderr", id);
    
    println!("Daemon: {}", id);
    println!("PID file: {}", pid_file);
    
    // Check if PID file exists
    if !Path::new(&pid_file).exists() {
        println!("Status: NOT FOUND (no PID file)");
        return Ok(());
    }
    
    // Read PID from file
    match std::fs::read_to_string(&pid_file) {
        Ok(contents) => {
            let pid_str = contents.trim();
            match pid_str.parse::<u32>() {
                Ok(pid) => {
                    println!("PID: {}", pid);
                    
                    if is_process_running_by_pid(pid) {
                        println!("Status: RUNNING");
                        
                        // Show file information
                        if Path::new(&stdout_file).exists() {
                            let metadata = std::fs::metadata(&stdout_file)?;
                            println!("Stdout file: {} ({} bytes)", stdout_file, metadata.len());
                        } else {
                            println!("Stdout file: {} (not found)", stdout_file);
                        }
                        
                        if Path::new(&stderr_file).exists() {
                            let metadata = std::fs::metadata(&stderr_file)?;
                            println!("Stderr file: {} ({} bytes)", stderr_file, metadata.len());
                        } else {
                            println!("Stderr file: {} (not found)", stderr_file);
                        }
                    } else {
                        println!("Status: DEAD (process not running)");
                        println!("Note: Use 'demon clean' to remove orphaned files");
                    }
                }
                Err(_) => {
                    println!("Status: ERROR (invalid PID in file)");
                }
            }
        }
        Err(e) => {
            println!("Status: ERROR (cannot read PID file: {})", e);
        }
    }
    
    Ok(())
}

fn clean_orphaned_files() -> Result<()> {
    tracing::info!("Scanning for orphaned daemon files...");
    
    let mut cleaned_count = 0;
    
    // Find all .pid files in current directory
    for entry in glob("*.pid")? {
        match entry {
            Ok(path) => {
                let path_str = path.to_string_lossy();
                let id = path_str.strip_suffix(".pid").unwrap_or(&path_str);
                
                // Read PID from file
                match std::fs::read_to_string(&path) {
                    Ok(contents) => {
                        let pid_str = contents.trim();
                        match pid_str.parse::<u32>() {
                            Ok(pid) => {
                                // Check if process is still running
                                if !is_process_running_by_pid(pid) {
                                    println!("Cleaning up orphaned files for '{}' (PID: {})", id, pid);
                                    
                                    // Remove PID file
                                    if let Err(e) = std::fs::remove_file(&path) {
                                        tracing::warn!("Failed to remove {}: {}", path_str, e);
                                    } else {
                                        tracing::info!("Removed {}", path_str);
                                    }
                                    
                                    // Remove stdout file if it exists
                                    let stdout_file = format!("{}.stdout", id);
                                    if Path::new(&stdout_file).exists() {
                                        if let Err(e) = std::fs::remove_file(&stdout_file) {
                                            tracing::warn!("Failed to remove {}: {}", stdout_file, e);
                                        } else {
                                            tracing::info!("Removed {}", stdout_file);
                                        }
                                    }
                                    
                                    // Remove stderr file if it exists
                                    let stderr_file = format!("{}.stderr", id);
                                    if Path::new(&stderr_file).exists() {
                                        if let Err(e) = std::fs::remove_file(&stderr_file) {
                                            tracing::warn!("Failed to remove {}: {}", stderr_file, e);
                                        } else {
                                            tracing::info!("Removed {}", stderr_file);
                                        }
                                    }
                                    
                                    cleaned_count += 1;
                                } else {
                                    tracing::info!("Skipping '{}' (PID: {}) - process is still running", id, pid);
                                }
                            }
                            Err(_) => {
                                println!("Cleaning up invalid PID file: {}", path_str);
                                if let Err(e) = std::fs::remove_file(&path) {
                                    tracing::warn!("Failed to remove invalid PID file {}: {}", path_str, e);
                                } else {
                                    tracing::info!("Removed invalid PID file {}", path_str);
                                    cleaned_count += 1;
                                }
                            }
                        }
                    }
                    Err(_) => {
                        println!("Cleaning up unreadable PID file: {}", path_str);
                        if let Err(e) = std::fs::remove_file(&path) {
                            tracing::warn!("Failed to remove unreadable PID file {}: {}", path_str, e);
                        } else {
                            tracing::info!("Removed unreadable PID file {}", path_str);
                            cleaned_count += 1;
                        }
                    }
                }
            }
            Err(e) => {
                tracing::warn!("Error reading glob entry: {}", e);
            }
        }
    }
    
    if cleaned_count == 0 {
        println!("No orphaned files found.");
    } else {
        println!("Cleaned up {} orphaned daemon(s).", cleaned_count);
    }
    
    Ok(())
}