parent
8fa6afed4a
commit
5a8ab805f7
@ -0,0 +1,43 @@
|
||||
use std::process::Command;
|
||||
|
||||
use crate::result::Result;
|
||||
|
||||
#[derive(Clone, Debug, Deserialize)]
|
||||
pub struct App {
|
||||
pub name: String,
|
||||
pub command: String,
|
||||
pub args: Vec<String>,
|
||||
pub restart_on_success: Option<bool>,
|
||||
pub restart_on_failure: Option<bool>,
|
||||
pub restart_on_terminate: Option<bool>,
|
||||
pub wait: Option<bool>,
|
||||
pub hold: Option<bool>,
|
||||
}
|
||||
|
||||
impl App {
|
||||
pub fn wait_start(&self) -> Result<()> {
|
||||
info!("starting application {}", &self.name);
|
||||
|
||||
let mut command = Command::new(&self.command);
|
||||
command.args(&self.args);
|
||||
|
||||
match command.output() {
|
||||
Ok(output) => {
|
||||
if !output.stdout.is_empty() {
|
||||
let s = String::from_utf8(output.stdout)?;
|
||||
for line in s.lines() {
|
||||
info!("[{}] stdout: {}", &self.name, line);
|
||||
}
|
||||
}
|
||||
if !output.stderr.is_empty() {
|
||||
let s = String::from_utf8(output.stderr)?;
|
||||
for line in s.lines() {
|
||||
info!("[{}] stderr: {}", &self.name, line);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
Err(e) => Err(Box::from(e)),
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,67 @@
|
||||
use std::io::Read;
|
||||
use std::process::{Command, Child, Stdio};
|
||||
|
||||
use crate::app::App;
|
||||
use crate::result::Result;
|
||||
|
||||
pub struct Proc {
|
||||
pub app: App,
|
||||
pub child: Child,
|
||||
}
|
||||
|
||||
impl Proc {
|
||||
pub fn start(app: App) -> Result<Proc> {
|
||||
info!("starting application {}", &app.name);
|
||||
|
||||
let mut command = Command::new(&app.command);
|
||||
command.stdout(Stdio::piped());
|
||||
command.stderr(Stdio::piped());
|
||||
command.args(&app.args);
|
||||
|
||||
let child = command.spawn()?;
|
||||
|
||||
Ok(Proc { app, child })
|
||||
}
|
||||
|
||||
pub fn check_stdout(&mut self) -> Option<String> {
|
||||
let child_stdout = self.child.stdout.as_mut();
|
||||
if let Some(stdout) = child_stdout {
|
||||
let mut buf = String::new();
|
||||
match stdout.read_to_string(&mut buf) {
|
||||
Ok(size) => {
|
||||
if size > 0 {
|
||||
return Some(buf);
|
||||
} else {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
error!("couldn't read stdout from {}: {:?}", &self.app.name, e);
|
||||
return None;
|
||||
}
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
pub fn check_stderr(&mut self) -> Option<String> {
|
||||
let child_stderr = self.child.stderr.as_mut();
|
||||
if let Some(stderr) = child_stderr {
|
||||
let mut buf = String::new();
|
||||
match stderr.read_to_string(&mut buf) {
|
||||
Ok(size) => {
|
||||
if size > 0 {
|
||||
return Some(buf);
|
||||
} else {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
error!("couldn't read stderr from {}: {:?}", &self.app.name, e);
|
||||
return None;
|
||||
}
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
}
|
@ -0,0 +1,3 @@
|
||||
use std::error::Error;
|
||||
|
||||
pub type Result<T> = std::result::Result<T, Box<dyn Error>>;
|
Loading…
Reference in new issue