Skip to main content

Module service

Module service 

Source
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 use async_trait to 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 into run() so the service must listen for cancellation. This is superior to relying on Drop semantics, 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 via TaskTracker::spawn.

  • &self (not &mut self) on run(): The trait takes &self so that services can be wrapped in Arc, 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 the Send + Sync bounds anyway.

Enums§

RestartPolicy
Restart policy for a service managed by the supervisor.

Traits§

JanusService
The core trait every supervised service must implement.