-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added example using retries, delay, and cleanup
- Loading branch information
Edouard Poitras
committed
Aug 14, 2024
1 parent
a49f1dc
commit 8be8b1f
Showing
3 changed files
with
60 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters