#job-scheduler #job-scheduling #cron-job #cron #cron-scheduler #job #scheduler

job_scheduler_ng

A simple cron-like job scheduling library for Rust (Updated since 2022)

6 stable releases

2.0.5 Apr 24, 2024
2.0.4 Feb 1, 2023
2.0.3 Dec 10, 2022
2.0.2 Oct 9, 2022
2.0.0 May 20, 2022

#37 in Date and time

Download history 729/week @ 2024-07-31 1261/week @ 2024-08-07 879/week @ 2024-08-14 924/week @ 2024-08-21 897/week @ 2024-08-28 838/week @ 2024-09-04 885/week @ 2024-09-11 1038/week @ 2024-09-18 1069/week @ 2024-09-25 1602/week @ 2024-10-02 1229/week @ 2024-10-09 1207/week @ 2024-10-16 1135/week @ 2024-10-23 959/week @ 2024-10-30 1178/week @ 2024-11-06 999/week @ 2024-11-13

4,425 downloads per month
Used in 10 crates (4 directly)

MIT/Apache

15KB
152 lines

JobScheduler

A simple cron-like job scheduling library for Rust.

Forked from https://github.com/lholden/job_scheduler, thanks @lholden! This is a fork which i try to maintain and maybe even improve where needed.

Updates

2024-04-24 (v2.0.5):

  • Set MSRV to v1.61.0 to match chrono's v0.4.34 MSRV
  • Updated dev dependency of tokio to v1.37.0 or higher
  • Several clippy check added
  • Fixed all clippy reported items
  • Set JobScheduler::new() as const fn
  • Updated examples to use a log function and always print the current thread id
  • Added a very simple hash to better differentiate the tokio 5th second example

2023-02-01 (v2.0.4):

  • Validated uuid v1.3.0 works
  • Used miri to check examples Added an std::process::exit(0); to produce a clean exit.
  • Set MSRV to v1.56.1
  • Updated dev dependency of tokio to v1.25.0 or higher

2022-12-10 (v2.0.3):

  • Don't require Sync trait for job function (PR #1 - Thanks @mikel1703)
  • Added two other examples. One using threading, and one also using MPSC.
  • Added some clippy checks
  • Fixed some spelling errors

2022-10-09 (v2.0.2):

  • Updated cron to v0.12.0
  • Set chrono v0.4.20 as minimum version to mitigate a know CVE.

Usage

Please see the Documentation for more details.

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

Time is specified for UTC and not your local timezone. 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:

extern crate job_scheduler_ng;
use job_scheduler_ng::{JobScheduler, Job};
use std::time::Duration;

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

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

    loop {
        sched.tick();

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

Similar Libraries

  • cron the cron expression parser we use.
  • schedule-rs is a similar rust library that implements it's own cron expression parser.

License

JobScheduler is licensed under either of

Contributing

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Please see the CONTRIBUTING file for more information.

Dependencies

~2.2–7.5MB
~51K SLoC