Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Max retries support for existing restart strategies #157

Merged
merged 10 commits into from
Jan 22, 2020
60 changes: 60 additions & 0 deletions bastion/examples/restart_strategy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use std::time::Duration;

use bastion::prelude::*;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We mostly write a prelude intro to our examples to explain where an example can be used for and what's the intent. I would like to have that. You might want to take a gaze to this one:
e.g. https://github.com/bastion-rs/bastion/blob/master/bastion/examples/parallel_computation.rs#L4

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

///
/// Supervisors with a custom restart strategy example.
///
/// Prologue:
/// This examples demonstrates how to override the default restart strategy
/// on a custom, provided by the bastion crate. The supervisor will spawn
/// the only one tracked actor that will be restarted a couple of times and
/// the certain timeout after a raised failure in the actor's code.
///
fn main() {
Bastion::init();

Bastion::supervisor(supervisor).expect("Couldn't create the supervisor.");

Bastion::start();
Bastion::block_until_stopped();
}

fn supervisor(supervisor: Supervisor) -> Supervisor {
// Here we are specifying the used restart strategy for our supervisor.
// By default the bastion's supervisors are always trying to restart
// failed actors with unlimited amount of tries.
//
// At the beginning we're creating a new instance of RestartStrategy
// and then provides a policy and a back-off strategy.
let restart_strategy = RestartStrategy::default()
// Set the limits for supervisor, so that it could stop
// after 3 attempts. If the actor can't be started, the supervisor
// will remove the failed actor from tracking.
.with_restart_policy(RestartPolicy::Tries(3))
// Set the desired restart strategy. By default supervisor will
// try to restore the failed actor as soon as possible. However,
// in our case we want to restart with a small delay between the
// tries. Let's say that we want a regular time interval between the
// attempts which is equal to 1 second.
.with_actor_restart_strategy(ActorRestartStrategy::LinearBackOff {
timeout: Duration::from_secs(1),
});

// After it we define the supervisor...
supervisor
// That uses our restart strategy defined earlier
.with_restart_strategy(restart_strategy)
// And tracks the child group, defined in the following function
.children(|children| failed_actors_group(children))
}

fn failed_actors_group(children: Children) -> Children {
// Specifying the child group, where each actor
// will output the sentence in the stdout, then will fail
// with panic.
children.with_exec(move |_ctx: BastionContext| async move {
println!("Worker started!");
panic!("Unexpected error...");
})
}
3 changes: 2 additions & 1 deletion bastion/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ pub mod prelude {
pub use crate::msg;
pub use crate::path::{BastionPath, BastionPathElement};
pub use crate::supervisor::{
ActorRestartStrategy, SupervisionStrategy, Supervisor, SupervisorRef,
ActorRestartStrategy, RestartPolicy, RestartStrategy, SupervisionStrategy, Supervisor,
SupervisorRef,
};
pub use crate::{blocking, children, run, spawn, supervisor};
}
Loading