[go: up one dir, main page]

Supervisor

Struct Supervisor 

Source
pub struct Supervisor { /* private fields */ }
Expand description

A supervisor that can supervise both Children and other supervisors using a defined SupervisionStrategy (set with with_strategy or SupervisionStrategy::OneForOne by default).

When a supervised children group or supervisor faults, the supervisor will restart it and eventually some of its other supervised entities, depending on its supervision strategy.

Note that a supervisor, called the “system supervisor”, is created by the system at startup and is the supervisor supervising children groups created via Bastion::children.

§Example

let sp_ref: SupervisorRef = Bastion::supervisor(|sp| {
    // Configure the supervisor...
    sp.with_strategy(SupervisionStrategy::OneForOne)
    // ...and return it.
}).expect("Couldn't create the supervisor.");

Implementations§

Source§

impl Supervisor

Source

pub fn id(&self) -> &BastionId

Returns this supervisor’s identifier.

Note that the supervisor’s identifier is reset when it is restarted.

§Example
Bastion::supervisor(|sp| {
    let supervisor_id: &BastionId = sp.id();
    // ...
}).expect("Couldn't create the supervisor.");
Source

pub fn supervisor<S>(self, init: S) -> Self

Creates a new supervisor, passes it through the specified init closure and then starts supervising it.

If you don’t need to chain calls to this Supervisor’s methods and need to get a SupervisorRef referencing the newly created supervisor, use the supervisor_ref method instead.

§Arguments
  • init - The closure taking the new supervisor as an argument and returning it once configured.
§Example
parent.supervisor(|sp| {
    // Configure the supervisor...
    sp.with_strategy(SupervisionStrategy::OneForOne)
    // ...and return it.
})
Source

pub fn supervisor_ref<S>(&mut self, init: S) -> SupervisorRef

Creates a new Supervisor, passes it through the specified init closure and then starts supervising it.

If you need to chain calls to this Supervisor’s methods and don’t need to get a SupervisorRef referencing the newly created supervisor, use the supervisor method instead.

§Arguments
  • init - The closure taking the new Supervisor as an argument and returning it once configured.
§Example
let sp_ref: SupervisorRef = parent.supervisor_ref(|sp| {
    // Configure the supervisor...
    sp.with_strategy(SupervisionStrategy::OneForOne)
    // ...and return it.
});
Source

pub fn children<C>(self, init: C) -> Self
where C: FnOnce(Children) -> Children,

Creates a new Children, passes it through the specified init closure and then starts supervising it.

If you don’t need to chain calls to this Supervisor’s methods and need to get a ChildrenRef referencing the newly created supervisor, use the children_ref method instead.

§Arguments
  • init - The closure taking the new Children as an argument and returning it once configured.
§Example
sp.children(|children| {
    children.with_exec(|ctx: BastionContext| {
        async move {
            // Send and receive messages...
            let opt_msg: Option<SignedMessage> = ctx.try_recv().await;

            // ...and return `Ok(())` or `Err(())` when you are done...
            Ok(())
            // Note that if `Err(())` was returned, the supervisor would
            // restart the children group.
        }
    })
})
Source

pub fn children_ref<C>(&self, init: C) -> ChildrenRef
where C: FnOnce(Children) -> Children,

Creates a new Children, passes it through the specified init closure and then starts supervising it.

If you need to chain calls to this Supervisor’s methods and don’t need to get a ChildrenRef referencing the newly created supervisor, use the children method instead.

§Arguments
  • init - The closure taking the new Children as an argument and returning it once configured.
§Example
let children_ref: ChildrenRef = sp.children_ref(|children| {
    children.with_exec(|ctx: BastionContext| {
        async move {
            // Send and receive messages...
            let opt_msg: Option<SignedMessage> = ctx.try_recv().await;

            // ...and return `Ok(())` or `Err(())` when you are done...
            Ok(())
            // Note that if `Err(())` was returned, the supervisor would
            // restart the children group.
        }
    })
});
Source

pub fn with_strategy(self, strategy: SupervisionStrategy) -> Self

Sets the strategy the supervisor should use when one of its supervised children groups or supervisors dies (in the case of a children group, it could be because one of its elements panicked or returned an error).

The default strategy is SupervisionStrategy::OneForOne.

§Arguments
  • strategy - The strategy to use:
    • SupervisionStrategy::OneForOne would only restart the supervised children groups or supervisors that fault.
    • SupervisionStrategy::OneForAll would restart all the supervised children groups or supervisors (even those which were stopped) when one of them faults, respecting the order in which they were added.
    • SupervisionStrategy::RestForOne would restart the supervised children groups or supervisors that fault along with all the other supervised children groups or supervisors that were added after them (even the stopped ones), respecting the order in which they were added.
§Example
Bastion::supervisor(|sp| {
    // Note that "one-for-one" is the default strategy.
    sp.with_strategy(SupervisionStrategy::OneForOne)
}).expect("Couldn't create the supervisor");
Source

pub fn with_restart_strategy(self, restart_strategy: RestartStrategy) -> Self

Sets the actor restart strategy the supervisor should use of its supervised children groups or supervisors dies to restore in the correct state.

The default strategy is the ActorRestartStrategy::Immediate and unlimited amount of retries.

§Example
sp.with_restart_strategy(
    RestartStrategy::default()
        .with_restart_policy(RestartPolicy::Tries(5))
        .with_actor_restart_strategy(           
            ActorRestartStrategy::ExponentialBackOff {
                timeout: Duration::from_millis(5000),
                multiplier: 3.0,
            }
        )
)
}).expect("Couldn't create the supervisor");
Source

pub fn with_callbacks(self, callbacks: Callbacks) -> Self

Sets the callbacks that will get called at this supervisor’s different lifecycle events.

See Callbacks’s documentation for more information about the different callbacks available.

§Arguments
  • callbacks - The callbacks that will get called for this supervisor.
§Example
Bastion::supervisor(|sp| {
    let callbacks = Callbacks::new()
        .with_before_start(|| println!("Supervisor started."))
        .with_after_stop(|| println!("Supervisor stopped."));

    sp.with_callbacks(callbacks)
}).expect("Couldn't create the supervisor.");

Trait Implementations§

Source§

impl Debug for Supervisor

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> AsAny for T
where T: Any,

Source§

fn as_any(&mut self) -> &mut (dyn Any + 'static)

Downcast implemented type to Any.
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,