Skip to main content

Supervisor

Struct Supervisor 

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

Supervises the Runledger runtime loops spawned for a worker process.

A supervisor owns the worker, scheduler, and reaper task handles selected by SupervisorBuilder. Use Self::run_until_shutdown for a typical worker process that should exit on either an external shutdown signal or an internal runtime task failure.

Dropping a supervisor requests shutdown and detaches the task handles. Call Self::shutdown or Self::join when the owning process needs to observe panics or unexpected task exits.

Implementations§

Source§

impl Supervisor

Source

pub fn builder( pool: &DbPool, config: JobsConfig, ) -> Result<SupervisorBuilder<'_>, RuntimeError>

Returns a builder for a supervisor over a shared pool and runtime configuration.

This validates that the caller is inside the Tokio runtime that will own spawned supervisor tasks.

Source

pub fn shutdown_handle(&self) -> SupervisorShutdown

Returns a cloneable shutdown handle that can request shutdown without owning the supervisor task joins.

Source

pub fn request_shutdown(&self)

Requests graceful shutdown of all supervised loops.

Source

pub fn is_shutdown_requested(&self) -> bool

Returns whether shutdown has been requested through this supervisor or a clone of its shutdown handle.

Source

pub async fn join(self) -> Result<()>

Waits for all supervised loops to exit.

With the default long-running loops, this method waits until shutdown is requested through a SupervisorShutdown handle or until a task exits. If a loop exits before shutdown was requested, the remaining loops are asked to shut down and the first observed error is returned. Additional task failures observed while draining are logged. This method does not impose a deadline; use Self::shutdown_with_timeout when the caller owns shutdown and needs a bounded wait.

Source

pub async fn shutdown(self) -> Result<()>

Requests graceful shutdown and waits for all supervised loops to exit.

If a loop exits before shutdown was requested, the remaining loops are asked to shut down and the pre-existing task exit is reported, even when that exit is only observed after shutdown begins. This method does not impose a deadline. Use Self::shutdown_with_timeout when the owning process needs a shutdown budget; externally timing out this consuming future can detach still-running task handles.

Source

pub async fn run_until_shutdown<F>( self, shutdown: F, timeout: Duration, ) -> Result<()>
where F: Future<Output = ()>,

Waits until shutdown resolves or a supervised task fails, then exits.

If shutdown resolves first, graceful shutdown is requested and the supervisor waits up to timeout for all loops to exit. If a loop panics or exits unexpectedly before shutdown resolves, shutdown is requested for the remaining loops and the original task error is returned after those loops drain or a timeout is reported. If shutdown is requested through a SupervisorShutdown handle and every loop exits cleanly before shutdown resolves, this returns successfully.

This is the preferred method for worker binaries because it observes internal task failures during normal operation while still applying a bounded shutdown budget to cooperative process termination.

If timeout is too large to represent as a runtime deadline, this returns RuntimeError::ShutdownTimeoutTooLarge immediately. A zero timeout requests shutdown, aborts tasks without waiting for cooperative exits, and reports RuntimeError::ShutdownTimeout.

If the initial timeout validation fails before shutdown resolves, the supervisor is still dropped, so shutdown is requested, but task handles are not aborted or drained. If a deadline overflow is detected after shutdown begins, remaining tasks are aborted and drained before returning.

Source

pub async fn shutdown_with_timeout(self, timeout: Duration) -> Result<()>

Requests graceful shutdown and waits up to timeout for all supervised loops to exit.

If a loop had already exited before this method begins shutdown, that failure is returned after the remaining loops have had the same shutdown budget to exit cooperatively. If the timeout expires, remaining tasks are aborted and drained with a bounded cleanup attempt before a timeout error is returned. Abort cleanup can make total wall-clock time exceed timeout by up to one second, or timeout, whichever is smaller. A zero timeout requests shutdown, immediately aborts tasks that did not already finish, and reports RuntimeError::ShutdownTimeout.

If timeout is too large to represent as a runtime deadline, this returns RuntimeError::ShutdownTimeoutTooLarge immediately. The supervisor is still dropped, so shutdown is requested, but task handles are not aborted or drained.

Trait Implementations§

Source§

impl Drop for Supervisor

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. 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> 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> 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