Expand description
§JobScheduler
A simple cron-like job scheduling library for Rust.
§Usage
Be sure to add the job_scheduler_ng crate to your Cargo.toml:
[dependencies]
job_scheduler_ng = "2"Creating a schedule for a job is done using the FromStr impl for the
Schedule type of the cron library.
Or
Cron type of the croner library.
See the documentation of the feature you use on which scheduling format is supported.
The scheduling format is as follows:
sec min hour day of month month day of week year
* * * * * * *Note that the year may be omitted.
Comma separated values such as 5,8,10 represent more than one time
value. So for example, a schedule of 0 2,14,26 * * * * would execute
on the 2nd, 14th, and 26th minute of every hour.
Ranges can be specified with a dash. A schedule of 0 0 * 5-10 * *
would execute once per hour but only on day 5 through 10 of the month.
§Features
cron(default): Uses theSchedulestruct from the cron library.croner: Uses theCronstruct from the croner library.
Only one of these features can be enabled at the same time. To use croner, disable the default features:
[dependencies]
job_scheduler_ng = { version = "2", default-features = false, features = ["croner"] }§Example
A simple usage example:
use job_scheduler_ng::{JobScheduler, Job, MIN_DURATION};
use core::time::Duration;
let mut sched = JobScheduler::new();
sched.add(Job::new("0/10 * * * * *".parse().expect("Valid schedule"), || {
println!("I get executed every 10th second!");
}));
sched.add(Job::new("*/4 * * * * *".parse().expect("Valid schedule"), || {
println!("I get executed every 4 seconds!");
}));
loop {
sched.tick();
std::thread::sleep(Duration::from_millis(MIN_DURATION));
}Setting a custom timezone other than the default UTC
Any Tz::Offset provided by chrono will work.
use chrono::Local;
use job_scheduler_ng::{JobScheduler, Job, MIN_DURATION};
use core::time::Duration;
let mut sched = JobScheduler::new();
let local_tz = Local::now();
sched.set_timezone(*local_tz.offset());
sched.add(Job::new("0 5 13 * * *".parse().expect("Valid schedule"), || {
println!("I get executed every day 13:05 local time!");
}));
loop {
sched.tick();
std::thread::sleep(Duration::from_millis(MIN_DURATION));
}§How jobs are executed
Jobs are executed one after the other, on the thread which calls tick(), in the order they were added.
A long running job delays all jobs after it. Spawn a thread or use a channel if this is an issue,
see the simple_job_thread and simple_job_mpsc examples.
The first call to tick() only stores the current time and does not run any jobs.
Every scheduled run is executed at most once. If a job panics, that run is not retried.
If the system clock is set backwards, jobs could run twice once the clock catches up again.
Structs§
- Job
- A schedulable
Job. - JobScheduler
- The
JobSchedulercontains and executes the scheduled jobs. - Schedule
- Uuid
- A Universally Unique Identifier (UUID).
Constants§
- MIN_
DURATION - Recommended minimum sleep between
tick()calls, in milliseconds.500for thecronbackend,1_000forcroner.