supervised
supervised is a small tokio service supervisor for applications that run
long-lived async tasks and want restart, shutdown, cancellation, and startup
readiness to be modeled explicitly.
The crate is intentionally compact:
SupervisedServiceis the single service trait.service_fnwraps one-off async functions as services.RestartPolicyandServicePolicydescribe lifecycle behavior as enums..until_cancelled()and.when_ready()are fluent service adapters..shutdown_on_ctrl_c()adds an immediately-ready Ctrl+C shutdown listener.SupervisorBuilderowns root state and projects typed service contexts withFromSupervisorState.
Example
use ;
async
service_fn accepts natural async return shapes and converts them into a
ServiceOutcome: () means completed, Result<(), E> means completed or
failed, and Result<ServiceOutcome, E> lets a service keep returning explicit
outcomes when needed.
Use .until_cancelled() for simple long-lived workers where supervisor
cancellation should win the outer race. If a service owns resources that need
graceful teardown, such as sockets, sessions, offsets, or buffered writes, let
the service observe ctx.token().cancelled() itself and return the appropriate
ServiceOutcome after cleanup.
External cancellation
Brought your own CancellationToken? No problem. Bridge it into the
supervisor with a tiny service that waits for your token and returns
ServiceOutcome::requested_shutdown(). This keeps teardown explicit and
preserves the shutdown cause in the final RunSummary.
use ;
use CancellationToken;
async
Readiness
Services are ready immediately by default. Use .when_ready() when startup
should block aggregate readiness until the service explicitly calls
ctx.readiness().mark_ready() or completes successfully.
use ;
async
License
Licensed under either of Apache License, Version 2.0 or MIT license at your option.