actix-jobs

Crates.ioactix-jobs
lib.rsactix-jobs
version0.1.7
sourcesrc
created_at2023-09-13 20:43:20.680032
updated_at2023-09-14 20:00:51.377868
descriptionA simple job scheduler for Actix
homepage
repository
max_upload_size
id971932
size25,244
Victor Garcia (TortitasT)

documentation

README

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...
}
Commit count: 0

cargo fmt