add service shutdown + refactor async svc code

This commit is contained in:
darkgallium 2020-06-15 00:53:39 +02:00
parent 344ee27b27
commit 97f73eed47
2 changed files with 31 additions and 11 deletions

View File

@ -48,16 +48,6 @@ fn set_hostname() {
}
}
async fn launch_services() {
let services = svc::get_services();
for s in services {
println!("starting {}", s.name);
let args: Vec<&str> = s.start.args.iter().map(|e| e.as_str()).collect();
processes::run(&s.start.command, &args[..]).await;
}
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
@ -89,7 +79,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
processes::run_wait("dbus-daemon", ["--system"].as_ref()).await; // a lot of services depend on dbus being on
println!("now loading your services");
launch_services().await;
svc::launch_services().await;
unsafe { signal_hook::register(SIGINT, sigint_handler) }?;
let sigchld_handle = unsafe { signal_hook::register(SIGCHLD, reap_handler) }?;
@ -101,6 +91,9 @@ async fn main() -> Result<(), Box<dyn Error>> {
signal_hook::unregister(sigchld_handle);
println!("stopping services");
svc::stop_services();
println!("killing remaining processes");
let pids = processes::get_all_pids();

View File

@ -1,6 +1,9 @@
use std::fs;
use serde::Deserialize;
use toml;
use tokio;
use crate::processes;
#[derive(Deserialize, Debug)]
pub struct Service {
@ -34,3 +37,27 @@ pub fn get_services() -> Vec<Service> {
services
}
pub async fn launch_services() {
let services = get_services();
for s in services {
tokio::spawn(async move {
println!("starting {}", s.name);
let args: Vec<&str> = s.start.args.iter().map(|e| e.as_str()).collect();
processes::run_wait(&s.start.command, &args[..]).await;
});
}
}
pub fn stop_services() {
let services = get_services();
for s in services {
println!("stopping {}", s.name);
if let Some(stop) = s.stop {
let args: Vec<&str> = stop.args.iter().map(|e| e.as_str()).collect();
processes::sync_run_wait(&stop.command, &args[..]);
}
}
}