Skip to content

Commit

Permalink
Added example using retries, delay, and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Edouard Poitras committed Aug 14, 2024
1 parent a49f1dc commit 8be8b1f
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
2 changes: 1 addition & 1 deletion examples/retries_and_delay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,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(
Expand Down
58 changes: 58 additions & 0 deletions examples/retries_and_delay_and_cleanup.rs
Original file line number Diff line number Diff line change
@@ -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<ProcessCompleted>,
query: Query<&LocalCommand, With<Retry>>,
mut retry_events: EventReader<RetryEvent>,
) {
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);
}
}
1 change: 1 addition & 0 deletions examples/run_all_examples.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 8be8b1f

Please sign in to comment.