ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/salaryman/trunk/src/server/main.rs
Revision: 16
Committed: Tue Jul 29 09:26:15 2025 UTC (2 months, 1 week ago) by yuzu
File size: 2428 byte(s)
Log Message:
parallel process monitoring get

File Contents

# User Rev Content
1 yuzu 9 use clap::Parser;
2 yuzu 16 use salaryman::service::{Service, ServiceConf, ServiceState};
3 yuzu 9 use serde::{Deserialize, Serialize};
4 yuzu 14 use std::{
5 yuzu 16 fs::read_to_string,
6     os::unix::net::{UnixListener, UnixStream},
7 yuzu 14 path::PathBuf,
8     };
9 yuzu 16 use rayon::prelude::*;
10 yuzu 9
11     #[derive(Parser, Debug)]
12     #[command(version, about, long_about = None)]
13     struct Args {
14     #[arg(
15     short,
16     long,
17     value_name = "FILE",
18     help = "config file override",
19     default_value = "salaryman.toml"
20     )]
21     config: PathBuf,
22     #[arg(
23     short,
24     long,
25 yuzu 16 value_name = "SOCK",
26     help = "UNIX socket to bind",
27     default_value = "/tmp/salaryman.sock"
28 yuzu 9 )]
29 yuzu 16 socket: PathBuf,
30 yuzu 9 }
31    
32 yuzu 16 pub enum ServiceReq {
33     Create(ServiceConf),
34 yuzu 9 }
35    
36 yuzu 16 #[derive(Serialize, Deserialize, Clone, Debug)]
37 yuzu 13 pub struct Config {
38 yuzu 16 pub socket: Option<PathBuf>,
39 yuzu 13 pub service: Vec<ServiceConf>,
40     }
41     impl Config {
42     pub fn new() -> Self {
43     Self {
44 yuzu 16 socket: None,
45 yuzu 13 service: Vec::new(),
46     }
47     }
48     }
49    
50 yuzu 16 fn load_config(file: &PathBuf) -> Result<Config, Box<dyn std::error::Error>> {
51     let s: String = match read_to_string(file) {
52 yuzu 9 Ok(s) => s,
53 yuzu 12 Err(_) => {
54     return Err(Box::new(std::io::Error::new(
55     std::io::ErrorKind::NotFound,
56     "cannot find config file",
57     )));
58     }
59 yuzu 9 };
60     match toml::from_str(s.as_str()) {
61 yuzu 11 Ok(c) => Ok(c),
62 yuzu 12 Err(_) => Err(Box::new(std::io::Error::new(
63     std::io::ErrorKind::Other,
64     "unable to parse config file",
65     ))),
66 yuzu 9 }
67     }
68    
69 yuzu 16 fn main() -> Result<(), Box<dyn std::error::Error>> {
70 yuzu 9 let args = Args::parse();
71 yuzu 16 let conf: Config = load_config(&args.config)?;
72     let _sockaddr = if let Some(sock) = conf.socket {
73     sock
74 yuzu 14 } else {
75 yuzu 16 args.socket
76 yuzu 14 };
77 yuzu 16 let mut services: Vec<Service> = Vec::new();
78     for service in conf.service {
79     services.push(service.build()?);
80 yuzu 14 }
81 yuzu 16 loop {
82     services.par_iter_mut()
83     .for_each(|service| {
84     match service.state().expect("unable to get service state") {
85     ServiceState::Failed => service.restart().expect("unable to restart service"),
86     ServiceState::Stopped => (),
87     _ => (),
88     }
89     });
90     services.push(Service::new());
91     std::thread::sleep(std::time::Duration::from_millis(100));
92 yuzu 9 }
93     }