โฐ schedules.rs ๐
โจ A lightweight Rust library for scheduling operations across multiple time scales โจ
๐ฅ Who This Is For
This library is perfect for developers who need:
- ๐ฏ Simple, predictable scheduling with absolute time intervals
- ๐งฉ Lightweight scheduling without complex calendar-based rules
- โ๏ธ Precise control over execution timing in systems programming
- ๐ง Efficient scheduling in resource-constrained environments
By design, we focus on absolute time durations (every 5 seconds, every 10 minutes) and deliberately avoid calendar-based scheduling (every Thursday, first day of month). This keeps the API clean, predictable, and easy to reason about for systems programming tasks.
๐ฏ Features
- โก Multiple tick frequencies (milliseconds to minutes)
- ๐ฐ๏ธ Configurable time sources for ultimate flexibility
- ๐ Compound durations (e.g., 10 minutes plus 30 seconds)
- ๐ Named schedules with intuitive fluent builder API
- ๐ Direct callback references and event emission
- ๐พ Easy serialization and state persistence
- ๐ Both synchronous AND asynchronous execution support
- ๐ Advanced scheduling patterns:
- โฑ๏ธ Jittered intervals (add randomness to prevent thundering herds)
- ๐ Exponential backoff with configurable rate and maximum
- ๐ Decay intervals that transition from quick to slow
- ๐ข Limited execution counts (run exactly N times)
- ๐๏ธ Full configuration options for:
- ๐งต Worker thread pool size
- ๐งฎ Schedule store pre-allocation
- ๐ฒ Deterministic operation with fixed RNG seeds
๐ Usage
// Create a scheduler with default settings
let mut scheduler = new;
// Or create with custom configuration
let config = SchedulerConfig ;
let mut scheduler = with_config;
// Register a callback handler (traditional way)
;
scheduler.register_callback?;
// Or register a callback using the helper function (much simpler!)
scheduler.register_callback?;
// Create a schedule with the registered handler
scheduler.every
.with_name
.with_callback_id
.build?;
// For simple cases, use the convenience method
scheduler.every
.with_name
.execute?;
// Create advanced schedule patterns
// Jittered schedule (adds randomness to prevent thundering herds)
scheduler.every
.with_name
.with_jitter
.with_callback_id
.build?;
// Exponential backoff (good for retries)
scheduler.every
.with_name
.exponential
.with_callback_id
.build?;
// Decaying schedule (transitions from quick to slow intervals)
scheduler.every
.with_name
.decay_to
.with_callback_id
.build?;
// Limited schedule (only executes a set number of times)
scheduler.every
.with_name
.max_executions
.with_callback_id
.build?;
// Start the scheduler
scheduler.start;
// You can manage schedules directly:
let all_schedules = scheduler.get_all_schedules;
scheduler.remove_schedule?;
scheduler.clear_schedules;
// Later, freeze the state
let state = scheduler.freeze?;
let serialized = to_string?;
// Restore (requires re-registering handlers)
let state: SchedulerState = from_str?;
let mut restored = restore?;
restored.register_callback?;
restored.start;
โก Async Support
For asynchronous operation with Tokio (requires the async feature):
// Enable the feature in Cargo.toml:
// [dependencies]
// schedules = { version = "0.3.0", features = ["async"] }
// Create an async scheduler with default settings
let mut scheduler = new;
// Or create with custom configuration
let config = SchedulerConfig ;
let mut scheduler = with_config;
// Register an async handler
scheduler.register_async_handler?;
// Create and start schedules as with the sync scheduler
scheduler.every
.with_name
.with_callback_id
.build?;
// Create a schedule with an inline async closure
scheduler
.every
.with_name
.execute?;
scheduler.start;
๐ฆ Installation
Add to your Cargo.toml:
[]
= "0.3.0"
# For async support:
= { = "0.3.0", = ["async"] }
๐ License
This project is licensed under the MIT License - see the LICENSE file for details.
๐ Contribute
Contributions are welcome! Feel free to open issues and submit PRs to make schedule.rs even better!