separate into a few files

master
rasul 5 years ago
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)),
}
}
}

@ -6,53 +6,18 @@ extern crate serde;
extern crate serde_derive;
extern crate toml;
use std::error::Error;
mod app;
mod proc;
mod result;
use std::fs::File;
use std::io::Read;
use std::path::PathBuf;
use std::process::{Child, Command, ExitStatus, Stdio};
type SupResult<T> = Result<T, Box<dyn Error>>;
#[derive(Clone, Debug, Deserialize)]
struct App {
name: String,
command: String,
args: Vec<String>,
restart_on_success: Option<bool>,
restart_on_failure: Option<bool>,
restart_on_terminate: Option<bool>,
wait: Option<bool>,
hold: Option<bool>,
}
impl App {
fn wait_start(&self) -> SupResult<()> {
info!("starting application {}", &self.name);
use std::process::ExitStatus;
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)),
}
}
}
use app::App;
use proc::Proc;
use result::Result;
#[derive(Debug, Deserialize)]
struct Config {
@ -60,7 +25,7 @@ struct Config {
}
impl Config {
fn load<T: Into<PathBuf>>(p: T) -> SupResult<Config> {
fn load<T: Into<PathBuf>>(p: T) -> Result<Config> {
let mut file = File::open(p.into())?;
let mut buf = String::new();
file.read_to_string(&mut buf)?;
@ -72,68 +37,6 @@ impl Config {
}
}
struct Proc {
app: App,
child: Child,
}
impl Proc {
fn start(app: App) -> SupResult<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 })
}
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
}
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
}
}
fn main() {
if let Err(e) = rag::init() {
println!("FATAL: Error initializing logger: {:?}", e);
@ -172,7 +75,7 @@ fn start_apps(apps: Vec<App>) -> (Vec<Proc>, Option<i8>) {
(procs, holds)
}
fn run() -> SupResult<()> {
fn run() -> Result<()> {
let config = Config::load("sup.toml")?;
info!("loaded config: sup.toml");

@ -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…
Cancel
Save