logo

Struct bastion::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.");

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)
})

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)
})

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)
})

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

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Returns the “default value” for a type. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Downcast implemented type to Any. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

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

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

Performs the conversion.

The alignment of pointer.

The type for initializers.

Initializes a with the given initializer. Read more

Dereferences the given pointer. Read more

Mutably dereferences the given pointer. Read more

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

Should always be Self

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

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