Skip to main content

Crate rustrade_supervisor

Crate rustrade_supervisor 

Source
Expand description

§rustrade-supervisor

Structured service lifecycle management for async trading bots.

Every long-running task in a rustrade bot — market feeds, candle pollers, heartbeats, brains — implements TradingService and is spawned through a Supervisor that:

  • Tracks running tasks without accumulating their results (uses TaskTracker rather than JoinSet).
  • Propagates graceful shutdown via a root CancellationToken that branches to each service.
  • Restarts failed services with exponential backoff plus full jitter and per-service circuit breakers.
  • Surfaces lifecycle state (Starting / Running / BackingOff / Stopping / Terminated) and metrics (restarts, active services, uptime) for observability.

§Quickstart

use rustrade_supervisor::{Supervisor, SupervisorConfig, TradingService};

let supervisor = Supervisor::new(SupervisorConfig::default());
supervisor.spawn_service(Box::new(my_market_feed));
supervisor.spawn_service(Box::new(my_risk_engine));

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

§Observability

Atomic counters in SupervisorMetrics are the in-process source of truth and are always available. Enable the prometheus feature to mirror them into a crate-local prometheus::Registry accessible via the prometheus::registry accessor in this crate’s prometheus submodule. The host service serves .gather() from that registry in its /metrics handler — rustrade does not own an HTTP layer of its own.

Re-exports§

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::RestartPolicy;
pub use service::TradingService;
pub use supervisor::MetricsSnapshot;
pub use supervisor::SpawnOptions;
pub use supervisor::Supervisor;
pub use supervisor::SupervisorConfig;
pub use supervisor::SupervisorMetrics;

Modules§

backoff
Exponential backoff with jitter for supervisor restart strategies.
lifecycle
Service lifecycle state machine.
service
The TradingService trait — every supervised unit must implement this.
supervisor
The Supervisor — manages a fleet of TradingServices.