#cron-job #cron #runner #thread #run #schedule #execute

crony

Simple cron runner that spawns another thread to run your cron jobs

6 releases

0.3.1 Jun 28, 2023
0.3.0 Feb 28, 2022
0.2.2 Jan 31, 2021
0.1.0 Dec 10, 2020

#114 in Concurrency

Download history 565/week @ 2023-11-27 344/week @ 2023-12-04 161/week @ 2023-12-11 221/week @ 2023-12-18 7/week @ 2023-12-25 17/week @ 2024-01-01 227/week @ 2024-01-08 209/week @ 2024-01-15 245/week @ 2024-01-22 187/week @ 2024-01-29 247/week @ 2024-02-05 190/week @ 2024-02-12 337/week @ 2024-02-19 170/week @ 2024-02-26 175/week @ 2024-03-04 206/week @ 2024-03-11

891 downloads per month

MIT license

19KB
292 lines

Crates.io Docs.rs

crony

crony = "0.3.0"

Crony: a simple cron runner

Use the Job trait to create your cron job struct, pass it to the Runner and then start it via run() method. Runner will spawn new thread where it will start looping through the jobs and will run their handle method once the scheduled time is reached.

If your OS has enough threads to spare each job will get its own thread to execute, if not it will be executed in the same thread as the loop but will hold the loop until the job is finished.

Please look at the Job trait documentation for more information.

Example

use crony::{Job, Runner, Schedule};
use std::thread;
use std::time::Duration;

struct ExampleJob;
impl Job for ExampleJob {
    fn schedule(&self) -> Schedule {
        "1/5 * * * * *".parse().unwrap()
    }
    fn handle(&self) {
        println!("Hello, I am a cron job running at: {}", self.now());
    }
}

fn run() {
    let mut runner = Runner::new();

    println!("Adding ExampleJob to the Runner");
    runner = runner.add(Box::new(ExampleJob));

    println!("Starting the Runner for 20 seconds");
    runner = runner.run();
    thread::sleep(Duration::from_millis(20 * 1000));

    println!("Stopping the Runner");
    runner.stop();
}

fn main() {
    run();
}

Output:

Adding ExampleJob to the Runner
Starting the Runner for 20 seconds
Hello, I am a cron job running at: 2021-01-31 03:06:25.908475 UTC
Hello, I am a cron job running at: 2021-01-31 03:06:30.912637 UTC
Hello, I am a cron job running at: 2021-01-31 03:06:35.926938 UTC
Hello, I am a cron job running at: 2021-01-31 03:06:40.962138 UTC
Stopping the Runner

License

Licensed under either of

at your option.

Contribution

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.

Dependencies

~2–8.5MB
~48K SLoC