Add worker monitor. Fix #284
This commit is contained in:
		
							parent
							
								
									aa366cb916
								
							
						
					
					
						commit
						921c39ba4e
					
				
							
								
								
									
										21
									
								
								src/main.rs
									
									
									
									
									
								
							
							
						
						
									
										21
									
								
								src/main.rs
									
									
									
									
									
								
							|  | @ -381,10 +381,27 @@ fn daemon_main(config_set: ConfigSet) { | ||||||
|     info!("spawning worker process..."); |     info!("spawning worker process..."); | ||||||
| 
 | 
 | ||||||
|     let espanso_path = std::env::current_exe().expect("unable to obtain espanso path location"); |     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(), |         &espanso_path.to_string_lossy().to_string(), | ||||||
|         &vec!["worker".to_owned()], |         &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)); |     std::thread::sleep(Duration::from_millis(200)); | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -18,10 +18,13 @@ | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| use log::warn; | use log::warn; | ||||||
|  | use std::process::{Command, Stdio, Child}; | ||||||
| use widestring::WideCString; | use widestring::WideCString; | ||||||
|  | use std::io; | ||||||
| 
 | 
 | ||||||
| #[cfg(target_os = "windows")] | #[cfg(target_os = "windows")] | ||||||
| pub fn spawn_process(cmd: &str, args: &Vec<String>) { | 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: Vec<String> = args.iter().map(|arg| format!("\"{}\"", arg)).collect(); | ||||||
|     let quoted_args = quoted_args.join(" "); |     let quoted_args = quoted_args.join(" "); | ||||||
|     let final_cmd = format!("\"{}\" {}", cmd, quoted_args); |     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"))] | #[cfg(not(target_os = "windows"))] | ||||||
| pub fn spawn_process(cmd: &str, args: &Vec<String>) { | pub fn spawn_process(cmd: &str, args: &Vec<String>) -> io::Result<Child> { | ||||||
|     use std::process::{Command, Stdio}; |     Command::new(cmd).args(args).spawn() | ||||||
| 
 |  | ||||||
|     Command::new(cmd).args(args).spawn(); |  | ||||||
| } | } | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user