Skip to main content

Supervisor

Struct Supervisor 

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

Builder for constructing and starting a supervisor.

Supervisor provides a fluent API for registering tasks, setting up prerequisites, and configuring global runtime options like concurrency limits and persistence.

§Example

use foxtive_supervisor::{Supervisor, SupervisedTask};

struct MyTask;
#[async_trait::async_trait]
impl SupervisedTask for MyTask {
    fn id(&self) -> &'static str { "my-task" }
    async fn run(&self) -> foxtive_supervisor::SupervisorResult<()> { Ok(()) }
}

#[tokio::main]
async fn main() {
    Supervisor::new()
        .add(MyTask)
        .start_and_wait_any()
        .await
        .unwrap();
}

Implementations§

Source§

impl Supervisor

Source

pub fn new() -> Self

Create a new, empty supervisor.

Source

pub fn add<T: SupervisedTask + 'static>(self, task: T) -> Self

Add a task to be supervised.

The task must implement the SupervisedTask trait.

Source

pub fn add_many<T: SupervisedTask + 'static>(self, tasks: Vec<T>) -> Self

Add multiple tasks of the same type.

Source

pub fn add_boxed(self, task: Box<dyn SupervisedTask>) -> Self

Add a task already wrapped in a Box.

Useful when managing a heterogeneous collection of tasks.

Source

pub fn add_arc(self, task: Arc<dyn SupervisedTask>) -> Self

Add a task already wrapped in an Arc.

This is the most efficient way to add a task if you already have an Arc handle.

Source

pub fn with_global_concurrency_limit(self, limit: usize) -> Self

Set a global concurrency limit for the supervisor.

This limits how many tasks can be in their run() loop simultaneously. It’s useful for preventing resource exhaustion during massive task restarts.

Source

pub fn with_event_channel_capacity(self, capacity: usize) -> Self

Set the event broadcast channel capacity.

The default is 1024. Increase this if you have many tasks emitting events rapidly and observe lagged event warnings.

Source

pub fn add_listener(self, listener: Arc<dyn SupervisorEventListener>) -> Self

Register an event listener to observe lifecycle events.

Event listeners receive notifications for task starts, failures, restarts, etc.

Source

pub fn with_state_store(self, store: Arc<dyn TaskStateStore>) -> Self

Set a custom state store for persisting task states across restarts.

The supervisor will use this store to load previous attempt counts and failure metadata before starting tasks, and will update it as tasks run.

Source

pub fn require<F>(self, name: &'static str, fut: F) -> Self
where F: Future<Output = SupervisorResult<()>> + Send + 'static,

Require a named async gate to resolve before any supervised task starts.

Prerequisites run sequentially in the order they were registered. If any prerequisite fails, the supervisor aborts startup.

Source

pub fn require_fn<F, Fut>(self, name: &'static str, f: F) -> Self
where F: FnOnce() -> Fut + Send + 'static, Fut: Future<Output = SupervisorResult<()>> + Send + 'static,

Require a named async gate using a closure.

Source

pub fn runtime(self) -> TaskRuntime

Consume the builder and return the underlying TaskRuntime.

Note: This does not start the tasks. You must call start_all() on the returned runtime.

Source

pub async fn start(self) -> Result<TaskRuntime, SupervisorError>

Start all registered tasks and return the TaskRuntime handle.

This validates the dependency graph and executes prerequisites before spawning tasks.

§Errors

Returns SupervisorError if prerequisites fail or if the dependency graph is invalid.

Source

pub async fn start_and_wait_any( self, ) -> Result<SupervisionResult, SupervisorError>

Start all tasks and block until the first one terminates.

§Errors

Returns SupervisorError if startup validation or prerequisites fail.

Source

pub async fn start_and_wait_all( self, ) -> Result<Vec<SupervisionResult>, SupervisorError>

Start all tasks and block until all have terminated.

§Errors

Returns SupervisorError if startup validation or prerequisites fail.

Trait Implementations§

Source§

impl Default for Supervisor

Source§

fn default() -> Self

Create a new supervisor with default settings.

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> 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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