Skip to content

Commit

Permalink
Merge pull request #21 from tmtmtoo/io
Browse files Browse the repository at this point in the history
Io
  • Loading branch information
tmtmtoo authored Jan 5, 2024
2 parents 1c4314c + 6eb216d commit c4fdcac
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/app/components/cmd_executor.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::app::*;
use crate::exec::*;
use crate::io::*;

#[derive(new)]
pub struct CmdExecutor {
pub command: String,
pub executor: std::sync::Arc<dyn PipedCmdExecutor + Send + Sync>,
pub executor: std::sync::Arc<dyn PipedCmdExecute + Send + Sync>,
}

#[async_trait::async_trait]
Expand Down
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;
}
}
12 changes: 9 additions & 3 deletions src/app/retry.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::{components::*, *};
use crate::exec::*;
use crate::io::*;

pub enum RetryResult {
Success,
Expand Down Expand Up @@ -55,7 +55,8 @@ where
pub struct SharedParams<C> {
command: String,
interval: f64,
executor: std::sync::Arc<dyn PipedCmdExecutor + Send + Sync>,
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(tokio_impl::TokioPipedCmdExecutor);
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
12 changes: 9 additions & 3 deletions src/app/supervise.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::{components::*, *};
use crate::exec::*;
use crate::io::*;

enum State<E, S> {
ExecuteCommand(E),
Expand Down Expand Up @@ -50,7 +50,8 @@ where
pub struct SharedParams<C> {
command: String,
interval: f64,
executor: std::sync::Arc<dyn PipedCmdExecutor + Send + Sync>,
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(tokio_impl::TokioPipedCmdExecutor);
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
11 changes: 9 additions & 2 deletions src/exec.rs → src/io.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
pub mod tokio_impl;
mod tokio_impl;

pub use tokio_impl::*;

#[derive(derive_new::new, Debug, Clone, PartialEq, derive_getters::Getters)]
pub struct Exit {
code: i32,
}

#[async_trait::async_trait]
pub trait PipedCmdExecutor {
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);
}
21 changes: 15 additions & 6 deletions src/exec/tokio_impl.rs → src/io/tokio_impl.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use super::*;

pub struct TokioPipedCmdExecutor;
pub struct PipedCmdExecutor;

impl TokioPipedCmdExecutor {
impl PipedCmdExecutor {
fn parse_command(command: &str) -> (String, Vec<String>) {
let mut elements = command.split(' ').map(Into::into).collect::<Vec<_>>();

Expand All @@ -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<Exit> {
let (program, options) = Self::parse_command(command);

Expand Down Expand Up @@ -57,26 +57,35 @@ impl PipedCmdExecutor for TokioPipedCmdExecutor {
}
}

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::*;

#[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();
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ extern crate derive_new;

mod app;
mod config;
mod exec;
mod io;

use app::*;
use config::*;
Expand Down

0 comments on commit c4fdcac

Please sign in to comment.