| Crates.io | siafu |
| lib.rs | siafu |
| version | 0.0.4 |
| created_at | 2025-05-02 18:27:53.767065+00 |
| updated_at | 2025-05-04 22:41:41.061589+00 |
| description | Ergonomic job scheduling library for Rust |
| homepage | |
| repository | https://github.com/vkhinvasara/siafu |
| max_upload_size | |
| id | 1658069 |
| size | 1,872,191 |
A flexible and ergonomic job scheduling library for Rust applications with an intuitive, fluent public API.
The name "Siafu" refers to safari ants (also known as driver ants or army ants), which are famous for their highly organized and efficient colony operations. These fascinating insects:
Just like these remarkable ants, the Siafu library excels at organizing, scheduling, and executing tasks in a coordinated manner. The library embodies the efficiency, reliability, and adaptability of safari ants' work patterns, making it the perfect metaphor for a job scheduling system.
use siafu::{JobBuilder, Scheduler, ScheduleTime, SchedulerError};
use std::time::{SystemTime, Duration};
use siafu::scheduler::types::RecurringInterval;
fn main() -> Result<(), SchedulerError> {
let mut scheduler = Scheduler::new();
// One-time job: run once after 5 seconds
let job = JobBuilder::new("one-time-job")
.once(ScheduleTime::Delay(Duration::from_secs(5)))
.add_handler(|| {
println!("One-time job executed!");
})
.build();
scheduler.add_job(job)?;
// Recurring job: every 3 seconds, up to 5 times
let job = JobBuilder::new("recurring-job")
.recurring(RecurringInterval::Secondly(3), Some(ScheduleTime::Delay(Duration::from_secs(3))))
.max_repeat(5)
.add_handler(|| {
println!("Recurring job executed!");
})
.build();
scheduler.add_job(job)?;
// Cron job: 9 AM on weekdays
let job = JobBuilder::new("cron-job")
.cron("0 0 9 * * 1-5 *")
.add_handler(|| {
println!("Cron job executed!");
})
.build();
scheduler.add_job(job)?;
// Random-time job: random time between 5-15 seconds from now
let start = SystemTime::now() + Duration::from_secs(5);
let end = SystemTime::now() + Duration::from_secs(15);
let job = JobBuilder::new("random-job")
.random(ScheduleTime::At(start), ScheduleTime::At(end))
.add_handler(|| {
println!("Random-time job executed!");
})
.build();
scheduler.add_job(job)?;
// Run pending jobs in a non blocking way to avoid busy_waiting
scheduler.run_non_blocking()?;
}
ScheduleTime is a core utility enum in Siafu that represents when a job should run. It has two variants:
Delay(Duration): run after a relative delay from nowAt(SystemTime): run at an absolute system timeIt implements std::str::FromStr, accepting human-friendly strings prefixed with delay: or at:. Under the hood it uses the humantime crate to parse durations and RFC3339 timestamps, returning a ScheduleTimeError on invalid input.
use std::str::FromStr;
let time = ScheduleTime::from_str("delay:10s")?; // parses "10s" as a Duration
let time = ScheduleTime::from_str("at:2025-05-04T10:00:00Z")?; // parses RFC3339 timestamp
let job = JobBuilder::new("once-job")
.once(time)
.add_handler(|| { })
.build();
use siafu::scheduler::types::RecurringInterval;
let job = JobBuilder::new("recurring")
.recurring(RecurringInterval::Hourly(1), None)
.add_handler(|| { })
.build();
let job = JobBuilder::new("cron")
.cron("*/5 * * * * * *")
.add_handler(|| { })
.build();
let job = JobBuilder::new("random")
.random(
ScheduleTime::Delay(Duration::from_secs(10)),
ScheduleTime::Delay(Duration::from_secs(20)),
)
.add_handler(|| { })
.build();
Check out the examples directory for more comprehensive examples:
To run an example:
cargo run --example basic_scheduler