#actix #job-scheduler #cron-job #async #self

bin+lib actix-jobs

A simple job scheduler for Actix

5 releases

0.1.7 Sep 14, 2023
0.1.6 Sep 14, 2023
0.1.2 Sep 14, 2023
0.1.1 Sep 14, 2023
0.1.0 Sep 13, 2023

#27 in #self

Download history 26/week @ 2025-04-09 20/week @ 2025-04-16 18/week @ 2025-04-30 22/week @ 2025-05-07 23/week @ 2025-05-14 2/week @ 2025-05-21 84/week @ 2025-06-04 101/week @ 2025-06-11 38/week @ 2025-06-18 100/week @ 2025-06-25 96/week @ 2025-07-02 33/week @ 2025-07-09 86/week @ 2025-07-16 108/week @ 2025-07-23

334 downloads per month

MIT license

9KB
121 lines

actix-jobs

Tests Crates.io Crates.io Docs License: MIT

A simple job scheduler for Actix.

Install

cargo add actix-jobs

Usage

Minimal example. For more information please refer to the Docs.

use actix_jobs::{Job, Scheduler, run_forever};

struct MyJob;
impl Job for MyJob {
    fn cron(&self) -> &str {
        "*/2 * * * * * *" // every two seconds
    }

    fn run(&mut self) {
        println!("Sending an email to all our clients...");
    }
}

#[actix_web::main]
async fn main() {
    let mut scheduler = Scheduler::new();
    scheduler.add(Box::new(MyJob));

    run_forever(scheduler); // This will start the scheduler in a new thread.

    // The rest of your program...
}

Information about the cron syntax.

Calling async functions inside run

This can be archieved via actix_rt::spawn as shown bellow.

use actix_jobs::{Job, Scheduler, run_forever};

struct MyJob;
impl Job for MyJob {
    fn cron(&self) -> &str {
        "*/2 * * * * * *" // every two seconds
    }

    fn run(&mut self) {
        actix_rt::spawn(async move {
            actix_rt::time::sleep(Duration::from_millis(1000)).await;

            println!("Some more async stuff...");
        }
    }
}

#[actix_web::main]
async fn main() {
    let mut scheduler = Scheduler::new();
    scheduler.add(Box::new(MyJob));

    run_forever(scheduler); // This will start the scheduler in a new thread.

    // The rest of your program...
}

Dependencies

~4–14MB
~146K SLoC