7 个版本

0.2.1 2023年2月26日
0.2.0 2023年2月26日
0.1.4 2022年6月7日
0.1.1 2020年7月21日

#23#cron-job

Download history 37/week @ 2024-03-11 31/week @ 2024-03-18 2/week @ 2024-03-25 32/week @ 2024-04-01 13/week @ 2024-04-08 18/week @ 2024-04-15 24/week @ 2024-04-22 10/week @ 2024-04-29 21/week @ 2024-05-06 14/week @ 2024-05-13 65/week @ 2024-05-20 3/week @ 2024-05-27 30/week @ 2024-06-03 41/week @ 2024-06-10 61/week @ 2024-06-17 8/week @ 2024-06-24

141 每月下载量

MIT 许可协议

24KB
411 代码行数,不包括注释

CronTab

Build

Rust 的 cron 作业库。

用法

请参阅文档获取更多详细信息。

添加到您的 Cargo.toml

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

cron 表达式格式

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

示例

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());  
}

异步示例

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());
}

许可协议

依赖项

~2.6–10MB
~90K SLoC