Add worker monitor. Fix #284

This commit is contained in:
Federico Terzi 2020-05-26 19:15:58 +02:00
parent aa366cb916
commit 921c39ba4e
2 changed files with 24 additions and 6 deletions

View File

@ -381,10 +381,27 @@ fn daemon_main(config_set: ConfigSet) {
info!("spawning worker process...");
let espanso_path = std::env::current_exe().expect("unable to obtain espanso path location");
crate::process::spawn_process(
let mut child = crate::process::spawn_process(
&espanso_path.to_string_lossy().to_string(),
&vec!["worker".to_owned()],
);
).expect("unable to create worker process");
// Create a monitor thread that will exit with the same non-zero code if
// the worker thread exits
thread::Builder::new()
.name("worker monitor".to_string())
.spawn(move || {
let result = child.wait();
if let Ok(status) = result {
if let Some(code) = status.code() {
if code != 0 {
error!("worker process exited with non-zero code: {}, exiting", code);
std::process::exit(code);
}
}
}
})
.expect("Unable to spawn worker monitor thread");
std::thread::sleep(Duration::from_millis(200));

View File

@ -18,10 +18,13 @@
*/
use log::warn;
use std::process::{Command, Stdio, Child};
use widestring::WideCString;
use std::io;
#[cfg(target_os = "windows")]
pub fn spawn_process(cmd: &str, args: &Vec<String>) {
// TODO: modify with https://doc.rust-lang.org/std/os/windows/process/trait.CommandExt.html
let quoted_args: Vec<String> = args.iter().map(|arg| format!("\"{}\"", arg)).collect();
let quoted_args = quoted_args.join(" ");
let final_cmd = format!("\"{}\" {}", cmd, quoted_args);
@ -39,8 +42,6 @@ pub fn spawn_process(cmd: &str, args: &Vec<String>) {
}
#[cfg(not(target_os = "windows"))]
pub fn spawn_process(cmd: &str, args: &Vec<String>) {
use std::process::{Command, Stdio};
Command::new(cmd).args(args).spawn();
pub fn spawn_process(cmd: &str, args: &Vec<String>) -> io::Result<Child> {
Command::new(cmd).args(args).spawn()
}