Expand description
JanusService trait — the contract every supervised service must implement.
This trait is the cornerstone of the Janus Supervisor Model. Every service
managed by the JanusSupervisor must implement
this trait so the supervisor can:
- Start the service in a tracked task
- Monitor its lifecycle (detect unexpected exits)
- Propagate shutdown signals via
CancellationToken - Restart the service according to the configured backoff strategy
§Design Decisions
-
async_trait: We useasync_traitto allow dynamic dispatch (Box<dyn JanusService>). The one-time heap allocation for the boxed future is negligible for long-running service loops that start once. -
CancellationToken: Passed explicitly intorun()so the service must listen for cancellation. This is superior to relying onDropsemantics, which are unpredictable in async contexts. -
anyhow::Result: Allows services to return diverse error types that the supervisor can uniformly log and use to decide on restart strategies. -
Send + Sync + 'static: Required because services are spawned onto the Tokio runtime viaTaskTracker::spawn. -
&self(not&mut self) onrun(): The trait takes&selfso that services can be wrapped inArc, shared across boundaries, or composed without requiring exclusive access. Services that need mutable state across restarts should use interior mutability (AtomicU64,Mutex, etc.), which is already required by theSend + Syncbounds anyway.
Enums§
- Restart
Policy - Restart policy for a service managed by the supervisor.
Traits§
- Janus
Service - The core trait every supervised service must implement.