wip: implement error kind

This commit is contained in:
Suyono 2023-11-25 19:09:01 +11:00
parent 11e2a14cc4
commit c03adb808a
3 changed files with 48 additions and 16 deletions

View File

@ -1,3 +1 @@
#!/bin/bash you cannot run this file
exec sleep 1

View File

@ -11,30 +11,37 @@ use nix::sys::signal::{kill, Signal};
use nix::errno::Errno; use nix::errno::Errno;
use nix::unistd::Pid; use nix::unistd::Pid;
use crate::init::config; use crate::init::config;
use crate::init::error::NoShellAvailableError; use crate::init::error::{NoShellAvailableError, SpawnError};
pub fn start_services(ts: &mut JoinSet<Result<(), Box<dyn error::Error + Send + Sync>>>, cfg: &config::Config, cancel: CancellationToken) pub fn start_services(ts: &mut JoinSet<Result<(), Box<dyn error::Error + Send + Sync>>>, cfg: &config::Config, cancel: CancellationToken)
-> Result<(), Box<dyn error::Error>> { -> Result<(), Box<dyn error::Error>> {
for svc_ in cfg.get_service_iter() { for svc_ in cfg.get_service_iter() {
let shell: String = cfg.get_shell().ok_or::<Box<dyn error::Error>>(NoShellAvailableError.into())?; let mut shell: String = String::new();
if let config::Command::ShellPrefixed(_) = svc_ {
shell = cfg.get_shell().ok_or::<Box<dyn error::Error>>(NoShellAvailableError.into())?;
}
let svc = svc_.clone(); let svc = svc_.clone();
// if let config::Command::ShellPrefixed(_) = svc {
// shell = cfg.get_shell().ok_or::<Box<dyn error::Error>>(NoShellAvailableError.into())?;
// }
let cancel = cancel.clone(); let cancel = cancel.clone();
ts.spawn(async move { ts.spawn(async move {
'autorestart: loop { 'autorestart: loop {
let mut child: Child; let mut child: Child;
let shell = shell.clone();
let svc = svc.clone(); let svc = svc.clone();
match svc { match svc {
config::Command::Direct(c) => { config::Command::Direct(c) => {
child = Command::new(c).spawn().expect("change me"); let exp_str = c.clone();
child = Command::new(c).spawn().map_err(|e| {
Box::new(SpawnError(format!("{}: {}", exp_str, e)))
})?;
}, },
config::Command::ShellPrefixed(s) => { config::Command::ShellPrefixed(s) => {
child = Command::new(shell).arg(s).spawn().expect("change me"); let shell = shell.clone();
let exp_str = s.clone();
let exp_shell = shell.clone();
child = Command::new(shell).arg(s).spawn().map_err(|e| {
Box::new(SpawnError(format!("{} {}: {}", exp_shell, exp_str, e)))
})?;
} }
} }
@ -74,12 +81,12 @@ pub fn start_services(ts: &mut JoinSet<Result<(), Box<dyn error::Error + Send +
}, },
} }
} }
println!("starter: task completed"); dbg!("starter: task completed");
Ok(()) Ok(())
}); });
} }
println!("starter: spawning completed"); dbg!("starter: spawning completed");
Ok(()) Ok(())
} }
@ -95,8 +102,7 @@ fn result_match(result: tokio_result<ExitStatus>) -> Result<(), Box<dyn error::E
} }
} }
//TODO: remove me! this is for debug + tracing purpose dbg!("starter: sleep exited");
println!("starter: sleep exited");
Ok(()) Ok(())
} }

View File

@ -1,6 +1,17 @@
use std::fmt; use std::fmt;
use std::error; use std::error;
pub enum WingmateErrorKind {
SpawnError,
#[allow(dead_code)]
Other,
}
pub trait WingmateError {
fn wingmate_error_kind(&self) -> WingmateErrorKind;
}
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct InvalidConfigSearchPathError; pub struct InvalidConfigSearchPathError;
@ -55,4 +66,21 @@ impl fmt::Display for NoShellAvailableError {
} }
} }
impl error::Error for NoShellAvailableError {} impl error::Error for NoShellAvailableError {}
#[derive(Debug,Clone)]
pub struct SpawnError(pub String);
impl fmt::Display for SpawnError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "failed to spawn: {}", self.0)
}
}
impl error::Error for SpawnError {}
impl WingmateError for SpawnError {
fn wingmate_error_kind(&self) -> WingmateErrorKind {
WingmateErrorKind::SpawnError
}
}