Skip to content

Commit

Permalink
Merge pull request #24 from tmtmtoo/remove-futures
Browse files Browse the repository at this point in the history
refactor: remove futures
  • Loading branch information
tmtmtoo committed Jan 7, 2024
2 parents 5028e13 + 1af79ba commit 5e6f31b
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 143 deletions.
93 changes: 0 additions & 93 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ derive-getters = "0.3.0"
anyhow = "1.0.78"
tokio = { version = "1.35.1", features = ["rt-multi-thread", "macros", "time", "process", "io-util", "io-std"] }
async-trait = "0.1.76"
futures = "0.3.30"
structopt = "0.3.26"

[dev-dependencies]
Expand Down
4 changes: 2 additions & 2 deletions src/app/components/cmd_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::io::*;

#[derive(new)]
pub struct CmdExecutor<'a> {
pub command: String,
pub command: &'a str,
pub executor: &'a (dyn PipedCmdExecute + Send + Sync),
}

Expand All @@ -11,7 +11,7 @@ impl<'a> super::Component for CmdExecutor<'a> {
type Output = anyhow::Result<Exit>;

async fn handle(&self) -> Self::Output {
let output = self.executor.piped_exec(self.command.as_str()).await?;
let output = self.executor.piped_exec(self.command).await?;
Ok(output)
}
}
8 changes: 4 additions & 4 deletions src/app/components/cmd_not_found.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#[derive(new)]
pub struct PrintableCmdNotFound<C> {
pub command: String,
pub struct PrintableCmdNotFound<'a, C> {
pub command: &'a str,
pub inner: C,
}

#[async_trait::async_trait]
impl<T: 'static, C: super::Component<Output = anyhow::Result<T>> + Send + Sync> super::Component
for PrintableCmdNotFound<C>
impl<'a, T: 'static, C: super::Component<Output = anyhow::Result<T>> + Send + Sync> super::Component
for PrintableCmdNotFound<'a, C>
{
type Output = anyhow::Result<T>;

Expand Down
21 changes: 12 additions & 9 deletions src/app/retry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ where

#[derive(new)]
pub struct SharedParams<'a, C> {
command: String,
command: &'a str,
interval: f64,
executor: &'a (dyn PipedCmdExecute + Send + Sync),
sleeper: &'a (dyn Sleep + Send + Sync),
Expand All @@ -69,7 +69,7 @@ impl<T: 'static, C: Component<Output = T> + Send + Sync> Component for SharedPar
}
}

impl<'a> From<SharedParams<'a, PrintableCmdNotFound<CmdExecutor<'a>>>>
impl<'a> From<SharedParams<'a, PrintableCmdNotFound<'a, CmdExecutor<'a>>>>
for SharedParams<'a, WaitSec<'a>>
{
fn from(state: SharedParams<'a, PrintableCmdNotFound<CmdExecutor>>) -> Self {
Expand All @@ -87,14 +87,14 @@ impl<'a> From<SharedParams<'a, PrintableCmdNotFound<CmdExecutor<'a>>>>
}

impl<'a> From<SharedParams<'a, WaitSec<'a>>>
for SharedParams<'a, PrintableCmdNotFound<CmdExecutor<'a>>>
for SharedParams<'a, PrintableCmdNotFound<'a, CmdExecutor<'a>>>
{
fn from(state: SharedParams<'a, WaitSec>) -> Self {
Self {
inner: PrintableCmdNotFound {
command: state.command.to_owned(),
command: state.command,
inner: CmdExecutor {
command: state.command.to_owned(),
command: state.command,
executor: state.executor,
},
},
Expand All @@ -107,22 +107,25 @@ impl<'a> From<SharedParams<'a, WaitSec<'a>>>
}

impl<'a>
RetryApp<SharedParams<'a, PrintableCmdNotFound<CmdExecutor<'a>>>, SharedParams<'a, WaitSec<'a>>>
RetryApp<
SharedParams<'a, PrintableCmdNotFound<'a, CmdExecutor<'a>>>,
SharedParams<'a, WaitSec<'a>>,
>
{
pub fn new(
command: String,
command: &'a str,
count: Option<usize>,
interval: f64,
executor: &'a (dyn PipedCmdExecute + Send + Sync),
sleeper: &'a (dyn Sleep + Send + Sync),
) -> Self {
Self {
state: State::ExecuteCommand(SharedParams::new(
command.to_owned(),
command,
interval,
executor,
sleeper,
PrintableCmdNotFound::new(command.to_owned(), CmdExecutor::new(command, executor)),
PrintableCmdNotFound::new(command, CmdExecutor::new(command, executor)),
)),
count,
}
Expand Down
20 changes: 10 additions & 10 deletions src/app/supervise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ where

#[derive(new)]
pub struct SharedParams<'a, C> {
command: String,
command: &'a str,
interval: f64,
executor: &'a (dyn PipedCmdExecute + Send + Sync),
sleeper: &'a (dyn Sleep + Send + Sync),
Expand All @@ -64,10 +64,10 @@ impl<T: 'static, C: Component<Output = T> + Send + Sync, 'a> Component for Share
}
}

impl<'a> From<SharedParams<'a, PrintableCmdNotFound<CmdExecutor<'a>>>>
impl<'a> From<SharedParams<'a, PrintableCmdNotFound<'a, CmdExecutor<'a>>>>
for SharedParams<'a, WaitSec<'a>>
{
fn from(state: SharedParams<'a, PrintableCmdNotFound<CmdExecutor<'a>>>) -> Self {
fn from(state: SharedParams<'a, PrintableCmdNotFound<'a, CmdExecutor<'a>>>) -> Self {
Self {
inner: WaitSec {
sec: state.interval,
Expand All @@ -82,14 +82,14 @@ impl<'a> From<SharedParams<'a, PrintableCmdNotFound<CmdExecutor<'a>>>>
}

impl<'a> From<SharedParams<'a, WaitSec<'a>>>
for SharedParams<'a, PrintableCmdNotFound<CmdExecutor<'a>>>
for SharedParams<'a, PrintableCmdNotFound<'a, CmdExecutor<'a>>>
{
fn from(state: SharedParams<'a, WaitSec<'a>>) -> Self {
Self {
inner: PrintableCmdNotFound {
command: state.command.to_owned(),
command: state.command,
inner: CmdExecutor {
command: state.command.to_owned(),
command: state.command,
executor: state.executor,
},
},
Expand All @@ -103,24 +103,24 @@ impl<'a> From<SharedParams<'a, WaitSec<'a>>>

impl<'a>
SuperviseApp<
SharedParams<'a, PrintableCmdNotFound<CmdExecutor<'a>>>,
SharedParams<'a, PrintableCmdNotFound<'a, CmdExecutor<'a>>>,
SharedParams<'a, WaitSec<'a>>,
>
{
pub fn new(
command: String,
command: &'a str,
count: Option<usize>,
interval: f64,
executor: &'a (dyn PipedCmdExecute + Send + Sync),
sleeper: &'a (dyn Sleep + Send + Sync),
) -> Self {
Self {
state: State::ExecuteCommand(SharedParams::new(
command.to_owned(),
command,
interval,
executor,
sleeper,
PrintableCmdNotFound::new(command.to_owned(), CmdExecutor::new(command, executor)),
PrintableCmdNotFound::new(command, CmdExecutor::new(command, executor)),
)),
count,
}
Expand Down
39 changes: 15 additions & 24 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,46 +30,37 @@ use io::*;

#[tokio::main]
async fn main() {
use futures::{future::Either, FutureExt};
use structopt::StructOpt;

let config = Config::from_args();
let executor = PipedCmdExecutor;
let sleeper = Sleeper;

let state_machine = match config {
let exit_code = match config {
Config::retry {
command,
count,
interval,
} => Either::Left(
run(RetryApp::new(
command.join(" "),
count,
interval,
&executor,
&sleeper,
))
.map(|output| match output {
} => {
let command = command.join(" ");
let app = RetryApp::new(&command, count, interval, &executor, &sleeper);
let output = run(app).await;
match output {
RetryResult::Success => 0,
RetryResult::Failure => 1,
}),
),
}
}
Config::supervise {
command,
count,
interval,
} => Either::Right(
run(SuperviseApp::new(
command.join(" "),
count,
interval,
&executor,
&sleeper,
))
.map(|_| 0),
),
} => {
let command = command.join(" ");
let app = SuperviseApp::new(&command, count, interval, &executor, &sleeper);
run(app).await;
0
}
};

std::process::exit(state_machine.await);
std::process::exit(exit_code);
}

0 comments on commit 5e6f31b

Please sign in to comment.