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

# Content
1 use clap::Parser;
2 use salaryman::service::{Service, ServiceConf, ServiceState};
3 use serde::{Deserialize, Serialize};
4 use std::{
5 fs::read_to_string,
6 os::unix::net::{UnixListener, UnixStream},
7 path::PathBuf,
8 };
9 use rayon::prelude::*;
10
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 value_name = "SOCK",
26 help = "UNIX socket to bind",
27 default_value = "/tmp/salaryman.sock"
28 )]
29 socket: PathBuf,
30 }
31
32 pub enum ServiceReq {
33 Create(ServiceConf),
34 }
35
36 #[derive(Serialize, Deserialize, Clone, Debug)]
37 pub struct Config {
38 pub socket: Option<PathBuf>,
39 pub service: Vec<ServiceConf>,
40 }
41 impl Config {
42 pub fn new() -> Self {
43 Self {
44 socket: None,
45 service: Vec::new(),
46 }
47 }
48 }
49
50 fn load_config(file: &PathBuf) -> Result<Config, Box<dyn std::error::Error>> {
51 let s: String = match read_to_string(file) {
52 Ok(s) => s,
53 Err(_) => {
54 return Err(Box::new(std::io::Error::new(
55 std::io::ErrorKind::NotFound,
56 "cannot find config file",
57 )));
58 }
59 };
60 match toml::from_str(s.as_str()) {
61 Ok(c) => Ok(c),
62 Err(_) => Err(Box::new(std::io::Error::new(
63 std::io::ErrorKind::Other,
64 "unable to parse config file",
65 ))),
66 }
67 }
68
69 fn main() -> Result<(), Box<dyn std::error::Error>> {
70 let args = Args::parse();
71 let conf: Config = load_config(&args.config)?;
72 let _sockaddr = if let Some(sock) = conf.socket {
73 sock
74 } else {
75 args.socket
76 };
77 let mut services: Vec<Service> = Vec::new();
78 for service in conf.service {
79 services.push(service.build()?);
80 }
81 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 }
93 }