From 5aec535c0e6efd80a3a8a201c47aacbbca29f48c Mon Sep 17 00:00:00 2001 From: rascul Date: Sat, 23 Jul 2022 16:49:34 -0500 Subject: [PATCH] use default values instead of Option --- src/app.rs | 12 +++++++----- src/apps.rs | 4 ++-- src/proc.rs | 3 +-- src/run.rs | 26 ++++++++++++++++++++------ 4 files changed, 30 insertions(+), 15 deletions(-) diff --git a/src/app.rs b/src/app.rs index 20f5b6f..dbdd274 100644 --- a/src/app.rs +++ b/src/app.rs @@ -16,7 +16,8 @@ pub struct App { pub command: String, /// arguments - pub args: Option>, + #[serde(default = "Vec::new")] + pub args: Vec, /// should the app be restarted if it exits successfully #[serde(default = "ret_false")] @@ -31,10 +32,12 @@ pub struct App { pub restart_on_terminate: bool, /// on sup startup, should we wait for this app to run before continuting - pub wait: Option, + #[serde(default = "ret_false")] + pub wait: bool, /// should this app keep sup from exiting, if it's running - pub hold: Option, + #[serde(default = "ret_false")] + pub hold: bool, } fn ret_false() -> bool { @@ -47,8 +50,7 @@ impl App { info!("[{}] starting", &self.name.yellow()); let mut command = Command::new(&self.command); - let args = self.args.as_ref(); - command.args(args.unwrap_or(&Vec::new())); + command.args(&self.args); match command.output() { Ok(output) => { diff --git a/src/apps.rs b/src/apps.rs index afd1045..1c34371 100644 --- a/src/apps.rs +++ b/src/apps.rs @@ -67,7 +67,7 @@ impl Apps { for app in self.apps() { // app needs to be waited on before starting the next one - if app.wait.unwrap_or(false) { + if app.wait { if let Err(e) = app.wait_start() { error!("[{}] failed to start: {:?}", &app.name.yellow(), e); } @@ -79,7 +79,7 @@ impl Apps { match Proc::start(app) { Ok(proc) => { // see if we need to hold on this app - if proc.app.hold.unwrap_or(false) { + if proc.app.hold { holds = Some(holds.unwrap_or(0) + 1); } diff --git a/src/proc.rs b/src/proc.rs index d9079a5..a6374ee 100644 --- a/src/proc.rs +++ b/src/proc.rs @@ -24,8 +24,7 @@ impl Proc { command.stdout(Stdio::piped()); command.stderr(Stdio::piped()); - let args = app.args.as_ref(); - command.args(args.unwrap_or(&Vec::new())); + command.args(&app.args); let process = command.spawn_async()?; let token = Token(process.id().try_into()?); diff --git a/src/run.rs b/src/run.rs index f4c3183..8c2f285 100644 --- a/src/run.rs +++ b/src/run.rs @@ -38,7 +38,7 @@ pub fn run(procs: Vec, holds: Option, poll: Poll) -> Result> } /// restart an App, return the new Proc or report error -fn restart_app(proc: Proc) -> Result { +fn restart_app(proc: &Proc) -> Result { let app = proc.app.clone(); let name = proc.app.name.clone(); @@ -123,16 +123,30 @@ fn run_loop(procs: Vec, holds: Option, poll: Poll, signals: Signals) - } // process exited Ok(ProcessEvent::Exit(status)) => { + let hold = proc.app.hold; + if let Some(code) = status.code() { - info!("[{}] exited with code {}", &proc.app.name.yellow(), code); + if code == 0 { + info!("[{}] exited successfully", &proc.app.name.yellow()); + + if proc.app.restart_on_failure { + if let Ok(p) = restart_app(&proc) { + poll.register(&p.process, p.token, Ready::all(), PollOpt::edge()) + .unwrap(); + procs.push(p); + } else if hold { + holds = Some(holds.unwrap_or(0) - 1); + } + } + } else { + info!("[{}] exited unsuccessfully with code {}", &proc.app.name.yellow(), code); + } } else { - info!("[{}] exited with unknown code", &proc.app.name.yellow()); + info!("[{}] was terminated", &proc.app.name.yellow()); } - let hold = proc.app.hold.unwrap_or(false); - if proc.app.check_restart(status) { - if let Ok(p) = restart_app(proc) { + if let Ok(p) = restart_app(&proc) { poll.register(&p.process, p.token, Ready::all(), PollOpt::edge()) .unwrap(); procs.push(p);