A lightweight, thread-safe cron job scheduler for Rust with support for both sync and async execution.
- β‘ Sync & Async - Choose the execution mode that fits your application
- π Timezone Aware - Full timezone support via chrono
- π― Second Precision - 7-field cron expressions with second-level scheduling
- π§ Runtime Control - Add, remove, start, and stop jobs dynamically
- π‘οΈ Thread Safe - Built with Rust's safety guarantees
Add to your Cargo.toml:
[dependencies]
cron_tab = { version = "0.2", features = ["sync", "async"] }use chrono::Utc;
use cron_tab::Cron;
fn main() {
let mut cron = Cron::new(Utc);
// Add a job that runs every second
cron.add_fn("* * * * * * *", || {
println!("Running every second!");
}).unwrap();
cron.start();
std::thread::sleep(std::time::Duration::from_secs(10));
cron.stop();
}use chrono::Utc;
use cron_tab::AsyncCron;
#[tokio::main]
async fn main() {
let mut cron = AsyncCron::new(Utc);
cron.add_fn("* * * * * *", || async {
println!("Running every minute!");
tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;
}).await.unwrap();
cron.start().await;
tokio::time::sleep(tokio::time::Duration::from_secs(60)).await;
cron.stop().await;
}CronTab uses 7-field cron expressions with second precision:
sec min hour day month weekday year
* * * * * * *
Field ranges:
sec: 0-59min: 0-59hour: 0-23day: 1-31month: 1-12weekday: 0-6 (Sunday=0)year: 1970-3000
Common patterns:
| Expression | Description |
|---|---|
* * * * * * * |
Every second |
0 * * * * * * |
Every minute |
0 0 * * * * * |
Every hour |
0 0 0 * * * * |
Daily at midnight |
0 0 9 * * MON-FRI * |
Weekdays at 9 AM |
0 30 14 * * * * |
Daily at 2:30 PM |
use chrono::Utc;
use cron_tab::Cron;
fn main() {
let mut cron = Cron::new(Utc);
cron.start();
// Add a job and get its ID
let job_id = cron.add_fn("*/5 * * * * * *", || {
println!("Running every 5 seconds");
}).unwrap();
std::thread::sleep(std::time::Duration::from_secs(15));
// Remove the job dynamically
cron.remove(job_id);
cron.stop();
}use std::sync::Arc;
use chrono::Utc;
use cron_tab::AsyncCron;
use tokio::sync::Mutex;
#[tokio::main]
async fn main() {
let mut cron = AsyncCron::new(Utc);
let counter = Arc::new(Mutex::new(0));
let counter_clone = counter.clone();
cron.add_fn("* * * * * * *", move || {
let counter = counter_clone.clone();
async move {
let mut count = counter.lock().await;
*count += 1;
println!("Count: {}", *count);
}
}).await.unwrap();
cron.start().await;
tokio::time::sleep(tokio::time::Duration::from_secs(5)).await;
cron.stop().await;
}use chrono::FixedOffset;
use cron_tab::Cron;
fn main() {
// UTC+9 (Tokyo)
let tokyo_tz = FixedOffset::east_opt(9 * 3600).unwrap();
let mut cron = Cron::new(tokyo_tz);
cron.add_fn("0 0 9 * * * *", || {
println!("Good morning from Tokyo!");
}).unwrap();
cron.start();
// Jobs run according to the specified timezone
}Choose the features you need:
# Sync only (default)
cron_tab = "0.2"
# Async only
cron_tab = { version = "0.2", features = ["async"] }
# Both sync and async
cron_tab = { version = "0.2", features = ["all"] }Run the included examples:
# Synchronous example
cargo run --example simple --features sync
# Asynchronous example
cargo run --example async_simple --features async# All tests
cargo test --all-features
# Sync tests only
cargo test --features sync
# Async tests only
cargo test --features asyncContributions are welcome! Please:
- Fork the repository
- Create a feature branch
- Make your changes with tests
- Run
cargo fmtandcargo clippy --all-features - Submit a pull request
For major changes, open an issue first to discuss your proposal.
MIT License - see LICENSE for details.
- Documentation - Full API reference
- Crates.io - Package registry
- Repository - Source code