Struct bastion::prelude::Callbacks [−][src]
pub struct Callbacks { /* fields omitted */ }
Expand description
A set of methods that will get called at different states of
a Supervisor
or Children
life.
Example
Bastion::children(|children| {
let callbacks = Callbacks::new()
.with_before_start(|| println!("Children group started."))
.with_after_stop(|| println!("Children group stopped."));
children
.with_callbacks(callbacks)
.with_exec(|ctx| {
// -- Children group started.
async move {
// ...
}
// -- Children group stopped.
})
}).expect("Couldn't create the children group.");
Implementations
Creates a new instance of Callbacks
for
Supervisor::with_callbacks
or Children::with_callbacks
.
Example
Bastion::children(|children| {
let callbacks = Callbacks::new()
.with_before_start(|| println!("Children group started."))
.with_after_stop(|| println!("Children group stopped."));
children
.with_callbacks(callbacks)
.with_exec(|ctx| {
// -- Children group started.
async move {
// ...
}
// -- Children group stopped.
})
}).expect("Couldn't create the children group.");
pub fn with_before_start<C>(self, before_start: C) -> Self where
C: Fn() + Send + Sync + 'static,
pub fn with_before_start<C>(self, before_start: C) -> Self where
C: Fn() + Send + Sync + 'static,
Sets the method that will get called before the Supervisor
or Children
is launched if:
- it was never called before
- or the supervisor of the supervised element using this callback (or the system) decided to restart it and it was already stopped or killed
- or the supervisor of the supervised element using this callback
(or the system) decided to restart it and it wasn’t already
stopped or killed but did not have a callback defined using
with_after_restart
Example
supervisor.children(|children| {
let callbacks = Callbacks::new()
.with_before_start(|| println!("Children group started."))
.with_before_restart(|| println!("Children group restarting."))
.with_after_restart(|| println!("Children group restarted."))
.with_after_stop(|| println!("Children group stopped."));
children
.with_exec(|ctx| {
// -- Children group started.
async move {
// ...
// This will stop the children group...
Ok(())
// Note that because the children group stopped by itself,
// if its supervisor restarts it, its `before_start` callback
// will get called and not `after_restart`.
}
// -- Children group stopped.
})
.with_callbacks(callbacks)
})
pub fn with_after_start<C>(self, after_start: C) -> Self where
C: Fn() + Send + Sync + 'static,
pub fn with_after_start<C>(self, after_start: C) -> Self where
C: Fn() + Send + Sync + 'static,
Sets the method that will get called right after the Supervisor
or Children
is launched.
This method will be called after the child has subscribed to its distributors and dispatchers.
Once the callback has run, the child has caught up it’s message backlog, and is waiting for new messages to process.
Example
supervisor.children(|children| {
let callbacks = Callbacks::new()
.with_after_start(|| println!("Children group ready to process messages."));
children
.with_exec(|ctx| {
// -- Children group started.
// with_after_start called
async move {
// ...
// This will stop the children group...
Ok(())
// Note that because the children group stopped by itself,
// if its supervisor restarts it, its `before_start` callback
// will get called and not `after_restart`.
}
// -- Children group stopped.
})
.with_callbacks(callbacks)
})
pub fn with_before_restart<C>(self, before_restart: C) -> Self where
C: Fn() + Send + Sync + 'static,
pub fn with_before_restart<C>(self, before_restart: C) -> Self where
C: Fn() + Send + Sync + 'static,
Sets the method that will get called before the Supervisor
or Children
is reset if:
- the supervisor of the supervised element using this callback (or the system) decided to restart it and it wasn’t already stopped or killed
Note that if this callback isn’t defined but one was defined using
with_after_stop
, it will get called instead.
Example
supervisor.children(|children| {
let callbacks = Callbacks::new()
.with_before_start(|| println!("Children group started."))
.with_before_restart(|| println!("Children group restarting."))
.with_after_restart(|| println!("Children group restarted."))
.with_after_stop(|| println!("Children group stopped."));
children
.with_exec(|ctx| {
// Once -- Children group started.
// and then -- Children group restarted.
async move {
// ...
// This will make the children group fault and get
// restarted by its supervisor...
Err(())
}
// -- Children group restarting.
// Note that if a `before_restart` wasn't specified for
// this children group, `after_stop` would get called
// instead.
})
.with_callbacks(callbacks)
})
pub fn with_after_restart<C>(self, after_restart: C) -> Self where
C: Fn() + Send + Sync + 'static,
pub fn with_after_restart<C>(self, after_restart: C) -> Self where
C: Fn() + Send + Sync + 'static,
Sets the method that will get called before the Supervisor
or Children
is launched if:
- the supervisor of the supervised element using this callback (or the system) decided to restart it and it wasn’t already stopped or killed
Note that if this callback isn’t defined but one was defined using
with_before_start
, it will get called instead.
Example
supervisor.children(|children| {
let callbacks = Callbacks::new()
.with_before_start(|| println!("Children group started."))
.with_before_restart(|| println!("Children group restarting."))
.with_after_restart(|| println!("Children group restarted."))
.with_after_stop(|| println!("Children group stopped."));
children
.with_exec(|ctx| {
// Once -- Children group started.
// and then -- Children group restarted.
// Note that if a `after_restart` callback wasn't specified
// for this children group, `before_restart` would get called
// instead.
async move {
// ...
// This will make the children group fault and get
// restarted by its supervisor...
Err(())
}
// -- Children group restarting.
})
.with_callbacks(callbacks)
})
Sets the method that will get called after the Supervisor
or Children
is stopped or killed if:
- the supervisor of the supervised element using this callback (or the system) decided to stop (not restart nor kill) it and it wasn’t already stopped or killed
- or the supervisor or children group using this callback stopped or killed itself or was stopped or killed by a reference to it
- or the supervisor of the supervised element using this callback
(or the system) decided to restart it and it wasn’t already
stopped or killed but did not have a callback defined using
with_before_restart
Example
supervisor.children(|children| {
let callbacks = Callbacks::new()
.with_before_start(|| println!("Children group started."))
.with_before_restart(|| println!("Children group restarting."))
.with_after_restart(|| println!("Children group restarted."))
.with_after_stop(|| println!("Children group stopped."));
children
.with_exec(|ctx| {
// -- Children group started.
async move {
// ...
// This will stop the children group...
Ok(())
}
// -- Children group stopped.
// Note that because the children group stopped by itself,
// it its supervisor restarts it, its `before_restart` callback
// will not get called.
})
.with_callbacks(callbacks)
})
Returns whether a callback was defined using with_before_start
.
Example
let callbacks = Callbacks::new()
.with_before_start(|| println!("Children group started."));
assert!(callbacks.has_before_start());
Returns whether a callback was defined using with_before_restart
.
Example
let callbacks = Callbacks::new()
.with_before_restart(|| println!("Children group restarting."));
assert!(callbacks.has_before_restart());
Returns whether a callback was defined using with_after_restart
.
Example
let callbacks = Callbacks::new()
.with_after_restart(|| println!("Children group restarted."));
assert!(callbacks.has_after_restart());
Returns whether a callback was defined using with_after_stop
.
Example
let callbacks = Callbacks::new()
.with_after_stop(|| println!("Children group stopped."));
assert!(callbacks.has_after_stop());
Trait Implementations
Auto Trait Implementations
impl !RefUnwindSafe for Callbacks
impl !UnwindSafe for Callbacks
Blanket Implementations
Mutably borrows from an owned value. Read more
pub fn vzip(self) -> V
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more