Skip to content

Commit

Permalink
refactor: isolate tokio
Browse files Browse the repository at this point in the history
  • Loading branch information
tmtmtoo committed Jan 5, 2024
1 parent 8dcf580 commit 6eb216d
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/app/components/wait.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
use crate::app::*;
use crate::io::*;

pub struct WaitSec {
pub sec: f64,
pub sleeper: std::sync::Arc<dyn Sleep + Send + Sync>,
}

#[async_trait::async_trait]
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;
}
}
6 changes: 6 additions & 0 deletions src/app/retry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ pub struct SharedParams<C> {
command: String,
interval: f64,
executor: std::sync::Arc<dyn PipedCmdExecute + Send + Sync>,
sleeper: std::sync::Arc<dyn Sleep + Send + Sync>,
inner: C,
}

Expand All @@ -73,10 +74,12 @@ impl From<SharedParams<PrintableCmdNotFound<CmdExecutor>>> for SharedParams<Wait
Self {
inner: WaitSec {
sec: state.interval,
sleeper: state.sleeper.clone(),
},
command: state.command,
interval: state.interval,
executor: state.executor,
sleeper: state.sleeper,
}
}
}
Expand All @@ -94,19 +97,22 @@ impl From<SharedParams<WaitSec>> for SharedParams<PrintableCmdNotFound<CmdExecut
command: state.command,
interval: state.interval,
executor: state.executor,
sleeper: state.sleeper,
}
}
}

impl RetryApp<SharedParams<PrintableCmdNotFound<CmdExecutor>>, SharedParams<WaitSec>> {
pub fn new(command: String, count: Option<usize>, 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,
Expand Down
6 changes: 6 additions & 0 deletions src/app/supervise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ pub struct SharedParams<C> {
command: String,
interval: f64,
executor: std::sync::Arc<dyn PipedCmdExecute + Send + Sync>,
sleeper: std::sync::Arc<dyn Sleep + Send + Sync>,
inner: C,
}

Expand All @@ -68,10 +69,12 @@ impl From<SharedParams<PrintableCmdNotFound<CmdExecutor>>> for SharedParams<Wait
Self {
inner: WaitSec {
sec: state.interval,
sleeper: state.sleeper.clone(),
},
command: state.command,
interval: state.interval,
executor: state.executor,
sleeper: state.sleeper,
}
}
}
Expand All @@ -89,19 +92,22 @@ impl From<SharedParams<WaitSec>> for SharedParams<PrintableCmdNotFound<CmdExecut
command: state.command,
interval: state.interval,
executor: state.executor,
sleeper: state.sleeper,
}
}
}

impl SuperviseApp<SharedParams<PrintableCmdNotFound<CmdExecutor>>, SharedParams<WaitSec>> {
pub fn new(command: String, count: Option<usize>, 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,
Expand Down
5 changes: 5 additions & 0 deletions src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,8 @@ pub struct Exit {
pub trait PipedCmdExecute {
async fn piped_exec(&self, command: &str) -> std::io::Result<Exit>;
}

#[async_trait::async_trait]
pub trait Sleep {
async fn sleep_sec(&self, sec: f64);
}
9 changes: 9 additions & 0 deletions src/io/tokio_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down

0 comments on commit 6eb216d

Please sign in to comment.