diff --git a/src/app/components/cmd_executor.rs b/src/app/components/cmd_executor.rs index f45e488..fc95c4a 100644 --- a/src/app/components/cmd_executor.rs +++ b/src/app/components/cmd_executor.rs @@ -1,13 +1,13 @@ use crate::io::*; #[derive(new)] -pub struct CmdExecutor { +pub struct CmdExecutor<'a> { pub command: String, - pub executor: std::sync::Arc, + pub executor: &'a (dyn PipedCmdExecute + Send + Sync), } #[async_trait::async_trait] -impl super::Component for CmdExecutor { +impl<'a> super::Component for CmdExecutor<'a> { type Output = anyhow::Result; async fn handle(&self) -> Self::Output { diff --git a/src/app/components/wait.rs b/src/app/components/wait.rs index fcb5c2c..8a2809d 100644 --- a/src/app/components/wait.rs +++ b/src/app/components/wait.rs @@ -1,12 +1,12 @@ use crate::io::*; -pub struct WaitSec { +pub struct WaitSec<'a> { pub sec: f64, - pub sleeper: std::sync::Arc, + pub sleeper: &'a (dyn Sleep + Send + Sync), } #[async_trait::async_trait] -impl super::Component for WaitSec { +impl<'a> super::Component for WaitSec<'a> { type Output = (); async fn handle(&self) -> Self::Output { diff --git a/src/app/retry.rs b/src/app/retry.rs index 1db0e95..78a2dea 100644 --- a/src/app/retry.rs +++ b/src/app/retry.rs @@ -52,16 +52,16 @@ where } #[derive(new)] -pub struct SharedParams { +pub struct SharedParams<'a, C> { command: String, interval: f64, - executor: std::sync::Arc, - sleeper: std::sync::Arc, + executor: &'a (dyn PipedCmdExecute + Send + Sync), + sleeper: &'a (dyn Sleep + Send + Sync), inner: C, } #[async_trait::async_trait] -impl + Send + Sync> Component for SharedParams { +impl + Send + Sync> Component for SharedParams<'_, C> { type Output = T; async fn handle(&self) -> Self::Output { @@ -69,8 +69,10 @@ impl + Send + Sync> Component for SharedPar } } -impl From>> for SharedParams { - fn from(state: SharedParams>) -> Self { +impl<'a> From>>> + for SharedParams<'a, WaitSec<'a>> +{ + fn from(state: SharedParams<'a, PrintableCmdNotFound>) -> Self { Self { inner: WaitSec { sec: state.interval, @@ -84,14 +86,16 @@ impl From>> for SharedParams> for SharedParams> { - fn from(state: SharedParams) -> Self { +impl<'a> From>> + for SharedParams<'a, PrintableCmdNotFound>> +{ + fn from(state: SharedParams<'a, WaitSec>) -> Self { Self { inner: PrintableCmdNotFound { command: state.command.to_owned(), inner: CmdExecutor { command: state.command.to_owned(), - executor: state.executor.clone(), + executor: state.executor, }, }, command: state.command, @@ -102,16 +106,21 @@ impl From> for SharedParams>, SharedParams> { - pub fn new(command: String, count: Option, interval: f64) -> Self { - let executor = std::sync::Arc::new(PipedCmdExecutor); - let sleeper = std::sync::Arc::new(Sleeper); - +impl<'a> + RetryApp>>, SharedParams<'a, WaitSec<'a>>> +{ + pub fn new( + command: String, + count: Option, + interval: f64, + executor: &'a (dyn PipedCmdExecute + Send + Sync), + sleeper: &'a (dyn Sleep + Send + Sync), + ) -> Self { Self { state: State::ExecuteCommand(SharedParams::new( command.to_owned(), interval, - executor.clone(), + executor, sleeper, PrintableCmdNotFound::new(command.to_owned(), CmdExecutor::new(command, executor)), )), diff --git a/src/app/supervise.rs b/src/app/supervise.rs index c0ebd7b..1a89c1b 100644 --- a/src/app/supervise.rs +++ b/src/app/supervise.rs @@ -47,16 +47,16 @@ where } #[derive(new)] -pub struct SharedParams { +pub struct SharedParams<'a, C> { command: String, interval: f64, - executor: std::sync::Arc, - sleeper: std::sync::Arc, + executor: &'a (dyn PipedCmdExecute + Send + Sync), + sleeper: &'a (dyn Sleep + Send + Sync), inner: C, } #[async_trait::async_trait] -impl + Send + Sync> Component for SharedParams { +impl + Send + Sync, 'a> Component for SharedParams<'a, C> { type Output = T; async fn handle(&self) -> Self::Output { @@ -64,8 +64,10 @@ impl + Send + Sync> Component for SharedPar } } -impl From>> for SharedParams { - fn from(state: SharedParams>) -> Self { +impl<'a> From>>> + for SharedParams<'a, WaitSec<'a>> +{ + fn from(state: SharedParams<'a, PrintableCmdNotFound>>) -> Self { Self { inner: WaitSec { sec: state.interval, @@ -79,14 +81,16 @@ impl From>> for SharedParams> for SharedParams> { - fn from(state: SharedParams) -> Self { +impl<'a> From>> + for SharedParams<'a, PrintableCmdNotFound>> +{ + fn from(state: SharedParams<'a, WaitSec<'a>>) -> Self { Self { inner: PrintableCmdNotFound { command: state.command.to_owned(), inner: CmdExecutor { command: state.command.to_owned(), - executor: state.executor.clone(), + executor: &*state.executor, }, }, command: state.command, @@ -97,16 +101,24 @@ impl From> for SharedParams>, SharedParams> { - pub fn new(command: String, count: Option, interval: f64) -> Self { - let executor = std::sync::Arc::new(PipedCmdExecutor); - let sleeper = std::sync::Arc::new(Sleeper); - +impl<'a> + SuperviseApp< + SharedParams<'a, PrintableCmdNotFound>>, + SharedParams<'a, WaitSec<'a>>, + > +{ + pub fn new( + command: String, + count: Option, + interval: f64, + executor: &'a (dyn PipedCmdExecute + Send + Sync), + sleeper: &'a (dyn Sleep + Send + Sync), + ) -> Self { Self { state: State::ExecuteCommand(SharedParams::new( command.to_owned(), interval, - executor.clone(), + executor, sleeper, PrintableCmdNotFound::new(command.to_owned(), CmdExecutor::new(command, executor)), )), diff --git a/src/main.rs b/src/main.rs index 5c5143a..b69492a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -26,6 +26,7 @@ mod io; use app::*; use config::*; +use io::*; #[tokio::main] async fn main() { @@ -33,6 +34,8 @@ async fn main() { use structopt::StructOpt; let config = Config::from_args(); + let executor = PipedCmdExecutor; + let sleeper = Sleeper; let state_machine = match config { Config::retry { @@ -40,7 +43,14 @@ async fn main() { count, interval, } => Either::Left( - run(RetryApp::new(command.join(" "), count, interval)).map(|output| match output { + run(RetryApp::new( + command.join(" "), + count, + interval, + &executor, + &sleeper, + )) + .map(|output| match output { RetryResult::Success => 0, RetryResult::Failure => 1, }), @@ -49,7 +59,16 @@ async fn main() { command, count, interval, - } => Either::Right(run(SuperviseApp::new(command.join(" "), count, interval)).map(|_| 0)), + } => Either::Right( + run(SuperviseApp::new( + command.join(" "), + count, + interval, + &executor, + &sleeper, + )) + .map(|_| 0), + ), }; std::process::exit(state_machine.await);