Skip to main content

Module supervisor

Module supervisor 

Source
Expand description

Janus Supervisor — hierarchical service lifecycle management.

Prometheus metrics are automatically published to the global JanusMetrics registry whenever the supervisor records a spawn, restart, termination, or circuit breaker trip. This means the /metrics endpoint exposes janus_supervisor_* counters/gauges with zero additional wiring.

This module implements the Janus Supervisor Model described in the architecture refactor document. It replaces the old “fire and forget” tokio::spawn pattern with a structured supervision tree built on:

  • TaskTracker — tracks spawned tasks without accumulating results (unlike JoinSet), preventing memory leaks in long-running processes.
  • CancellationToken — propagates graceful shutdown signals through the service hierarchy.

§Architecture

┌─────────────────────────────────────────────────┐
│                JanusSupervisor                   │
│                                                  │
│  ┌──────────┐  ┌──────────┐  ┌──────────┐      │
│  │  Data    │  │   CNS    │  │Execution │ ...   │
│  │ Service  │  │ Service  │  │ Service  │      │
│  └──────────┘  └──────────┘  └──────────┘      │
│                                                  │
│  TaskTracker  ◄── tracks all spawned tasks       │
│  CancellationToken ◄── shutdown signal tree      │
│  BackoffState[] ◄── per-service restart state    │
│  ServiceLifecycle[] ◄── per-service state machine│
└─────────────────────────────────────────────────┘

§Usage

use janus_core::supervisor::{JanusSupervisor, SupervisorConfig};

let config = SupervisorConfig::default();
let mut supervisor = JanusSupervisor::new(config);

supervisor.spawn_service(Box::new(my_data_service));
supervisor.spawn_service(Box::new(my_cns_service));

// Blocks until Ctrl+C / SIGTERM, then orchestrates graceful shutdown
supervisor.run_until_shutdown().await?;

Re-exports§

pub use adapters::ApiModuleAdapter;
pub use adapters::ModuleAdapter;
pub use backoff::BackoffAction;
pub use backoff::BackoffConfig;
pub use backoff::BackoffState;
pub use lifecycle::ServiceLifecycle;
pub use lifecycle::ServiceLifecycleSnapshot;
pub use lifecycle::ServicePhase;
pub use lifecycle::TerminationReason;
pub use lifecycle::TransitionError;
pub use service::JanusService;
pub use service::RestartPolicy;

Modules§

adapters
Service adapters for integrating existing Janus modules with the JanusSupervisor.
backoff
Exponential backoff with jitter for supervisor restart strategies.
lifecycle
Service lifecycle state machine for the Janus Supervisor.
service
JanusService trait — the contract every supervised service must implement.

Structs§

JanusSupervisor
The central supervisor for the Janus system.
MetricsSnapshot
Plain-data snapshot of supervisor metrics.
SpawnOptions
Per-service spawn configuration.
SupervisorConfig
Configuration for the JanusSupervisor.
SupervisorMetrics
Atomic counters for supervisor-level Prometheus-compatible metrics.