10 stable releases

1.2.2 Apr 27, 2023
1.2.1 Aug 10, 2022
1.1.3 Jun 19, 2022
1.0.3 Jun 19, 2022

#5 in #job-scheduling

Download history 33/week @ 2024-02-22 102/week @ 2024-02-29 3/week @ 2024-03-07 14/week @ 2024-03-14

129 downloads per month
Used in 2 crates

MIT license

12KB
127 lines

rcron

a simple cron-like job scheduling library for Rust

Usage

Be sure to add the rcron crate to your Cargo.toml:

[dependencies]
rcron = "1.2.2"

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
*     *     *      *              *       *             *

Time is for Local your local timezone.

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 rcron::{JobScheduler, Job};
use std::time::Duration;

fn main() {
    let mut sched = JobScheduler::new();

    sched.add(Job::new("1/10 * * * * *".parse().unwrap(), || {
        println!("exec task every 10 seconds!");
    }));

    sched.add(Job::new("1/5 * * * * *".parse().unwrap(), || {
        println!("exec task every 5 seconds!");
    }));

    loop {
        sched.tick();

        std::thread::sleep(Duration::from_millis(500));
    }
}

example

cargo run --package rcron --example rcron_basic 

    Finished dev [unoptimized + debuginfo] target(s) in 0.09s
     Running `target/debug/examples/rcron_basic`
exec task every 10 seconds!
exec task every 5 seconds!
exec task every 5 seconds!
exec task every 10 seconds!
exec task every 5 seconds!
exec task every 5 seconds!
exec task every 10 seconds!
exec task every 5 seconds!
exec task every 5 seconds!

Similar Libraries

  • cron the cron expression parser we use.
  • schedule-rs is a similar rust library that implements it's own cron expression parser.
  • tokio-cron-scheduler Schedule tasks on Tokio using cron-like annotation.

refer

License

rcron is licensed under either of

  • MIT license ([LICENSE])(LICENSE)

Dependencies

~2.1–9MB
~50K SLoC