Skip to content

Commit

Permalink
Added example using retries, delay, and cleanup
Browse files Browse the repository at this point in the history
Bumped up version.
  • Loading branch information
Edouard Poitras committed Aug 14, 2024
1 parent a49f1dc commit 2affa98
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bevy_local_commands"
version = "0.6.0"
version = "0.6.1"
edition = "2021"
description = "Simple local shell commands for the Bevy game engine"
license = "MIT OR Apache-2.0"
Expand Down
34 changes: 26 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,36 @@ fn get_completed(mut process_completed_event: EventReader<ProcessCompleted>) {
}
```

**Retry and cleanup behavior:**
**Retries:**

```rust
fn retries_and_cleanup_on_completion(mut commands: Commands) {
fn retries(mut commands: Commands) {
commands.spawn((
LocalCommand::new("bash").args(["-c", "sleep 1 && invalid-command --that=fails"]),
// Attempt the command 3 times before giving up
// NOTE: The Retry component will be removed from the entity when no retries are left
Retry::Attempts(3)
// Cleanup::DespawnEntity will despawn the entity upon process completion.
// Cleanup::RemoveComponents will remove this crate's components upon process completion.
Cleanup::DespawnEntity
Retry::Attempts(3) // Attempt the command 3 times before giving up
));
}
```

**Cleanup:**

```rust
fn cleanup_on_completion(mut commands: Commands) {
commands.spawn((
LocalCommand::new("bash").args(["-c", "sleep 1"]),
Cleanup::DespawnEntity // Will despawn the entity upon process completion
// Cleanup::RemoveComponents // Will remove only this crate's components upon process completion
));
}
```

**Delay:**

```rust
fn delay_process_start(mut commands: Commands) {
commands.spawn((
LocalCommand::new("bash").args(["-c", "sleep 1"]),
Delay::Fixed(Duration::from_secs(2)), // Start the process after a 2s delay (applies to each retry)
));
}
```
Expand Down
6 changes: 1 addition & 5 deletions examples/retries_and_delay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,14 @@ fn startup(mut commands: Commands) {
"echo Sleeping for 1s && timeout 1 && THIS SHOULD FAIL",
]);

// EDDIE
// Add examples for retry + delay + cleanup
// Then work on implementing chain

let id = commands
.spawn((
cmd,
Retry::Attempts(2),
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
1 change: 1 addition & 0 deletions src/addons/chain.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use bevy::prelude::*;

// Coming soon.
#[derive(Debug, Component)]
pub enum Chain {}

0 comments on commit 2affa98

Please sign in to comment.