8 releases

new 0.2.2 May 16, 2024
0.2.1 May 13, 2024
0.1.5 Apr 19, 2024
0.1.4 Jan 16, 2024
0.1.0 Dec 26, 2020

#84 in Concurrency

Download history 4/week @ 2024-02-14 20/week @ 2024-02-21 17/week @ 2024-02-28 2/week @ 2024-03-27 22/week @ 2024-04-03 5/week @ 2024-04-10 121/week @ 2024-04-17 3/week @ 2024-04-24 3629/week @ 2024-05-08 822/week @ 2024-05-15

4,455 downloads per month

MIT license

48KB
845 lines

CircleCI Crates.io Crates.io GitHub last commit Codecov License GitHub issues

⏱️ An asynchronous task scheduling library written in Rust

About

tasklet is a task scheduling library written in Rust. It is built over tokio runtime and utilizes green threads in order to run tasks asynchronously.

Dependencies

library version
cron 0.12.1
chrono 0.4.38
time 0.3.36
log 0.4.21
tokio 1.37.0

How to use this library

In your Cargo.toml add:

[dependencies]
tasklet = "0.2.2"

Example

Find more examples in the examples folder.

use log::{error, info};
use simple_logger::SimpleLogger;
use tasklet::{TaskBuilder, TaskScheduler};

/// A simple example of a task with two steps,
/// that might work or fail sometimes.
#[tokio::main]
async fn main() {
    // Init the logger.
    SimpleLogger::new().init().unwrap();

    // A variable to be passed in the task.
    let mut exec_count = 0;

    // Task scheduler with 1000ms loop frequency.
    let mut scheduler = TaskScheduler::default(chrono::Local);

    // Create a task with 2 steps and add it to the scheduler.
    // The second step fails every second execution.
    // Append the task to the scheduler.
    scheduler.add_task(
        TaskBuilder::new(chrono::Local)
            .every("1 * * * * * *")
            .description("A simple task")
            .add_step_default(|| {
                info!("Hello from step 1");
                Ok(()) // Let the scheduler know this step was a success.
            })
            .add_step_default(move || {
                if exec_count % 2 == 0 {
                    error!("Oh no this step failed!");
                    exec_count += 1;
                    Err(()) // Indicate that this step was a fail.
                } else {
                    info!("Hello from step 2");
                    exec_count += 1;
                    Ok(()) // Indicate that this step was a success.
                }
            })
            .build(),
    );

    // Execute the scheduler.
    scheduler.run().await;
}

Author

Stavros Grigoriou (stav121)

Dependencies

~6–13MB
~135K SLoC