chain_failure_delay_retries/
chain_failure_delay_retries.rs1use std::time::Duration;
2
3use bevy::prelude::*;
4use bevy_local_commands::{
5 BevyLocalCommandsPlugin, Chain, ChainCompletedEvent, Delay, LocalCommand, ProcessCompleted,
6 ProcessOutput, Retry, RetryEvent,
7};
8
9fn main() {
10 App::new()
11 .add_plugins((MinimalPlugins, BevyLocalCommandsPlugin))
12 .add_systems(Startup, setup)
13 .add_systems(Update, update)
14 .run();
15}
16
17fn setup(mut commands: Commands) {
18 #[cfg(not(windows))]
20 let chain = Chain::new(vec![
21 LocalCommand::new("sh").args(["-c", "echo 'First command'"]),
22 LocalCommand::new("sh").args(["-c", "exit 1"]), LocalCommand::new("sh").args(["-c", "echo 'Third command'"]),
24 ]);
25 #[cfg(windows)]
26 let chain = Chain::new(vec![
27 LocalCommand::new("powershell").args(["echo 'First command'"]),
28 LocalCommand::new("powershell").args(["exit 1"]), LocalCommand::new("powershell").args(["echo 'Third command'"]),
30 ]);
31 let id = commands
32 .spawn((
33 chain,
34 Retry::Attempts(2),
35 Delay::Fixed(Duration::from_secs(2)),
36 ))
37 .id();
38 println!("Spawned the chain as entity {id:?}");
39}
40
41fn update(
42 mut process_output_event: MessageReader<ProcessOutput>,
43 mut process_completed_event: MessageReader<ProcessCompleted>,
44 mut process_chain_completed_event: MessageReader<ChainCompletedEvent>,
45 mut process_retry_event: MessageReader<RetryEvent>,
46 chain_query: Query<(), With<Chain>>,
47) {
48 for process_output in process_output_event.read() {
49 for line in process_output.lines() {
50 println!("Output Line ({:?}): {line}", process_output.entity);
51 }
52 }
53
54 for process_retry in process_retry_event.read() {
55 println!(
56 "Command for entity {:?} failed, retrying ({} left)",
57 process_retry.entity, process_retry.retries_left,
58 );
59 }
60
61 for process_completed in process_completed_event.read() {
62 println!(
63 "Command {:?} completed (Success - {})",
64 process_completed.entity,
65 process_completed.exit_status.success()
66 );
67 }
68
69 for process_chain_completed in process_chain_completed_event.read() {
70 println!(
71 "Chain of commands completed for entity {} (Success - {})",
72 process_chain_completed.entity, process_chain_completed.success,
73 );
74 }
75
76 if chain_query.is_empty() {
78 println!("Chain commands done. Exiting the app.");
79 std::process::exit(0);
80 }
81}