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
pub mod file;
mod process;

pub use self::process::ProcessData;
pub use self::process::Process;

use std::fs;

pub fn start(process: &mut Process) {
  println!("Start monitoring");
  if let Ok(mut child) = process.spawn() {
    if let Ok(status) = child.wait() {
      match status.code() {
        Some(code) => {
          if code > 0 {
            println!("Child process ended with errors, retry!");
            process.increment_retries();
            
            if process.should_retry() {
              start(process);
            } else {
              println!("Too many retries, stopping!");
            }
          } else {
            println!("Child process ended with no errors.");
          }
        },
        None => {
          println!("Child process closed by signals. Stopping.");
        }
      }
    } else {
      println!("Process was not started");
    }
  }
}

pub fn active_processes() -> Vec<Process> {
  let base_path = file::procs_path();
  
  fs::read_dir(base_path).unwrap().map(|entry| {
    let file = entry.unwrap();
    let path = format!("{}", file.path().to_str().unwrap());
    
    file::read(path).unwrap()
  }).filter(|p| { p.is_active() }).collect()
}