wip: replace dyn Error with concrete
This commit is contained in:
parent
aabf39f041
commit
6e60f58145
@ -6,7 +6,7 @@ use wingmate_rs::init;
|
|||||||
async fn main() -> Result<(), Box<dyn error::Error>> {
|
async fn main() -> Result<(), Box<dyn error::Error>> {
|
||||||
if let Err(e) = init::start().await {
|
if let Err(e) = init::start().await {
|
||||||
eprintln!("{}", e);
|
eprintln!("{}", e);
|
||||||
return Err(e);
|
return Err(e.into());
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|||||||
11
src/init.rs
11
src/init.rs
@ -3,12 +3,14 @@ mod config;
|
|||||||
pub(crate) mod error;
|
pub(crate) mod error;
|
||||||
|
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::error as std_err;
|
use anyhow::Context;
|
||||||
|
|
||||||
pub async fn start() -> Result<(), Box<dyn std_err::Error>> {
|
const WINGMATE_CONFIG_PATH: &'static str = "WINGMATE_CONFIG_PATH";
|
||||||
|
|
||||||
|
pub async fn start() -> Result<(), error::WingmateInitError> {
|
||||||
let mut vec_search: Vec<String> = Vec::new();
|
let mut vec_search: Vec<String> = Vec::new();
|
||||||
|
|
||||||
match env::var("WINGMATE_CONFIG_PATH") {
|
match env::var(WINGMATE_CONFIG_PATH) {
|
||||||
Ok(paths) => {
|
Ok(paths) => {
|
||||||
for p in paths.split(':') {
|
for p in paths.split(':') {
|
||||||
vec_search.push(String::from(p));
|
vec_search.push(String::from(p));
|
||||||
@ -16,7 +18,8 @@ pub async fn start() -> Result<(), Box<dyn std_err::Error>> {
|
|||||||
},
|
},
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
if let env::VarError::NotUnicode(_) = e {
|
if let env::VarError::NotUnicode(_) = e {
|
||||||
return Err(e.into());
|
return Err(e).context(format!("reading {} env var", WINGMATE_CONFIG_PATH))
|
||||||
|
.map_err(|e| {error::WingmateInitError::Other { source: e }} );
|
||||||
} else {
|
} else {
|
||||||
vec_search.push(String::from("/etc/wingmate"));
|
vec_search.push(String::from("/etc/wingmate"));
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,11 +2,23 @@ use std::fs;
|
|||||||
use std::env;
|
use std::env;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::io::{BufReader, BufRead};
|
use std::io::{BufReader, BufRead};
|
||||||
use std::error as std_error;
|
|
||||||
use crate::init::error as wingmate_error;
|
use crate::init::error as wingmate_error;
|
||||||
use nix::unistd::{access, AccessFlags};
|
use nix::unistd::{access, AccessFlags};
|
||||||
use lazy_static::lazy_static;
|
use lazy_static::lazy_static;
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
|
use anyhow::Context;
|
||||||
|
|
||||||
|
const CRON_REGEX_STR: &'static str = r"^\s*(?P<minute>\S+)\s+(?P<hour>\S+)\s+(?P<dom>\S+)\s+(?P<month>\S+)\s+(?P<dow>\S+)\s+(?P<command>\S.*\S)\s*$";
|
||||||
|
const MINUTE: &'static str = "minute";
|
||||||
|
const HOUR: &'static str = "hour";
|
||||||
|
const DAY_OF_MONTH_ABBRV: &'static str = "dom";
|
||||||
|
const DAY_OF_MONTH: &'static str = "day of month";
|
||||||
|
const MONTH: &'static str = "month";
|
||||||
|
const DAY_OF_WEEK_ABBRV: &'static str = "dow";
|
||||||
|
const DAY_OF_WEEK: &'static str = "day of week";
|
||||||
|
const COMMAND: &'static str = "command";
|
||||||
|
const WINGMATE_SHELL_ENV: &'static str = "WINGMATE_SHELL";
|
||||||
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum Command {
|
pub enum Command {
|
||||||
@ -40,7 +52,7 @@ pub struct Config {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Config {
|
impl Config {
|
||||||
pub fn find(search_path: Vec<String>) -> Result<Config, Box<dyn std_error::Error>> {
|
pub fn find(search_path: Vec<String>) -> Result<Config, wingmate_error::WingmateInitError> {
|
||||||
if search_path.is_empty() {
|
if search_path.is_empty() {
|
||||||
return Err(wingmate_error::WingmateInitError::InvalidConfigSearchPath.into());
|
return Err(wingmate_error::WingmateInitError::InvalidConfigSearchPath.into());
|
||||||
}
|
}
|
||||||
@ -68,7 +80,7 @@ impl Config {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
cron = Self::read_crontab(&mut buf)?;
|
cron = Self::read_crontab(&mut buf).map_err(|e| { wingmate_error::WingmateInitError::Cron { source: e }})?;
|
||||||
|
|
||||||
//TODO: need to include cron in the condition
|
//TODO: need to include cron in the condition
|
||||||
if !svc_commands.is_empty() || !cron.is_empty() {
|
if !svc_commands.is_empty() || !cron.is_empty() {
|
||||||
@ -89,16 +101,14 @@ impl Config {
|
|||||||
cron,
|
cron,
|
||||||
shell_path: None,
|
shell_path: None,
|
||||||
};
|
};
|
||||||
config.find_shell()?;
|
config.find_shell().map_err(|e| { wingmate_error::WingmateInitError::FindShell { source: e } })?;
|
||||||
|
|
||||||
Ok(config)
|
Ok(config)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_crontab(path: &mut PathBuf) -> Result<Vec<Crontab>, Box<dyn std_error::Error>> {
|
fn read_crontab(path: &mut PathBuf) -> Result<Vec<Crontab>, wingmate_error::CronParseError> {
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
static ref CRON_REGEX: Regex = Regex::new(
|
static ref CRON_REGEX: Regex = Regex::new(CRON_REGEX_STR).unwrap();
|
||||||
r"^\s*(?P<minute>\S+)\s+(?P<hour>\S+)\s+(?P<dom>\S+)\s+(?P<month>\S+)\s+(?P<dow>\S+)\s+(?P<command>\S.*\S)\s*$"
|
|
||||||
).unwrap();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let cron_path = path.join("crontab");
|
let cron_path = path.join("crontab");
|
||||||
@ -107,45 +117,72 @@ impl Config {
|
|||||||
if let Ok(f) = fs::File::open(cron_path.as_path()) {
|
if let Ok(f) = fs::File::open(cron_path.as_path()) {
|
||||||
for line in BufReader::new(f).lines() {
|
for line in BufReader::new(f).lines() {
|
||||||
if let Ok(l) = line {
|
if let Ok(l) = line {
|
||||||
let cap = CRON_REGEX.captures(&l).ok_or::<Box<dyn std_error::Error>>(wingmate_error::CronSyntaxError(String::from(&l)).into())?;
|
let cap = CRON_REGEX.captures(&l).ok_or::<wingmate_error::CronParseError>(
|
||||||
|
wingmate_error::CronParseError::InvalidSyntax(String::from(&l))
|
||||||
|
)?;
|
||||||
|
|
||||||
let mut match_str = cap.name("minute").ok_or::<Box<dyn std_error::Error>>(
|
let mut match_str = cap.name(MINUTE).ok_or::<wingmate_error::CronParseError>(
|
||||||
wingmate_error::CronSyntaxError(format!("cannot capture minute in \"{}\"", &l)).into()
|
wingmate_error::CronParseError::FieldMatch { cron_line: String::from(&l), field_name: String::from(MINUTE) }
|
||||||
)?;
|
)?;
|
||||||
let minute = Self::to_cron_time_field_spec(&match_str).map_err(|e| {
|
let minute = Self::to_cron_time_field_spec(&match_str).map_err(|e| {
|
||||||
Box::new(wingmate_error::CronSyntaxError(format!("failed to parse minute \"{}\" in \"{}\": {}", &match_str.as_str(), &l, e)))
|
wingmate_error::CronParseError::Parse {
|
||||||
|
source: e,
|
||||||
|
cron_line: String::from(&l),
|
||||||
|
matched: String::from(match_str.as_str()),
|
||||||
|
field_name: String::from(MINUTE)
|
||||||
|
}
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
match_str = cap.name("hour").ok_or::<Box<dyn std_error::Error>>(
|
match_str = cap.name(HOUR).ok_or::<wingmate_error::CronParseError>(
|
||||||
wingmate_error::CronSyntaxError(format!("cannot capture hour in \"{}\"", &l)).into()
|
wingmate_error::CronParseError::FieldMatch { cron_line: String::from(&l), field_name: String::from(HOUR) }
|
||||||
)?;
|
)?;
|
||||||
let hour = Self::to_cron_time_field_spec(&match_str).map_err(|e| {
|
let hour = Self::to_cron_time_field_spec(&match_str).map_err(|e| {
|
||||||
Box::new(wingmate_error::CronSyntaxError(format!("failed to parse hour \"{}\" in \"{}\": {}", &match_str.as_str(), &l, e)))
|
wingmate_error::CronParseError::Parse {
|
||||||
|
source: e,
|
||||||
|
cron_line: String::from(&l),
|
||||||
|
matched: String::from(match_str.as_str()),
|
||||||
|
field_name: String::from(HOUR)
|
||||||
|
}
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
match_str = cap.name("dom").ok_or::<Box<dyn std_error::Error>>(
|
match_str = cap.name(DAY_OF_MONTH_ABBRV).ok_or::<wingmate_error::CronParseError>(
|
||||||
wingmate_error::CronSyntaxError(format!("cannot capture day of month in \"{}\"", &l)).into()
|
wingmate_error::CronParseError::FieldMatch { cron_line: String::from(&l), field_name: String::from(DAY_OF_MONTH) }
|
||||||
)?;
|
)?;
|
||||||
let dom = Self::to_cron_time_field_spec(&match_str).map_err(|e| {
|
let dom = Self::to_cron_time_field_spec(&match_str).map_err(|e| {
|
||||||
Box::new(wingmate_error::CronSyntaxError(format!("failed to parse day of month \"{}\" in \"{}\": {}", &match_str.as_str(), &l, e)))
|
wingmate_error::CronParseError::Parse {
|
||||||
|
source: e,
|
||||||
|
cron_line: String::from(&l),
|
||||||
|
matched: String::from(match_str.as_str()),
|
||||||
|
field_name: String::from(DAY_OF_MONTH)
|
||||||
|
}
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
match_str = cap.name("month").ok_or::<Box<dyn std_error::Error>>(
|
match_str = cap.name(MONTH).ok_or::<wingmate_error::CronParseError>(
|
||||||
wingmate_error::CronSyntaxError(format!("cannot capture month in \"{}\"", &l)).into()
|
wingmate_error::CronParseError::FieldMatch { cron_line: String::from(&l), field_name: String::from(MONTH) }
|
||||||
)?;
|
)?;
|
||||||
let month = Self::to_cron_time_field_spec(&match_str).map_err(|e| {
|
let month = Self::to_cron_time_field_spec(&match_str).map_err(|e| {
|
||||||
Box::new(wingmate_error::CronSyntaxError(format!("failed to parse month \"{}\" in \"{}\": {}", &match_str.as_str(), &l, e)))
|
wingmate_error::CronParseError::Parse {
|
||||||
|
source: e,
|
||||||
|
cron_line: String::from(&l),
|
||||||
|
matched: String::from(match_str.as_str()),
|
||||||
|
field_name: String::from(MONTH)
|
||||||
|
}
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
match_str = cap.name("dow").ok_or::<Box<dyn std_error::Error>>(
|
match_str = cap.name(DAY_OF_WEEK_ABBRV).ok_or::<wingmate_error::CronParseError>(
|
||||||
wingmate_error::CronSyntaxError(format!("cannot capture day of week in \"{}\"", &l)).into()
|
wingmate_error::CronParseError::FieldMatch { cron_line: String::from(&l), field_name: String::from(DAY_OF_WEEK) }
|
||||||
)?;
|
)?;
|
||||||
let dow = Self::to_cron_time_field_spec(&match_str).map_err(|e| {
|
let dow = Self::to_cron_time_field_spec(&match_str).map_err(|e| {
|
||||||
Box::new(wingmate_error::CronSyntaxError(format!("failed to parse day of week \"{}\" in \"{}\": {}", &match_str.as_str(), &l, e)))
|
wingmate_error::CronParseError::Parse {
|
||||||
|
source: e,
|
||||||
|
cron_line: String::from(&l),
|
||||||
|
matched: String::from(match_str.as_str()),
|
||||||
|
field_name: String::from(DAY_OF_WEEK)
|
||||||
|
}
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
match_str = cap.name("command").ok_or::<Box<dyn std_error::Error>>(
|
match_str = cap.name(COMMAND).ok_or::<wingmate_error::CronParseError>(
|
||||||
wingmate_error::CronSyntaxError(format!("cannot capture command in \"{}\"", &l)).into()
|
wingmate_error::CronParseError::FieldMatch { cron_line: String::from(&l), field_name: String::from(COMMAND) }
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
ret_vec.push(Crontab {
|
ret_vec.push(Crontab {
|
||||||
@ -163,34 +200,34 @@ impl Config {
|
|||||||
Ok(ret_vec)
|
Ok(ret_vec)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn to_cron_time_field_spec(match_str: ®ex::Match) -> Result<CronTimeFieldSpec, Box<dyn std_error::Error>> {
|
fn to_cron_time_field_spec(match_str: ®ex::Match) -> Result<CronTimeFieldSpec, anyhow::Error> {
|
||||||
let field = match_str.as_str();
|
let field = match_str.as_str();
|
||||||
|
|
||||||
if field == "*" {
|
if field == "*" {
|
||||||
return Ok(CronTimeFieldSpec::Any);
|
return Ok(CronTimeFieldSpec::Any);
|
||||||
} else if field.starts_with("*/") {
|
} else if field.starts_with("*/") {
|
||||||
let every = field[2..].parse::<u8>()?;
|
let every = field[2..].parse::<u8>().context("parsing on field matching \"every\" pattern")?;
|
||||||
return Ok(CronTimeFieldSpec::Every(every));
|
return Ok(CronTimeFieldSpec::Every(every));
|
||||||
} else if field.contains(",") {
|
} else if field.contains(",") {
|
||||||
let multi: Vec<&str> = field.split(",").collect();
|
let multi: Vec<&str> = field.split(",").collect();
|
||||||
let mut multi_occurrence: Vec<u8> = Vec::new();
|
let mut multi_occurrence: Vec<u8> = Vec::new();
|
||||||
|
|
||||||
for m in multi {
|
for m in multi {
|
||||||
let ur = m.parse::<u8>()?;
|
let ur = m.parse::<u8>().context("parsing on field matching \"multi occurrence\" pattern")?;
|
||||||
multi_occurrence.push(ur);
|
multi_occurrence.push(ur);
|
||||||
}
|
}
|
||||||
|
|
||||||
return Ok(CronTimeFieldSpec::MultiOccurrence(multi_occurrence));
|
return Ok(CronTimeFieldSpec::MultiOccurrence(multi_occurrence));
|
||||||
} else {
|
} else {
|
||||||
let n = field.parse::<u8>()?;
|
let n = field.parse::<u8>().context("parsing on field matching \"exact\" pattern")?;
|
||||||
return Ok(CronTimeFieldSpec::Exact(n));
|
return Ok(CronTimeFieldSpec::Exact(n));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn find_shell(&mut self) -> Result<(), Box<dyn std_error::Error>> {
|
fn find_shell(&mut self) -> Result<(), wingmate_error::FindShellError> {
|
||||||
|
|
||||||
let shell: String;
|
let shell: String;
|
||||||
match env::var("WINGMATE_SHELL") {
|
match env::var(WINGMATE_SHELL_ENV) {
|
||||||
Ok(sh) => {
|
Ok(sh) => {
|
||||||
shell = sh;
|
shell = sh;
|
||||||
},
|
},
|
||||||
@ -200,13 +237,15 @@ impl Config {
|
|||||||
shell = String::from("sh");
|
shell = String::from("sh");
|
||||||
},
|
},
|
||||||
env::VarError::NotUnicode(_) => {
|
env::VarError::NotUnicode(_) => {
|
||||||
return Err(e.into());
|
return Err(e).context(format!("reading {} env var", WINGMATE_SHELL_ENV))
|
||||||
|
.map_err(|e| { wingmate_error::FindShellError::Other { source: e } })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let env_path = env::var("PATH")?;
|
let env_path = env::var("PATH").context("getting PATH env variable")
|
||||||
|
.map_err(|e| { wingmate_error::FindShellError::Other { source: e } })?;
|
||||||
let vec_path: Vec<&str> = env_path.split(':').collect();
|
let vec_path: Vec<&str> = env_path.split(':').collect();
|
||||||
|
|
||||||
for p in vec_path {
|
for p in vec_path {
|
||||||
@ -220,7 +259,7 @@ impl Config {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Err(wingmate_error::ShellNotFoundError(shell).into())
|
Err(wingmate_error::FindShellError::ShellNotFound)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_service_iter(&self) -> std::slice::Iter<Command> {
|
pub fn get_service_iter(&self) -> std::slice::Iter<Command> {
|
||||||
|
|||||||
@ -3,13 +3,14 @@ mod waiter;
|
|||||||
mod starter;
|
mod starter;
|
||||||
mod constants;
|
mod constants;
|
||||||
|
|
||||||
use std::error;
|
|
||||||
use tokio::task::JoinSet;
|
use tokio::task::JoinSet;
|
||||||
use tokio_util::sync::CancellationToken;
|
use tokio_util::sync::CancellationToken;
|
||||||
use std::sync::{Arc, Mutex};
|
use std::sync::{Arc, Mutex};
|
||||||
use crate::init::config;
|
use crate::init::config;
|
||||||
|
use crate::init::error as wmerr;
|
||||||
|
use crate::init::error::WingmateInitError;
|
||||||
|
|
||||||
pub async fn start(cfg: config::Config) -> Result<(), Box<dyn error::Error>> {
|
pub async fn start(cfg: config::Config) -> Result<(), WingmateInitError> {
|
||||||
let sync_flag = Arc::new(Mutex::new(false));
|
let sync_flag = Arc::new(Mutex::new(false));
|
||||||
let sig_sync_flag = sync_flag.clone();
|
let sig_sync_flag = sync_flag.clone();
|
||||||
|
|
||||||
@ -19,7 +20,7 @@ pub async fn start(cfg: config::Config) -> Result<(), Box<dyn error::Error>> {
|
|||||||
let cancel = CancellationToken::new();
|
let cancel = CancellationToken::new();
|
||||||
let starter_cancel = cancel.clone();
|
let starter_cancel = cancel.clone();
|
||||||
|
|
||||||
let mut set: JoinSet<Result<(), Box<dyn error::Error + Send + Sync>>> = JoinSet::new();
|
let mut set: JoinSet<Result<(), wmerr::WingmateInitError>> = JoinSet::new();
|
||||||
set.spawn(async move {
|
set.spawn(async move {
|
||||||
sighandler::sighandler(sig_sync_flag, cancel, sighandler_cancel).await
|
sighandler::sighandler(sig_sync_flag, cancel, sighandler_cancel).await
|
||||||
});
|
});
|
||||||
@ -38,12 +39,20 @@ pub async fn start(cfg: config::Config) -> Result<(), Box<dyn error::Error>> {
|
|||||||
Ok(v) => {
|
Ok(v) => {
|
||||||
if let Err(ev) = v {
|
if let Err(ev) = v {
|
||||||
dbg!(&ev);
|
dbg!(&ev);
|
||||||
return Err(ev as Box<dyn error::Error>);
|
match ev {
|
||||||
|
WingmateInitError::SpawnError { source, message } => {
|
||||||
|
eprintln!("{}", WingmateInitError::SpawnError { source, message });
|
||||||
|
},
|
||||||
|
_ => {
|
||||||
|
return Err(ev);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// return Err(ev as Box<dyn error::Error>);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
dbg!(&e);
|
dbg!(&e);
|
||||||
return Err(e.into());
|
return Err(WingmateInitError::Join { source: e });
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,13 +1,13 @@
|
|||||||
use std::error;
|
|
||||||
use tokio::signal::unix::{signal, SignalKind};
|
use tokio::signal::unix::{signal, SignalKind};
|
||||||
use tokio::select;
|
use tokio::select;
|
||||||
use std::sync::{Arc, Mutex};
|
use std::sync::{Arc, Mutex};
|
||||||
use tokio_util::sync::CancellationToken;
|
use tokio_util::sync::CancellationToken;
|
||||||
|
use crate::init::error::WingmateInitError;
|
||||||
|
|
||||||
pub async fn sighandler(flag: Arc<Mutex<bool>>, cancel: CancellationToken, exit: CancellationToken) -> Result<(), Box<dyn error::Error + Send + Sync>> {
|
pub async fn sighandler(flag: Arc<Mutex<bool>>, cancel: CancellationToken, exit: CancellationToken) -> Result<(), WingmateInitError> {
|
||||||
let mut sigint = signal(SignalKind::interrupt())?;
|
let mut sigint = signal(SignalKind::interrupt()).map_err(|e| { WingmateInitError::Signal { source: e } })?;
|
||||||
let mut sigterm = signal(SignalKind::terminate())?;
|
let mut sigterm = signal(SignalKind::terminate()).map_err(|e| { WingmateInitError::Signal { source: e } })?;
|
||||||
let mut sigchld = signal(SignalKind::child())?;
|
let mut sigchld = signal(SignalKind::child()).map_err(|e| { WingmateInitError::Signal { source: e } })?;
|
||||||
|
|
||||||
'signal: loop {
|
'signal: loop {
|
||||||
select! {
|
select! {
|
||||||
|
|||||||
@ -6,21 +6,21 @@ use tokio::io::Result as tokio_result;
|
|||||||
use tokio::time::sleep;
|
use tokio::time::sleep;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use std::process::ExitStatus;
|
use std::process::ExitStatus;
|
||||||
use std::error;
|
|
||||||
use nix::sys::signal::{kill, Signal};
|
use nix::sys::signal::{kill, Signal};
|
||||||
use nix::errno::Errno;
|
use nix::errno::Errno;
|
||||||
use nix::unistd::Pid;
|
use nix::unistd::Pid;
|
||||||
|
use anyhow::Context;
|
||||||
use crate::init::config;
|
use crate::init::config;
|
||||||
use crate::init::error::{NoShellAvailableError, WingmateInitError};
|
use crate::init::error::WingmateInitError;
|
||||||
|
|
||||||
|
|
||||||
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<(), WingmateInitError>>, cfg: &config::Config, cancel: CancellationToken)
|
||||||
-> Result<(), Box<dyn error::Error>> {
|
-> Result<(), WingmateInitError> {
|
||||||
|
|
||||||
for svc_ in cfg.get_service_iter() {
|
for svc_ in cfg.get_service_iter() {
|
||||||
let mut shell: String = String::new();
|
let mut shell: String = String::new();
|
||||||
if let config::Command::ShellPrefixed(_) = svc_ {
|
if let config::Command::ShellPrefixed(_) = svc_ {
|
||||||
shell = cfg.get_shell().ok_or::<Box<dyn error::Error>>(NoShellAvailableError.into())?;
|
shell = cfg.get_shell().ok_or::<WingmateInitError>(WingmateInitError::NoShellAvailable)?;
|
||||||
}
|
}
|
||||||
let svc = svc_.clone();
|
let svc = svc_.clone();
|
||||||
let cancel = cancel.clone();
|
let cancel = cancel.clone();
|
||||||
@ -32,7 +32,7 @@ pub fn start_services(ts: &mut JoinSet<Result<(), Box<dyn error::Error + Send +
|
|||||||
config::Command::Direct(c) => {
|
config::Command::Direct(c) => {
|
||||||
let exp_str = c.clone();
|
let exp_str = c.clone();
|
||||||
child = Command::new(c).spawn().map_err(|e| {
|
child = Command::new(c).spawn().map_err(|e| {
|
||||||
Box::new(WingmateInitError::SpawnError { source: e, message: exp_str })
|
WingmateInitError::SpawnError { source: e, message: exp_str }
|
||||||
})?;
|
})?;
|
||||||
},
|
},
|
||||||
config::Command::ShellPrefixed(s) => {
|
config::Command::ShellPrefixed(s) => {
|
||||||
@ -40,7 +40,7 @@ pub fn start_services(ts: &mut JoinSet<Result<(), Box<dyn error::Error + Send +
|
|||||||
let exp_str = s.clone();
|
let exp_str = s.clone();
|
||||||
let exp_shell = shell.clone();
|
let exp_shell = shell.clone();
|
||||||
child = Command::new(shell).arg(s).spawn().map_err(|e| {
|
child = Command::new(shell).arg(s).spawn().map_err(|e| {
|
||||||
Box::new(WingmateInitError::SpawnError { source: e, message: format!("{} {}", exp_shell, exp_str) })
|
WingmateInitError::SpawnError { source: e, message: format!("{} {}", exp_shell, exp_str) }
|
||||||
})?;
|
})?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -56,7 +56,7 @@ pub fn start_services(ts: &mut JoinSet<Result<(), Box<dyn error::Error + Send +
|
|||||||
},
|
},
|
||||||
result = child.wait() => {
|
result = child.wait() => {
|
||||||
if let Err(e) = result_match(result) {
|
if let Err(e) = result_match(result) {
|
||||||
return Err(e);
|
return Err(WingmateInitError::ChildExit { source: e });
|
||||||
}
|
}
|
||||||
break 'autorestart;
|
break 'autorestart;
|
||||||
}
|
}
|
||||||
@ -64,7 +64,7 @@ pub fn start_services(ts: &mut JoinSet<Result<(), Box<dyn error::Error + Send +
|
|||||||
},
|
},
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
if e != Errno::ESRCH {
|
if e != Errno::ESRCH {
|
||||||
return Err(e.into());
|
return Err(WingmateInitError::ChildNotFound);
|
||||||
} else {
|
} else {
|
||||||
break 'autorestart;
|
break 'autorestart;
|
||||||
}
|
}
|
||||||
@ -76,7 +76,7 @@ pub fn start_services(ts: &mut JoinSet<Result<(), Box<dyn error::Error + Send +
|
|||||||
},
|
},
|
||||||
result = child.wait() => {
|
result = child.wait() => {
|
||||||
if let Err(e) = result_match(result) {
|
if let Err(e) = result_match(result) {
|
||||||
return Err(e);
|
return Err(WingmateInitError::ChildExit { source: e });
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@ -91,14 +91,14 @@ pub fn start_services(ts: &mut JoinSet<Result<(), Box<dyn error::Error + Send +
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn result_match(result: tokio_result<ExitStatus>) -> Result<(), Box<dyn error::Error + Send + Sync>> {
|
fn result_match(result: tokio_result<ExitStatus>) -> Result<(), anyhow::Error> {
|
||||||
if let Err(e) = result {
|
if let Err(e) = result {
|
||||||
if let Some(eos) = e.raw_os_error() {
|
if let Some(eos) = e.raw_os_error() {
|
||||||
if eos != nix::Error::ECHILD as i32 {
|
if eos != nix::Error::ECHILD as i32 {
|
||||||
return Err(e.into());
|
return Err(e).context("unexpected child exit status");
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return Err(e.into());
|
return Err(e).context("unexpected child error");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -37,7 +37,6 @@ pub fn wait_all(flag: Arc<Mutex<bool>>, stop_sighandler: CancellationToken) {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
// dbg!("sanity");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -1,9 +1,5 @@
|
|||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
|
|
||||||
use std::fmt;
|
|
||||||
use std::error;
|
|
||||||
|
|
||||||
|
|
||||||
#[derive(Error,Debug)]
|
#[derive(Error,Debug)]
|
||||||
pub enum WingmateInitError {
|
pub enum WingmateInitError {
|
||||||
#[error("invalid config search path")]
|
#[error("invalid config search path")]
|
||||||
@ -17,38 +13,80 @@ pub enum WingmateInitError {
|
|||||||
#[source]
|
#[source]
|
||||||
source: std::io::Error,
|
source: std::io::Error,
|
||||||
message: String,
|
message: String,
|
||||||
|
},
|
||||||
|
|
||||||
|
#[error("parsing cron")]
|
||||||
|
Cron {
|
||||||
|
#[source]
|
||||||
|
source: CronParseError,
|
||||||
|
},
|
||||||
|
|
||||||
|
#[error("looking for shell")]
|
||||||
|
FindShell {
|
||||||
|
#[source]
|
||||||
|
source: FindShellError,
|
||||||
|
},
|
||||||
|
|
||||||
|
#[error("child exited")]
|
||||||
|
ChildExit {
|
||||||
|
#[source]
|
||||||
|
source: anyhow::Error,
|
||||||
|
},
|
||||||
|
|
||||||
|
#[error("cannot find the child process")]
|
||||||
|
ChildNotFound,
|
||||||
|
|
||||||
|
#[error("failed to setup signal handler")]
|
||||||
|
Signal {
|
||||||
|
#[source]
|
||||||
|
source: std::io::Error,
|
||||||
|
},
|
||||||
|
|
||||||
|
#[error("no shell available")]
|
||||||
|
NoShellAvailable,
|
||||||
|
|
||||||
|
#[error("problem when join task")]
|
||||||
|
Join {
|
||||||
|
#[source]
|
||||||
|
source: tokio::task::JoinError,
|
||||||
|
},
|
||||||
|
|
||||||
|
#[error("tripped over")]
|
||||||
|
Other {
|
||||||
|
#[source]
|
||||||
|
source: anyhow::Error,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug,Clone)]
|
#[derive(Error,Debug)]
|
||||||
pub struct CronSyntaxError(pub String);
|
pub enum CronParseError {
|
||||||
|
#[error("invalid cron syntax: {}", .0)]
|
||||||
|
InvalidSyntax(String),
|
||||||
|
|
||||||
impl fmt::Display for CronSyntaxError {
|
#[error("cannot capture {} in \"{}\"", field_name, cron_line)]
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
FieldMatch {
|
||||||
write!(f, "cron syntax error at: {}", self.0)
|
cron_line: String,
|
||||||
|
field_name: String,
|
||||||
|
},
|
||||||
|
|
||||||
|
#[error("failed to parse {} \"{}\" in \"{}\"", field_name, matched, cron_line)]
|
||||||
|
Parse {
|
||||||
|
#[source]
|
||||||
|
source: anyhow::Error,
|
||||||
|
cron_line: String,
|
||||||
|
matched: String,
|
||||||
|
field_name: String,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl error::Error for CronSyntaxError {}
|
#[derive(Error,Debug)]
|
||||||
|
pub enum FindShellError {
|
||||||
|
#[error("shell not found")]
|
||||||
|
ShellNotFound,
|
||||||
|
|
||||||
#[derive(Debug,Clone)]
|
#[error("when finding shell")]
|
||||||
pub struct ShellNotFoundError(pub String);
|
Other {
|
||||||
|
#[source]
|
||||||
impl fmt::Display for ShellNotFoundError {
|
source: anyhow::Error
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
||||||
write!(f, "shell not found: {}", self.0)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl error::Error for ShellNotFoundError {}
|
|
||||||
|
|
||||||
#[derive(Debug,Clone)]
|
|
||||||
pub struct NoShellAvailableError;
|
|
||||||
|
|
||||||
impl fmt::Display for NoShellAvailableError {
|
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
||||||
write!(f, "no shell available")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl error::Error for NoShellAvailableError {}
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user