about summary refs log tree commit diff stats
path: root/src/server/main.rs
blob: b769fbf23277b607082feda61d8b92c72e66e52e (plain) (blame)
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
use clap::Parser;
use salaryman::service::{Service, ServiceConf, ServiceState};
use serde::{Deserialize, Serialize};
use std::{
    fs::read_to_string,
    os::unix::net::{UnixListener, UnixStream},
    path::PathBuf,
};
use rayon::prelude::*;

#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
struct Args {
    #[arg(
        short,
        long,
        value_name = "FILE",
        help = "config file override",
        default_value = "salaryman.toml"
    )]
    config: PathBuf,
    #[arg(
        short,
        long,
        value_name = "SOCK",
        help = "UNIX socket to bind",
        default_value = "/tmp/salaryman.sock"
    )]
    socket: PathBuf,
}

pub enum ServiceReq {
    Create(ServiceConf),
}

#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct Config {
    pub socket: Option<PathBuf>,
    pub service: Vec<ServiceConf>,
}
impl Config {
    pub fn new() -> Self {
        Self {
            socket: None,
            service: Vec::new(),
        }
    }
}

fn load_config(file: &PathBuf) -> Result<Config, Box<dyn std::error::Error>> {
    let s: String = match read_to_string(file) {
        Ok(s) => s,
        Err(_) => {
            return Err(Box::new(std::io::Error::new(
                std::io::ErrorKind::NotFound,
                "cannot find config file",
            )));
        }
    };
    match toml::from_str(s.as_str()) {
        Ok(c) => Ok(c),
        Err(_) => Err(Box::new(std::io::Error::new(
            std::io::ErrorKind::Other,
            "unable to parse config file",
        ))),
    }
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let args = Args::parse();
    let conf: Config = load_config(&args.config)?;
    let _sockaddr = if let Some(sock) = conf.socket {
        sock
    } else {
        args.socket
    };
    let mut services: Vec<Service> = Vec::new();
    for service in conf.service {
        services.push(service.build()?);
    }
    loop {
        services.par_iter_mut()
            .for_each(|service| {
                match service.state().expect("unable to get service state") {
                    ServiceState::Failed => service.restart().expect("unable to restart service"),
                    ServiceState::Stopped => (),
                    _ => (),
                }
            });
        services.push(Service::new());
        std::thread::sleep(std::time::Duration::from_millis(100));
    }
}