| Crates.io | foxtive-cron |
| lib.rs | foxtive-cron |
| version | 0.2.0 |
| created_at | 2025-06-22 15:13:07.462216+00 |
| updated_at | 2025-07-19 13:11:32.606487+00 |
| description | Foxtive Cron |
| homepage | |
| repository | https://github.com/foxtive/foxtive-cron |
| max_upload_size | |
| id | 1721695 |
| size | 37,851 |
Foxtive Cron is a lightweight, asynchronous, cron-based job scheduler for Rust powered by Tokio. It allows you to schedule and execute asynchronous tasks (including closures and blocking code) using standard cron expressions with precision down to the second.
Add the following to your Cargo.toml:
[dependencies]
foxtive-cron = "0.1"
tokio = { version = "1", features = ["full"] }
use foxtive_cron::Cron;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let mut cron = Cron::new();
// Run every 5 seconds
cron.add_job_fn("Ping", "*/5 * * * * * *", || async {
println!("Ping at {}", chrono::Utc::now());
Ok(())
})?;
tokio::spawn(async move {
cron.run().await;
});
// keep the main task alive
tokio::signal::ctrl_c().await?;
Ok(())
}
Custom Job with JobContract You can define your own job logic by implementing the JobContract trait:
use foxtive_cron::CronResult;
use foxtive_cron::contracts::JobContract;
use async_trait::async_trait;
use std::sync::Arc;
struct MyJob;
#[async_trait]
impl JobContract for MyJob {
fn name(&self) -> &str {
"MyCustomJob"
}
fn schedule(&self) -> &str {
"0 * * * * * *"
}
async fn run(&self) -> foxtive_cron::CronResult<()> {
println!("Running my custom job!");
Ok(())
}
}
#[tokio::main]
async fn main() -> CronResult<()> {
let mut cron = Cron::new();
// Register:
cron.add_job(Arc::new(MyJob))?;
tokio::spawn(async move {
cron.run().await;
});
// keep the main task alive
tokio::signal::ctrl_c().await?;
Ok(())
}
This library uses a 7-field cron format: sec min hour day month weekday year
Examples:
*/10 * * * * * * → every 10 seconds0 0 * * * * * → top of every hour5 0 12 * * * * → at 12:00:05 every dayJobs are executed in separate tasks and can be cloned using Arc. The internal scheduler uses BinaryHeap to prioritize the next job run time efficiently.
This library logs job execution status using the log crate.
Add persistence / job state recovery
Contributions, bug reports, and feature requests are welcome! Feel free to open issues or PRs.
This project is licensed under the MIT License.