From 755b28e06054fe2308476aa87aa85995de7ee7e4 Mon Sep 17 00:00:00 2001 From: matsu Date: Fri, 5 Jan 2024 14:09:31 +0000 Subject: [PATCH 1/3] refactor: rename for io --- src/app/components/cmd_executor.rs | 2 +- src/app/retry.rs | 4 ++-- src/app/supervise.rs | 4 ++-- src/{exec.rs => io.rs} | 4 +++- src/{exec => io}/tokio_impl.rs | 0 src/main.rs | 2 +- 6 files changed, 9 insertions(+), 7 deletions(-) rename src/{exec.rs => io.rs} (85%) rename src/{exec => io}/tokio_impl.rs (100%) diff --git a/src/app/components/cmd_executor.rs b/src/app/components/cmd_executor.rs index e3f3c8c..09426cf 100644 --- a/src/app/components/cmd_executor.rs +++ b/src/app/components/cmd_executor.rs @@ -1,5 +1,5 @@ use crate::app::*; -use crate::exec::*; +use crate::io::*; #[derive(new)] pub struct CmdExecutor { diff --git a/src/app/retry.rs b/src/app/retry.rs index 73c3224..5df0670 100644 --- a/src/app/retry.rs +++ b/src/app/retry.rs @@ -1,5 +1,5 @@ use super::{components::*, *}; -use crate::exec::*; +use crate::io::*; pub enum RetryResult { Success, @@ -100,7 +100,7 @@ impl From> for SharedParams>, SharedParams> { pub fn new(command: String, count: Option, interval: f64) -> Self { - let executor = std::sync::Arc::new(tokio_impl::TokioPipedCmdExecutor); + let executor = std::sync::Arc::new(TokioPipedCmdExecutor); Self { state: State::ExecuteCommand(SharedParams::new( diff --git a/src/app/supervise.rs b/src/app/supervise.rs index 3b53db9..26abc81 100644 --- a/src/app/supervise.rs +++ b/src/app/supervise.rs @@ -1,5 +1,5 @@ use super::{components::*, *}; -use crate::exec::*; +use crate::io::*; enum State { ExecuteCommand(E), @@ -95,7 +95,7 @@ impl From> for SharedParams>, SharedParams> { pub fn new(command: String, count: Option, interval: f64) -> Self { - let executor = std::sync::Arc::new(tokio_impl::TokioPipedCmdExecutor); + let executor = std::sync::Arc::new(TokioPipedCmdExecutor); Self { state: State::ExecuteCommand(SharedParams::new( diff --git a/src/exec.rs b/src/io.rs similarity index 85% rename from src/exec.rs rename to src/io.rs index f63f406..4b04051 100644 --- a/src/exec.rs +++ b/src/io.rs @@ -1,4 +1,6 @@ -pub mod tokio_impl; +mod tokio_impl; + +pub use tokio_impl::*; #[derive(derive_new::new, Debug, Clone, PartialEq, derive_getters::Getters)] pub struct Exit { diff --git a/src/exec/tokio_impl.rs b/src/io/tokio_impl.rs similarity index 100% rename from src/exec/tokio_impl.rs rename to src/io/tokio_impl.rs diff --git a/src/main.rs b/src/main.rs index 699a304..5c5143a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -22,7 +22,7 @@ extern crate derive_new; mod app; mod config; -mod exec; +mod io; use app::*; use config::*; From 8dcf58017178eac06c16ef55383a433962d968f4 Mon Sep 17 00:00:00 2001 From: matsu Date: Fri, 5 Jan 2024 14:15:31 +0000 Subject: [PATCH 2/3] refactor: rename piped command executor --- src/app/components/cmd_executor.rs | 2 +- src/app/retry.rs | 4 ++-- src/app/supervise.rs | 4 ++-- src/io.rs | 2 +- src/io/tokio_impl.rs | 12 ++++++------ 5 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/app/components/cmd_executor.rs b/src/app/components/cmd_executor.rs index 09426cf..10d4b11 100644 --- a/src/app/components/cmd_executor.rs +++ b/src/app/components/cmd_executor.rs @@ -4,7 +4,7 @@ use crate::io::*; #[derive(new)] pub struct CmdExecutor { pub command: String, - pub executor: std::sync::Arc, + pub executor: std::sync::Arc, } #[async_trait::async_trait] diff --git a/src/app/retry.rs b/src/app/retry.rs index 5df0670..0e0c20f 100644 --- a/src/app/retry.rs +++ b/src/app/retry.rs @@ -55,7 +55,7 @@ where pub struct SharedParams { command: String, interval: f64, - executor: std::sync::Arc, + executor: std::sync::Arc, inner: C, } @@ -100,7 +100,7 @@ impl From> for SharedParams>, SharedParams> { pub fn new(command: String, count: Option, interval: f64) -> Self { - let executor = std::sync::Arc::new(TokioPipedCmdExecutor); + let executor = std::sync::Arc::new(PipedCmdExecutor); Self { state: State::ExecuteCommand(SharedParams::new( diff --git a/src/app/supervise.rs b/src/app/supervise.rs index 26abc81..4a428bf 100644 --- a/src/app/supervise.rs +++ b/src/app/supervise.rs @@ -50,7 +50,7 @@ where pub struct SharedParams { command: String, interval: f64, - executor: std::sync::Arc, + executor: std::sync::Arc, inner: C, } @@ -95,7 +95,7 @@ impl From> for SharedParams>, SharedParams> { pub fn new(command: String, count: Option, interval: f64) -> Self { - let executor = std::sync::Arc::new(TokioPipedCmdExecutor); + let executor = std::sync::Arc::new(PipedCmdExecutor); Self { state: State::ExecuteCommand(SharedParams::new( diff --git a/src/io.rs b/src/io.rs index 4b04051..e01856a 100644 --- a/src/io.rs +++ b/src/io.rs @@ -8,6 +8,6 @@ pub struct Exit { } #[async_trait::async_trait] -pub trait PipedCmdExecutor { +pub trait PipedCmdExecute { async fn piped_exec(&self, command: &str) -> std::io::Result; } diff --git a/src/io/tokio_impl.rs b/src/io/tokio_impl.rs index 4b78da8..45922eb 100644 --- a/src/io/tokio_impl.rs +++ b/src/io/tokio_impl.rs @@ -1,8 +1,8 @@ use super::*; -pub struct TokioPipedCmdExecutor; +pub struct PipedCmdExecutor; -impl TokioPipedCmdExecutor { +impl PipedCmdExecutor { fn parse_command(command: &str) -> (String, Vec) { let mut elements = command.split(' ').map(Into::into).collect::>(); @@ -18,7 +18,7 @@ impl TokioPipedCmdExecutor { } #[async_trait::async_trait] -impl PipedCmdExecutor for TokioPipedCmdExecutor { +impl PipedCmdExecute for PipedCmdExecutor { async fn piped_exec(&self, command: &str) -> std::io::Result { let (program, options) = Self::parse_command(command); @@ -63,20 +63,20 @@ mod tests { #[tokio::test] async fn should_success_given_suitable_command() { - let actual = TokioPipedCmdExecutor.piped_exec("echo abcd").await.unwrap(); + let actual = PipedCmdExecutor.piped_exec("echo abcd").await.unwrap(); let expected = Exit { code: 0 }; assert_eq!(actual, expected); } #[tokio::test] async fn should_failure_when_command_not_found() { - let actual = TokioPipedCmdExecutor.piped_exec("failed").await.is_err(); + let actual = PipedCmdExecutor.piped_exec("failed").await.is_err(); assert!(actual); } #[tokio::test] async fn should_success_when_exit_not_zero() { - let actual = TokioPipedCmdExecutor + let actual = PipedCmdExecutor .piped_exec("cat non_existent_file") .await .unwrap(); From 6eb216d75b8355d8cfcbe302e7a38ae9b3f25aff Mon Sep 17 00:00:00 2001 From: matsu Date: Fri, 5 Jan 2024 14:38:17 +0000 Subject: [PATCH 3/3] refactor: isolate tokio --- src/app/components/wait.rs | 4 +++- src/app/retry.rs | 6 ++++++ src/app/supervise.rs | 6 ++++++ src/io.rs | 5 +++++ src/io/tokio_impl.rs | 9 +++++++++ 5 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/app/components/wait.rs b/src/app/components/wait.rs index 7c24242..e683e37 100644 --- a/src/app/components/wait.rs +++ b/src/app/components/wait.rs @@ -1,7 +1,9 @@ use crate::app::*; +use crate::io::*; pub struct WaitSec { pub sec: f64, + pub sleeper: std::sync::Arc, } #[async_trait::async_trait] @@ -9,6 +11,6 @@ impl Component for WaitSec { type Output = (); async fn handle(&self) -> Self::Output { - tokio::time::sleep(tokio::time::Duration::from_secs_f64(self.sec)).await + self.sleeper.sleep_sec(self.sec).await; } } diff --git a/src/app/retry.rs b/src/app/retry.rs index 0e0c20f..1db0e95 100644 --- a/src/app/retry.rs +++ b/src/app/retry.rs @@ -56,6 +56,7 @@ pub struct SharedParams { command: String, interval: f64, executor: std::sync::Arc, + sleeper: std::sync::Arc, inner: C, } @@ -73,10 +74,12 @@ impl From>> for SharedParams> for SharedParams> 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); Self { state: State::ExecuteCommand(SharedParams::new( command.to_owned(), interval, executor.clone(), + sleeper, PrintableCmdNotFound::new(command.to_owned(), CmdExecutor::new(command, executor)), )), count, diff --git a/src/app/supervise.rs b/src/app/supervise.rs index 4a428bf..c0ebd7b 100644 --- a/src/app/supervise.rs +++ b/src/app/supervise.rs @@ -51,6 +51,7 @@ pub struct SharedParams { command: String, interval: f64, executor: std::sync::Arc, + sleeper: std::sync::Arc, inner: C, } @@ -68,10 +69,12 @@ impl From>> for SharedParams> for SharedParams> 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); Self { state: State::ExecuteCommand(SharedParams::new( command.to_owned(), interval, executor.clone(), + sleeper, PrintableCmdNotFound::new(command.to_owned(), CmdExecutor::new(command, executor)), )), count, diff --git a/src/io.rs b/src/io.rs index e01856a..1e24a0d 100644 --- a/src/io.rs +++ b/src/io.rs @@ -11,3 +11,8 @@ pub struct Exit { pub trait PipedCmdExecute { async fn piped_exec(&self, command: &str) -> std::io::Result; } + +#[async_trait::async_trait] +pub trait Sleep { + async fn sleep_sec(&self, sec: f64); +} diff --git a/src/io/tokio_impl.rs b/src/io/tokio_impl.rs index 45922eb..20248aa 100644 --- a/src/io/tokio_impl.rs +++ b/src/io/tokio_impl.rs @@ -57,6 +57,15 @@ impl PipedCmdExecute for PipedCmdExecutor { } } +pub struct Sleeper; + +#[async_trait::async_trait] +impl Sleep for Sleeper { + async fn sleep_sec(&self, sec: f64) { + tokio::time::sleep(tokio::time::Duration::from_secs_f64(sec)).await; + } +} + #[cfg(test)] mod tests { use super::*;