grizzly_scheduler

Crates.iogrizzly_scheduler
lib.rsgrizzly_scheduler
version0.2.0
sourcesrc
created_at2024-02-12 16:19:05.576053
updated_at2024-10-09 10:39:36.086715
descriptionA scheduler for running async tasks using cron expressions. It is built on top of tokio. Tasks can be parallel or sequential. Fuzzy random offset can be added to the cron expression.
homepagehttps://github.com/ivan-brko/grizzly_scheduler
repositoryhttps://github.com/ivan-brko/grizzly_scheduler
max_upload_size
id1137138
size44,765
Ivan Brko (ivan-brko)

documentation

https://docs.rs/grizzly_scheduler

README

Grizzly Cron Scheduler

A simple and easy to use scheduler, built on top of Tokio, that allows you to schedule async tasks using cron expressions (with optional random fuzzy offsets for each trigger).

Tasks can be of two types:

  • Parallel: Two instances of this job can run at the same time if the previous run has not completed and new trigger time has arrived.
  • Sequential: The next run of the job will not start until the previous run has completed, so some triggers might be skipped.

Alternatives

  • tokio cron scheduler: Well-maintained and feature-rich, but it does not support fuzzy scheduling or differentiate between parallel and sequential jobs.

Features

  • Schedule parallel and sequential jobs
  • Schedule jobs using cron expressions
  • Integrated with Tracing for logging
  • Add random fuzzy offset to each trigger time
    • for example, if the job is configured to run at 30th second of every minute, you can add a fuzzy offset of 5 seconds, so the job will run at a random point between 25th and 35th second of every minute

Example

use std::sync::Arc;
use grizzly_scheduler::scheduler::Scheduler;

let scheduler = grizzly_scheduler::scheduler::Scheduler::new_in_utc();
let important_shared_state = Arc::new(5);
let cloned_state = important_shared_state.clone();

let job_id = scheduler.schedule_parallel_job(
       "*/5 * * * * *",                          // run the job on every second divisible by 5 of every minute
       Some("Example Parallel Job".to_string()), // this name will appear in the tracing logs
       Some("Some Category".to_string()),        // we can use this to stop all tasks of single category
       Some(chrono::Duration::seconds(2)),       // we want the fuzzy effect of maximally +/-2 seconds
       move ||
          {
             let cloned_state = cloned_state.clone();
             async move {
                 tracing::info!("We are using our important shared state! {}", cloned_state);
             }
          },
       ).unwrap();
scheduler.start().unwrap();

License

This project is licensed under MIT license, which can be found in the LICENSE file.

Contribution

All contributions are welcome! Please feel free to open an issue or a pull request.

Commit count: 9

cargo fmt