pub struct ModuleAdapter { /* private fields */ }Expand description
Adapts an existing start_module(Arc<JanusState>) -> Result<()> function
into a JanusService that the supervisor can manage.
§Shutdown Bridging
When the supervisor cancels this service’s CancellationToken, the
adapter calls state.request_shutdown() to signal the inner module
through its existing shutdown pathway. This avoids having to rewrite
every service to accept a CancellationToken directly — they continue
polling state.is_shutdown_requested() as before.
§Shared-State Shutdown Invariant
Because JanusState is shared across all modules, request_shutdown()
sets a global AtomicBool. This is safe under the current
supervisor model because:
- The supervisor propagates cancellation to all services at once (child tokens of the same root), so if one bridge fires they all do.
- When a service fails on its own (returns
Err), the bridge task is aborted before it can callrequest_shutdown(), keeping the flagfalsefor the restart. - The supervisor checks
cancel.is_cancelled()before each restart attempt, so a staletrueflag never leads to a restart loop.
If per-module independent restart semantics are ever needed (e.g.,
restarting one module while others keep running), this must be
refactored to use a per-adapter shutdown signal instead of the global
JanusState flag.
§Error Propagation
Errors returned by the inner start_module are converted to
anyhow::Error and bubbled up to the supervisor, which then applies
the configured RestartPolicy.
Implementations§
Source§impl ModuleAdapter
impl ModuleAdapter
Sourcepub fn new<F>(
name: &str,
state: Arc<JanusState>,
start_fn: F,
policy: RestartPolicy,
) -> Self
pub fn new<F>( name: &str, state: Arc<JanusState>, start_fn: F, policy: RestartPolicy, ) -> Self
Create a new adapter.
§Arguments
name— Service name for the supervisor (e.g.,"forward","data","cns").state— SharedJanusStatethat the module uses for configuration, health reporting, and shutdown signalling.start_fn— A closure or function pointer matching thestart_modulesignature. Must beSend + Sync + 'static.policy— How the supervisor should handle failures.
§Example
let adapter = ModuleAdapter::new(
"forward",
state.clone(),
|s| Box::pin(janus_forward::start_module(s)),
RestartPolicy::OnFailure,
);Sourcepub fn on_failure<F>(name: &str, state: Arc<JanusState>, start_fn: F) -> Self
pub fn on_failure<F>(name: &str, state: Arc<JanusState>, start_fn: F) -> Self
Create an adapter with the default RestartPolicy::OnFailure.
Sourcepub fn one_shot<F>(name: &str, state: Arc<JanusState>, start_fn: F) -> Self
pub fn one_shot<F>(name: &str, state: Arc<JanusState>, start_fn: F) -> Self
Create an adapter that never restarts (one-shot module).