11 releases

new 0.2.5 Nov 12, 2024
0.2.4 Nov 9, 2024
0.2.3 Sep 13, 2024
0.2.2 Aug 29, 2024
0.1.1 Jul 21, 2020

#400 in Concurrency

Download history 75/week @ 2024-07-25 18/week @ 2024-08-01 17/week @ 2024-08-08 4/week @ 2024-08-15 42/week @ 2024-08-22 186/week @ 2024-08-29 62/week @ 2024-09-05 259/week @ 2024-09-12 136/week @ 2024-09-19 196/week @ 2024-09-26 144/week @ 2024-10-03 139/week @ 2024-10-10 109/week @ 2024-10-17 73/week @ 2024-10-24 144/week @ 2024-10-31 259/week @ 2024-11-07

599 downloads per month

MIT license

26KB
414 lines

CronTab

Build codecov

A cron job library for Rust.

Features

  • Sync and Async Support: Manage cron jobs in both synchronous and asynchronous contexts.
  • Flexible Cron Expressions: Use standard cron expressions for job scheduling.

Usage

Please see the Documentation for more details.

Add to your Cargo.toml:

[dependencies]
cron_tab = {version = "0.2", features = ["sync", "async"]}

The cron expression format:

sec   min   hour   day of month   month   day of week   year
*     *     *      *              *       *             *

Example:

extern crate cron_tab;  
  
use chrono::{FixedOffset, Local, TimeZone, Utc};  
  
fn main() {  
 let local_tz = Local::from_offset(&FixedOffset::east(7));  
 let utc_tz = Utc;  
 
 // create new cron with timezone
 let mut cron = cron_tab::Cron::new(utc_tz);  
  
 // add test fn to cron
 let job_test_id = cron.add_fn("* * * * * * *", test).unwrap();  
  
 // start cron
 cron.start();  

 // sleep 2 second
 std::thread::sleep(std::time::Duration::from_secs(2));
 
 // add one more function
 let anonymous_job_id = cron.add_fn("* * * * * *", || {  
            println!("anonymous fn");  
 }).unwrap();  
  
 // remove job_test  
 cron.remove(job_test_id);  
  
 // sleep 2 second
 std::thread::sleep(std::time::Duration::from_secs(2));
  
 // stop cron  
 cron.stop();  
}  
  
fn test() {  
    println!("now: {}", Local::now().to_string());  
}

Async Example:

use std::sync::Arc;

use chrono::{FixedOffset, Local, TimeZone};
use cron_tab::AsyncCron;
use tokio::sync::Mutex;

#[tokio::main]
async fn main() {
    let local_tz = Local::from_offset(&FixedOffset::east(7));
    let mut cron = AsyncCron::new(local_tz);

    let first_job_id = cron.add_fn("* * * * * *", print_now).await;

    cron.start().await;

    let counter = Arc::new(Mutex::new(1));
    cron.add_fn("* * * * * *", move || {
        let counter = counter.clone();
        async move {
            let mut counter = counter.lock().await;
            *counter += 1;
            let now = Local::now().to_string();
            println!("{} counter value: {}", now, counter);
        }
    })
    .await;

    std::thread::sleep(std::time::Duration::from_secs(10));

    // stop cron
    cron.stop();
}

async fn print_now() {
    println!("now: {}", Local::now().to_string());
}

License

Dependencies

~2.5–9.5MB
~88K SLoC