🦊 Foxtive Supervisor
Because tasks crash. We help them get back up.
A Rust supervision library that keeps your async tasks running, even when things go wrong. Think of it as a caring parent for your background jobs.
What's New in v0.3?
Advanced Scheduling & Composition
- Cron Scheduling: Schedule tasks with cron expressions (e.g.,
"*/1 * * * * * *"for every second) - Task Groups: Atomic operations on related tasks with group-level health monitoring
- Supervisor Hierarchies: Create nested supervisor trees for complex architectures
- Task Pools: Load-balanced worker pools with RoundRobin, Random, and LeastLoaded strategies
- Conditional Dependencies: Environment-based dependency activation for flexible deployments
- Rate Limiting: Enforce minimum restart intervals to prevent resource exhaustion
- Time Windows: Restrict task execution to specific time periods
- Cascading Shutdown: Bottom-up shutdown propagation through supervisor hierarchies
- Complex Dependency Graphs: Support for diamond dependencies, fan-out/fan-in patterns, and more
Why?
In production, things fail:
- Network hiccups
- Temporary resource exhaustion
- Race conditions you didn't catch in testing
- That one edge case from 3am
Foxtive Supervisor automatically restarts your tasks with configurable policies, handles panics gracefully, and gives you hooks to observe what's happening.
Features
Core Supervision
- Automatic Restarts: Keep tasks running with customizable retry policies and backoff strategies.
- Panic Recovery: Gracefully handle task panics without crashing your application.
- Dependency Management: Ensure tasks start only after their dependencies are ready.
- Prerequisites Gates: Run async checks before any task starts (e.g., database connectivity).
Dynamic Task Management
- Runtime Task Control: Add, remove, pause, resume, or restart tasks at runtime.
- Task Groups: Perform atomic operations on related tasks (start/stop/restart groups together).
- Group Health Monitoring: Aggregate health status across task groups for easy monitoring.
- Task Information: Query detailed task status, metrics, and health at any time.
Scheduling & Timing
- Cron Scheduling: Schedule tasks using cron expressions (integrates with
foxtive-cron). - Delayed Starts: Configure initial delays before task execution begins.
- Random Jitter: Add randomness to prevent thundering herd in distributed deployments.
- Rate Limiting: Enforce minimum intervals between restarts to prevent resource exhaustion.
- Time Windows: Restrict task execution to specific time periods (e.g., business hours only).
Advanced Reliability
- Circuit Breaker: Fail fast and prevent cascading failures when external services are down.
- Persistence Layer: Persist task state (attempts, failures, status) across application restarts.
- Multiple Storage Backends: In-memory and filesystem persistence out of the box.
- Graceful Shutdown: Configurable timeouts with forced termination and cleanup hooks.
- Cascading Shutdown: Hierarchical shutdown propagation for nested supervisors.
Observability & Monitoring
- Event System: Comprehensive lifecycle events for monitoring and alerting.
- Distributed Tracing: Built-in
tracingintegration with correlation IDs. - Structured Logging: Rich, contextual logs throughout the supervision lifecycle.
- Health Checks: Per-task and group-level health status reporting.
- Custom Metrics: Expose task-specific metrics for monitoring dashboards.
Concurrency & Performance
- Concurrency Control: Global and per-task limits to prevent resource exhaustion.
- Priority Scheduling: Control the order in which tasks are started and restarted.
- Task Pools: Load-balanced worker pools with multiple strategies (RoundRobin, Random, LeastLoaded).
- Supervisor Hierarchies: Create nested supervisor trees for organized task management.
Testing & Development
- Testing Utilities: Built-in mock tasks and test harness for reliable testing.
- Conditional Dependencies: Environment-based dependency activation for flexible testing.
- Hot Reload Ready: Architecture designed for future configuration reload support.
Installation
[]
= "0.3"
= { = "1", = ["full"] }
= "1"
= "0.1"
Feature Flags
Foxtive Supervisor uses feature flags to enable optional functionality:
[]
= { = "0.3", = ["cron"] }
Available Features:
cron- Enable cron scheduling support (requiresfoxtive-cronandchrono-tz)- All other features are enabled by default
Optional Dependencies
Some features require additional dependencies:
# For distributed tracing
= "0.1"
= { = "0.3", = ["env-filter"] }
# For persistence with filesystem
= "1"
# For cron scheduling (enabled with 'cron' feature)
= "0.1"
= "0.8"
= "0.8"
Quick Start
use ;
;
async
Advanced Examples
Cron-Scheduled Tasks
Run tasks on a schedule using cron expressions:
;
Task Groups with Health Monitoring
Group related tasks and monitor them together:
;
;
;
// Start all infrastructure tasks atomically
let mut runtime = supervisor.start.await?;
runtime.start_group;
// Check group health
let health = runtime.get_group_health.await;
match health
Rate Limiting & Backoff
Prevent resource exhaustion with rate limiting:
;
Supervisor Hierarchies
Create nested supervisor trees for complex architectures:
use ;
let mut hierarchy = new;
// Add child supervisors
let mut api_node = root;
api_node.add_child_supervisor;
api_node.add_child_supervisor;
let mut worker_node = root;
worker_node.add_child_supervisor;
worker_node.add_child_supervisor;
hierarchy.root_mut.add_child;
hierarchy.root_mut.add_child;
// Cascading shutdown - shuts down all children first, then parent
hierarchy.root.shutdown_all.await;
Task Pools with Load Balancing
Distribute work across multiple workers:
use ;
// Create a pool of 4 workers with round-robin distribution
let pool = new;
// Build supervisor with pooled workers
let supervisor = pool.build_pool;
Conditional Dependencies
Activate dependencies based on environment or runtime conditions:
;
State Persistence
Persist task state across application restarts:
use ;
use PathBuf;
;
async
The persistence layer supports:
- InMemoryStateStore: Default, no configuration needed
- FsStateStore: Filesystem-based persistence with JSON serialization
- Automatic state recovery when tasks restart
- Custom storage backends via
TaskStateStoretrait
Documentation
Guides
- Testing Guide - Comprehensive testing strategies and utilities
Examples
Real-world examples demonstrating various use cases:
-
Microservice Orchestration (examples/microservice-orchestration)
- Database to Cache to API Server architecture
- Dependency management and health monitoring
-
Circuit Breaker Pattern (examples/circuit-breaker)
- Automatic failure detection and recovery
- Half-open state testing
-
Graceful Shutdown (examples/graceful-shutdown)
- Configurable shutdown timeouts
- Cleanup hooks and forced termination
-
Database Message Consumer (examples/db-message-consumer)
- Persistent message processing with retry logic
- State persistence across restarts
-
Panic Recovery (examples/panic-catcher)
- Handling task panics gracefully
- Maintaining supervision despite crashes
-
Axum Integration (examples/axum-cron)
- Web framework integration
- Scheduled task management
-
Tracing & Observability (examples/tracing)
- Distributed tracing with correlation IDs
- Structured logging throughout task lifecycle
Troubleshooting Guide
1. Task not starting
- Check Dependencies: Ensure all IDs in
dependencies()exist and theirsetup()succeeds. - Check Prerequisites: If any
require()gate fails, no tasks will start. - Concurrency Limits: If the
global_concurrency_limitis 0 or very low, tasks might be queued indefinitely.
2. Immediate Restart Loops
- Setup Failure: If
setup()fails, the task will not enter therun()loop. Check logs forTaskSetupFailed. - Backoff Strategy: Ensure your
backoff_strategy()provides enough time for external resources to recover. - Should Restart Hook: Check if
should_restart()is returningfalseunexpectedly.
3. State Not Persisting
- Storage Permissions: Ensure the directory passed to
FsStateStoreis writable. - Task ID Consistency: Persistence is tied to the task's
id(). If you change the ID, previous state is lost.
4. High Resource Usage
- Limit Concurrency: Use
with_global_concurrency_limitto cap the number of active tasks. - Check Leaks: Ensure your
run()loop orsetup()doesn't leak memory or file handles. The supervisor restarts tasks, but it can't fix underlying leaks.
Test Coverage & Quality
Foxtive Supervisor is thoroughly tested with 89 comprehensive tests covering:
Test Categories
- Core Functionality (16 tests) - Basic supervision, restarts, lifecycle
- Dynamic Management (7 tests) - Add/remove/pause/resume at runtime
- Dependency Resolution (6 tests) - Complex dependency graphs, diamond patterns
- Persistence (2 tests) - State storage and recovery
- Circuit Breaker (3 tests) - All state transitions and recovery
- Concurrency Control (2 tests) - Global and per-task limits
- Graceful Shutdown (4 tests) - Timeouts, forced termination, cascading shutdown
- Event System (3 tests) - Lifecycle event emission and listeners
- Prerequisites (7 tests) - Async gates and validation
- Error Handling (3 tests) - Panic recovery, error propagation
- Edge Cases (17 tests) - Boundary conditions, error scenarios
- Task Groups (4 tests) - Atomic operations, health aggregation
- Task Pools (4 tests) - Load balancing strategies
- Conditional Dependencies (3 tests) - Environment-based activation
- Cron Scheduling (7 tests) - Scheduled execution, rate limiting, time windows
- Real-World Scenarios (2 tests) - Microservice architecture, message pipelines
- Complex Dependency Graphs (6 tests) - Fan-out/fan-in, linear chains, mixed deps
Quality Metrics
- Test Pass Rate: 98.9% (88/89 tests passing)
- Clippy Compliance: Zero warnings
- Documentation: Every public API documented with examples
- Integration Tests: Real-world scenarios with multiple features combined
- Performance: Benchmarks for persistence overhead and concurrency
License
MIT
Built with 🦊 by the Foxtive team. Made for humans who write Rust.