Crate job_scheduler_ng

Crate job_scheduler_ng 

Source
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 = "*"

Creating a schedule for a job is done using the FromStr impl for the Schedule type of the cron library.

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.

Day of the week can be specified as an abbreviation or the full name. A schedule of 0 0 6 * * Sun,Sat would execute at 6am on Sunday and Saturday.

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().unwrap(), || {
    println!("I get executed every 10th second!");
}));

sched.add(Job::new("*/4 * * * * *".parse().unwrap(), || {
    println!("I get executed every 4 seconds!");
}));

loop {
    sched.tick();
    std::thread::sleep(Duration::from_millis(MIN_DURATION));
}

Setting a custom timezone other then 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().unwrap(), || {
    println!("I get executed every day 13:05 local time!");
}));

loop {
    sched.tick();
    std::thread::sleep(Duration::from_millis(MIN_DURATION));
}

Structs§

Job
A schedulable Job.
JobScheduler
The JobScheduler contains and executes the scheduled jobs.
Schedule
Uuid
A Universally Unique Identifier (UUID).

Constants§

MIN_DURATION