7 releases

0.3.2 Jun 18, 2024
0.3.1 Jul 3, 2023
0.3.0 May 22, 2021
0.2.1 May 10, 2021
0.1.1 May 10, 2021

#107 in Asynchronous

Download history 2007/week @ 2024-07-22 2531/week @ 2024-07-29 2427/week @ 2024-08-05 1818/week @ 2024-08-12 2622/week @ 2024-08-19 2811/week @ 2024-08-26 3051/week @ 2024-09-02 2466/week @ 2024-09-09 2724/week @ 2024-09-16 3368/week @ 2024-09-23 1781/week @ 2024-09-30 1906/week @ 2024-10-07 1041/week @ 2024-10-14 2207/week @ 2024-10-21 1770/week @ 2024-10-28 2759/week @ 2024-11-04

7,817 downloads per month
Used in 21 crates (6 directly)

MIT license

47KB
1.5K SLoC

MIT licensed Version Code Coverage Downloads

This crate provides a library for scheduling periodic tasks. It is inspired by python's schedule lib and provides similar API. It is built on tokio (version 1) and chrono lib

Documentation link

Crates.io link

Example

use std::error::Error;
use chrono::{Utc, Duration, Weekday};
use tokio::spawn;
use tokio_schedule::{every, Job};

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    let weekly = every(1).week().on(Weekday::Mon).at(12, 00, 00)
        .in_timezone(&Utc).perform(|| async { println!("Every week job") });
    spawn(weekly);

    let even_weekly = every(2).weeks().on(Weekday::Mon).at(12, 00, 00)
        .in_timezone(&Utc).perform(|| async { println!("Every even week job") });
    spawn(even_weekly);

    let every_30_seconds = every(30).seconds() // by default chrono::Local timezone
        .perform(|| async { println!("Every minute at 00'th and 30'th second") });
    spawn(every_30_seconds);

    let every_30_minutes = every(30).minutes().at(20).in_timezone(&Utc)
        .perform(|| async { println!("Every 30 minutes at 20'th second") });
    spawn(every_30_minutes);

    let every_hour = every(1).hour().at(10, 30).in_timezone(&Utc)
        .perform(|| async { println!("Every hour at :10:30") });
    spawn(every_hour);

    let every_second_1_day = every(1).second().until(&(Utc::now() + Duration::days(1)))
        .in_timezone(&Utc).perform(|| async { println!("Every second until next day") });
    spawn(every_second_1_day);

    let every_day = every(1).day().at(10, 00, 00)
        .in_timezone(&Utc).perform(|| async { println!("I'm scheduled!") });
    every_day.await;

    Ok(())
}

Timezone

By default all jobs use Local timezone. You can use any timezone that implements chrono::Timezone.

Dependencies

~3–8.5MB
~65K SLoC