5 releases

0.1.4 Dec 27, 2023
0.1.3 Dec 27, 2023
0.1.2 Dec 27, 2023
0.1.1 Nov 29, 2023
0.1.0 Nov 29, 2023

#1903 in Web programming

Download history 36/week @ 2024-01-02 50/week @ 2024-01-09 100/week @ 2024-01-16 2/week @ 2024-01-23 29/week @ 2024-01-30 34/week @ 2024-02-13 145/week @ 2024-02-20 38/week @ 2024-02-27 37/week @ 2024-03-05 65/week @ 2024-03-12 250/week @ 2024-03-19 76/week @ 2024-03-26 58/week @ 2024-04-02 63/week @ 2024-04-09 39/week @ 2024-04-16

252 downloads per month
Used in spider

MIT license

15KB
261 lines

async_job

A simple trait for async cron jobs in Rust.

Getting Started

  1. cargo add async_job

Usage

use async_job::{Job, Runner, Schedule, async_trait};

struct ExampleJob;

#[async_trait]
impl Job for ExampleJob {
    fn schedule(&self) -> Option<Schedule> {
        Some("1/5 * * * * *".parse().unwrap())
    }
    // run any async or sync task here with mutation capabilities
    async fn handle(&mut self) {
        println!("Hello, I am a cron job running at: {}", self.now());
    }
}

If you need to use a single threaded env disable the default feature and set the feature rt,

Feature Flags

  1. rt: Single threaded tokio runtime.
  2. rt-multi-thread: Multi threaded tokio runtime. Enabled by default

Examples

Run the example with cargo run --example example


lib.rs:

async_job: a simple async 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 async task 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 async_job::{Job, Runner, Schedule, async_trait};
use tokio::time::Duration;
use tokio;

struct ExampleJob;

#[async_trait]
impl Job for ExampleJob {
    fn schedule(&self) -> Option<Schedule> {
        Some("1/5 * * * * *".parse().unwrap())
    }
    async fn handle(&mut self) {
        println!("Hello, I am a cron job running at: {}", self.now());
    }
}

async 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().await;
    tokio::time::sleep(Duration::from_millis(20 * 1000)).await;

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

#[tokio::main]
async fn main() {
    run().await;
}

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

Dependencies

~5–13MB
~120K SLoC