retries_and_remove/
retries_and_remove.rs1use bevy::prelude::*;
2use bevy_local_commands::{
3 BevyLocalCommandsPlugin, Cleanup, LocalCommand, Process, ProcessCompleted, Retry, RetryEvent,
4};
5
6fn main() {
7 App::new()
8 .add_plugins((MinimalPlugins, BevyLocalCommandsPlugin))
9 .add_systems(Startup, startup)
10 .add_systems(Update, update)
11 .run();
12}
13
14fn startup(mut commands: Commands) {
15 #[cfg(not(windows))]
17 let cmd = LocalCommand::new("sh").args(["-c", "echo Sleeping for 1s && sleep 1 && INVALID "]);
18 #[cfg(windows)]
19 let cmd = LocalCommand::new("cmd").args(["/C", "echo Sleeping for 1s && timeout 1 && INVALID"]);
20
21 let id = commands
22 .spawn((cmd, Retry::Attempts(3), Cleanup::RemoveComponents))
23 .id();
24 println!("Spawned the command as temporary entity {id:?} with 3 retries");
25}
26
27fn update(
28 mut process_completed_event: MessageReader<ProcessCompleted>,
29 mut retry_events: MessageReader<RetryEvent>,
30 query: Query<(
31 Entity,
32 Option<&LocalCommand>,
33 Option<&Process>,
34 Option<&Retry>,
35 Option<&Cleanup>,
36 )>,
37) {
38 for retry_event in retry_events.read() {
39 println!("Retry event triggered: {:?}", retry_event);
40 let components = query.get(retry_event.entity).unwrap();
41 assert!(components.1.is_some());
42 assert!(components.2.is_none());
43 assert!(components.3.is_some());
44 assert!(components.4.is_some());
45 }
46 for process_completed in process_completed_event.read() {
47 println!("{:?}", process_completed);
48 let components = query.get(process_completed.entity).unwrap();
49 assert!(components.1.is_none());
50 assert!(components.2.is_none());
51 assert!(components.3.is_none());
52 assert!(components.4.is_none());
53 std::process::exit(0);
54 }
55}