Skip to main content

ProducerLifecycle

Struct ProducerLifecycle 

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

Manages background-task lifecycle for a set of SourceHandles.

The lifecycle:

  1. Construct with ProducerLifecycle::new (or with_parent_signal to inherit a shared cancellation tree).
  2. For each registered source, call spawn_from_source — the source’s own loop observes the lifecycle’s Signal cooperatively and returns once it has drained.
  3. Hand callers a clone of cancel_signal if they need to coordinate their own shutdown observation.
  4. On shutdown, call shutdown with a grace Duration. The signal is fired; tasks that finish within the grace count as drained; tasks that don’t are aborted and count as aborted.

Implementations§

Source§

impl ProducerLifecycle

Source

pub fn new() -> Self

Construct a new lifecycle with a fresh root cancellation token.

Source

pub fn with_parent_signal(parent: &Signal) -> Self

Construct from a parent token; cancelling the parent cancels this lifecycle’s child token too. Useful when a higher-level shutdown (e.g. the app-level oneshot signal) owns the root token.

Source

pub fn cancel_signal(&self) -> Signal

Return a clone of the cancellation token. Callers that need to observe shutdown alongside the spawned tasks (e.g. the listener serve loop) can race against this.

Source

pub fn spawn_from_source(&mut self, name: &str, source: &SourceHandle)

Spawn source under this lifecycle’s shared cancellation Signal.

Unlike a wrapper-cancelled task, the source’s own loop receives the lifecycle’s real Signal as its call input and observes it cooperatively (see run_interval_loop’s select_biased! against cancel.fired()) — cancellation is not drop-based, the source returns on its own once it has drained.

Source

pub fn task_count(&self) -> usize

Number of tasks currently being managed.

Source

pub fn spawned_task_names(&self) -> impl Iterator<Item = &str>

Names of all spawned tasks (in spawn order), per spawn_from_source.

Source

pub async fn shutdown(self, grace: Duration) -> ShutdownReport

Initiate graceful shutdown.

  1. Cancel the lifecycle’s token so cooperating tasks observe cancellation.
  2. Drain the join set with the given grace deadline. Tasks that exit within the grace count as drained; tasks that don’t are aborted and count as aborted. Tasks that panicked are noted in ShutdownReport::panics and count as aborted (the task did not drain cleanly).

Consumes self, so the lifecycle cannot be re-used after shutdown.

Trait Implementations§

Source§

impl Debug for ProducerLifecycle

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for ProducerLifecycle

Source§

fn default() -> Self

Returns the “default value” for a type. 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, 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