retries_and_delay/
retries_and_delay.rs1use bevy::prelude::*;
2use bevy_local_commands::{
3 BevyLocalCommandsPlugin, Delay, LocalCommand, ProcessCompleted, Retry, RetryEvent,
4};
5use std::time::Duration;
6
7fn main() {
8 App::new()
9 .add_plugins((MinimalPlugins, BevyLocalCommandsPlugin))
10 .add_systems(Startup, startup)
11 .add_systems(Update, update)
12 .run();
13}
14
15fn startup(mut commands: Commands) {
16 #[cfg(not(windows))]
18 let cmd =
19 LocalCommand::new("sh").args(["-c", "echo Sleeping for 1s && sleep 1 && THIS SHOULD FAIL"]);
20 #[cfg(windows)]
21 let cmd = LocalCommand::new("cmd").args([
22 "/C",
23 "echo Sleeping for 1s && timeout 1 && THIS SHOULD FAIL",
24 ]);
25
26 let id = commands
27 .spawn((
28 cmd,
29 Retry::Attempts(2),
30 Delay::Fixed(Duration::from_secs(2)),
31 ))
32 .id();
33 println!("Spawned the command as entity {id:?} with 2 retries and a 2s delay");
34}
35
36fn update(
37 mut process_completed_event: MessageReader<ProcessCompleted>,
38 query: Query<&LocalCommand, With<Retry>>,
39 mut retry_events: MessageReader<RetryEvent>,
40) {
41 if let Some(process_completed) = process_completed_event.read().last() {
42 if let Ok(local_command) = query.get(process_completed.entity) {
43 println!(
44 "Command {:?} {:?} completed (Success - {})",
45 local_command.get_program(),
46 local_command.get_args(),
47 process_completed.exit_status.success()
48 );
49 } else {
50 println!("Retry component removed from entity, exiting");
51 std::process::exit(0);
52 }
53 }
54 for retry_event in retry_events.read() {
55 println!("Retry event triggered: {:?}", retry_event);
56 }
57}