pub struct ProducerLifecycle { /* private fields */ }Expand description
Manages background-task lifecycle for a set of SourceHandles.
The lifecycle:
- Construct with
ProducerLifecycle::new(orwith_parent_signalto inherit a shared cancellation tree). - For each registered source, call
spawn_from_source— the source’s own loop observes the lifecycle’sSignalcooperatively and returns once it has drained. - Hand callers a clone of
cancel_signalif they need to coordinate their own shutdown observation. - On shutdown, call
shutdownwith a graceDuration. The signal is fired; tasks that finish within the grace count asdrained; tasks that don’t are aborted and count asaborted.
Implementations§
Source§impl ProducerLifecycle
impl ProducerLifecycle
Sourcepub fn with_parent_signal(parent: &Signal) -> Self
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.
Sourcepub fn cancel_signal(&self) -> Signal
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.
Sourcepub fn spawn_from_source(&mut self, name: &str, source: &SourceHandle)
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.
Sourcepub fn task_count(&self) -> usize
pub fn task_count(&self) -> usize
Number of tasks currently being managed.
Sourcepub fn spawned_task_names(&self) -> impl Iterator<Item = &str>
pub fn spawned_task_names(&self) -> impl Iterator<Item = &str>
Names of all spawned tasks (in spawn order), per spawn_from_source.
Sourcepub async fn shutdown(self, grace: Duration) -> ShutdownReport
pub async fn shutdown(self, grace: Duration) -> ShutdownReport
Initiate graceful shutdown.
- Cancel the lifecycle’s token so cooperating tasks observe cancellation.
- Drain the join set with the given
gracedeadline. Tasks that exit within the grace count asdrained; tasks that don’t are aborted and count asaborted. Tasks that panicked are noted inShutdownReport::panicsand count asaborted(the task did not drain cleanly).
Consumes self, so the lifecycle cannot be re-used after shutdown.