pub struct Callbacks { /* private fields */ }
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§
Source§impl Callbacks
impl Callbacks
Sourcepub fn new() -> Self
pub fn new() -> Self
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.");
Sourcepub fn with_before_start<C>(self, before_start: C) -> Self
pub fn with_before_start<C>(self, before_start: C) -> Self
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)
})
Sourcepub fn with_after_start<C>(self, after_start: C) -> Self
pub fn with_after_start<C>(self, after_start: C) -> Self
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)
})
Sourcepub fn with_before_restart<C>(self, before_restart: C) -> Self
pub fn with_before_restart<C>(self, before_restart: C) -> Self
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)
})
Sourcepub fn with_after_restart<C>(self, after_restart: C) -> Self
pub fn with_after_restart<C>(self, after_restart: C) -> Self
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)
})
Sourcepub fn with_after_stop<C>(self, after_stop: C) -> Self
pub fn with_after_stop<C>(self, after_stop: C) -> Self
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)
})
Sourcepub fn has_before_start(&self) -> bool
pub fn has_before_start(&self) -> bool
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());
Sourcepub fn has_before_restart(&self) -> bool
pub fn has_before_restart(&self) -> bool
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());
Sourcepub fn has_after_restart(&self) -> bool
pub fn has_after_restart(&self) -> bool
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());
Sourcepub fn has_after_stop(&self) -> bool
pub fn has_after_stop(&self) -> bool
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 Freeze for Callbacks
impl !RefUnwindSafe for Callbacks
impl Send for Callbacks
impl Sync for Callbacks
impl Unpin for Callbacks
impl !UnwindSafe for Callbacks
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more