From 2affa98f503b20b2eeb28067e2c1cdc8f4ebd56e Mon Sep 17 00:00:00 2001 From: Edouard Poitras Date: Wed, 14 Aug 2024 16:48:41 -0400 Subject: [PATCH] Added example using retries, delay, and cleanup Bumped up version. --- Cargo.lock | 2 +- Cargo.toml | 2 +- README.md | 34 +++++++++---- examples/retries_and_delay.rs | 6 +-- examples/retries_and_delay_and_cleanup.rs | 58 +++++++++++++++++++++++ examples/run_all_examples.sh | 1 + src/addons/chain.rs | 1 + 7 files changed, 89 insertions(+), 15 deletions(-) create mode 100644 examples/retries_and_delay_and_cleanup.rs diff --git a/Cargo.lock b/Cargo.lock index ab98da2..9cf9e6c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -477,7 +477,7 @@ dependencies = [ [[package]] name = "bevy_local_commands" -version = "0.6.0" +version = "0.6.1" dependencies = [ "bevy", ] diff --git a/Cargo.toml b/Cargo.toml index 3ae6f20..155f986 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "bevy_local_commands" -version = "0.6.0" +version = "0.6.1" edition = "2021" description = "Simple local shell commands for the Bevy game engine" license = "MIT OR Apache-2.0" diff --git a/README.md b/README.md index 9d652d1..a823587 100644 --- a/README.md +++ b/README.md @@ -83,18 +83,36 @@ fn get_completed(mut process_completed_event: EventReader) { } ``` -**Retry and cleanup behavior:** +**Retries:** ```rust -fn retries_and_cleanup_on_completion(mut commands: Commands) { +fn retries(mut commands: Commands) { commands.spawn(( LocalCommand::new("bash").args(["-c", "sleep 1 && invalid-command --that=fails"]), - // Attempt the command 3 times before giving up - // NOTE: The Retry component will be removed from the entity when no retries are left - Retry::Attempts(3) - // Cleanup::DespawnEntity will despawn the entity upon process completion. - // Cleanup::RemoveComponents will remove this crate's components upon process completion. - Cleanup::DespawnEntity + Retry::Attempts(3) // Attempt the command 3 times before giving up + )); +} +``` + +**Cleanup:** + +```rust +fn cleanup_on_completion(mut commands: Commands) { + commands.spawn(( + LocalCommand::new("bash").args(["-c", "sleep 1"]), + Cleanup::DespawnEntity // Will despawn the entity upon process completion + // Cleanup::RemoveComponents // Will remove only this crate's components upon process completion + )); +} +``` + +**Delay:** + +```rust +fn delay_process_start(mut commands: Commands) { + commands.spawn(( + LocalCommand::new("bash").args(["-c", "sleep 1"]), + Delay::Fixed(Duration::from_secs(2)), // Start the process after a 2s delay (applies to each retry) )); } ``` diff --git a/examples/retries_and_delay.rs b/examples/retries_and_delay.rs index dccdad2..3fbe4d1 100644 --- a/examples/retries_and_delay.rs +++ b/examples/retries_and_delay.rs @@ -23,10 +23,6 @@ fn startup(mut commands: Commands) { "echo Sleeping for 1s && timeout 1 && THIS SHOULD FAIL", ]); - // EDDIE - // Add examples for retry + delay + cleanup - // Then work on implementing chain - let id = commands .spawn(( cmd, @@ -34,7 +30,7 @@ fn startup(mut commands: Commands) { Delay::Fixed(Duration::from_secs(2)), )) .id(); - println!("Spawned the command as entity {id:?} with 2 retries and a 3s delay"); + println!("Spawned the command as entity {id:?} with 2 retries and a 2s delay"); } fn update( diff --git a/examples/retries_and_delay_and_cleanup.rs b/examples/retries_and_delay_and_cleanup.rs new file mode 100644 index 0000000..754b00c --- /dev/null +++ b/examples/retries_and_delay_and_cleanup.rs @@ -0,0 +1,58 @@ +use bevy::prelude::*; +use bevy_local_commands::{ + BevyLocalCommandsPlugin, Cleanup, Delay, LocalCommand, ProcessCompleted, Retry, RetryEvent, +}; +use std::time::Duration; + +fn main() { + App::new() + .add_plugins((MinimalPlugins, BevyLocalCommandsPlugin)) + .add_systems(Startup, startup) + .add_systems(Update, update) + .run(); +} + +fn startup(mut commands: Commands) { + // Choose the command based on the OS + #[cfg(not(windows))] + let cmd = + LocalCommand::new("sh").args(["-c", "echo Sleeping for 1s && sleep 1 && THIS SHOULD FAIL"]); + #[cfg(windows)] + let cmd = LocalCommand::new("cmd").args([ + "/C", + "echo Sleeping for 1s && timeout 1 && THIS SHOULD FAIL", + ]); + + let id = commands + .spawn(( + cmd, + Retry::Attempts(2), + Delay::Fixed(Duration::from_secs(2)), + Cleanup::RemoveComponents, + )) + .id(); + println!("Spawned the command as entity {id:?} with 2 retries and a 2s delay"); +} + +fn update( + mut process_completed_event: EventReader, + query: Query<&LocalCommand, With>, + mut retry_events: EventReader, +) { + if let Some(process_completed) = process_completed_event.read().last() { + if let Ok(local_command) = query.get(process_completed.entity) { + println!( + "Command {:?} {:?} completed (Success - {})", + local_command.get_program(), + local_command.get_args(), + process_completed.exit_status.success() + ); + } else { + println!("Retry component removed from entity, exiting"); + std::process::exit(0); + } + } + for retry_event in retry_events.read() { + println!("Retry event triggered: {:?}", retry_event); + } +} diff --git a/examples/run_all_examples.sh b/examples/run_all_examples.sh index 744715d..6f8b24b 100755 --- a/examples/run_all_examples.sh +++ b/examples/run_all_examples.sh @@ -2,6 +2,7 @@ cargo run --example despawn_on_completion cargo run --example error cargo run --example input cargo run --example kill +cargo run --example retries_and_delay_and_cleanup cargo run --example retries_and_delay cargo run --example retries_and_remove cargo run --example simple \ No newline at end of file diff --git a/src/addons/chain.rs b/src/addons/chain.rs index 7981f90..4e793f9 100644 --- a/src/addons/chain.rs +++ b/src/addons/chain.rs @@ -1,4 +1,5 @@ use bevy::prelude::*; +// Coming soon. #[derive(Debug, Component)] pub enum Chain {}