-
-
Notifications
You must be signed in to change notification settings - Fork 102
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
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d2b3d5a
Implemented max retries for restoring actors
Relrin c7b64b7
Added example for the supervisor with recovery strategy
Relrin 4c79d7a
Changed signature of the Supervisor::restart method
Relrin f0e4354
Changed signature of the Supervisor::kill method
Relrin af58a40
Changed signature of the Supervisor::stop method
Relrin 1cb4b48
Added saving old counters for the failed actors
Relrin cffa1cf
Replaced max_retries on the RestartPolicy enum
Relrin a006c89
Updated example with restart_strategy
Relrin 638fefd
Added tests for the RestartStrategy struct
Relrin 87b4082
Fixed formatting warnings
Relrin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
use std::time::Duration; | ||
|
||
use bastion::prelude::*; | ||
|
||
/// | ||
/// 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..."); | ||
}) | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.