use default values instead of Option

master
rascul 2 years ago
parent 16dc05992a
commit 5aec535c0e

@ -16,7 +16,8 @@ pub struct App {
pub command: String, pub command: String,
/// arguments /// arguments
pub args: Option<Vec<String>>, #[serde(default = "Vec::new")]
pub args: Vec<String>,
/// should the app be restarted if it exits successfully /// should the app be restarted if it exits successfully
#[serde(default = "ret_false")] #[serde(default = "ret_false")]
@ -31,10 +32,12 @@ pub struct App {
pub restart_on_terminate: bool, pub restart_on_terminate: bool,
/// on sup startup, should we wait for this app to run before continuting /// on sup startup, should we wait for this app to run before continuting
pub wait: Option<bool>, #[serde(default = "ret_false")]
pub wait: bool,
/// should this app keep sup from exiting, if it's running /// should this app keep sup from exiting, if it's running
pub hold: Option<bool>, #[serde(default = "ret_false")]
pub hold: bool,
} }
fn ret_false() -> bool { fn ret_false() -> bool {
@ -47,8 +50,7 @@ impl App {
info!("[{}] starting", &self.name.yellow()); info!("[{}] starting", &self.name.yellow());
let mut command = Command::new(&self.command); let mut command = Command::new(&self.command);
let args = self.args.as_ref(); command.args(&self.args);
command.args(args.unwrap_or(&Vec::new()));
match command.output() { match command.output() {
Ok(output) => { Ok(output) => {

@ -67,7 +67,7 @@ impl Apps {
for app in self.apps() { for app in self.apps() {
// app needs to be waited on before starting the next one // 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() { if let Err(e) = app.wait_start() {
error!("[{}] failed to start: {:?}", &app.name.yellow(), e); error!("[{}] failed to start: {:?}", &app.name.yellow(), e);
} }
@ -79,7 +79,7 @@ impl Apps {
match Proc::start(app) { match Proc::start(app) {
Ok(proc) => { Ok(proc) => {
// see if we need to hold on this app // 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); holds = Some(holds.unwrap_or(0) + 1);
} }

@ -24,8 +24,7 @@ impl Proc {
command.stdout(Stdio::piped()); command.stdout(Stdio::piped());
command.stderr(Stdio::piped()); command.stderr(Stdio::piped());
let args = app.args.as_ref(); command.args(&app.args);
command.args(args.unwrap_or(&Vec::new()));
let process = command.spawn_async()?; let process = command.spawn_async()?;
let token = Token(process.id().try_into()?); let token = Token(process.id().try_into()?);

@ -38,7 +38,7 @@ pub fn run(procs: Vec<Proc>, holds: Option<i8>, poll: Poll) -> Result<Vec<Proc>>
} }
/// restart an App, return the new Proc or report error /// restart an App, return the new Proc or report error
fn restart_app(proc: Proc) -> Result<Proc> { fn restart_app(proc: &Proc) -> Result<Proc> {
let app = proc.app.clone(); let app = proc.app.clone();
let name = proc.app.name.clone(); let name = proc.app.name.clone();
@ -123,16 +123,30 @@ fn run_loop(procs: Vec<Proc>, holds: Option<i8>, poll: Poll, signals: Signals) -
} }
// process exited // process exited
Ok(ProcessEvent::Exit(status)) => { Ok(ProcessEvent::Exit(status)) => {
let hold = proc.app.hold;
if let Some(code) = status.code() { 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 { } 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 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()) poll.register(&p.process, p.token, Ready::all(), PollOpt::edge())
.unwrap(); .unwrap();
procs.push(p); procs.push(p);

Loading…
Cancel
Save