Crates.io | tasklet |
lib.rs | tasklet |
version | 0.2.3 |
source | src |
created_at | 2020-12-26 12:35:22.981163 |
updated_at | 2024-08-08 06:27:42.853815 |
description | An asynchronous task scheduling library |
homepage | |
repository | https://github.com/stav121/tasklet |
max_upload_size | |
id | 327464 |
size | 96,440 |
⏱️ An asynchronous task scheduling library written in Rust
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.
library | version |
---|---|
cron | 0.12.1 |
chrono | 0.4.38 |
time | 0.3.36 |
log | 0.4.21 |
tokio | 1.37.0 |
In your Cargo.toml
add:
[dependencies]
tasklet = "0.2.3"
Find more examples in the examples folder.
use log::info;
use simple_logger::SimpleLogger;
use tasklet::task::TaskStepStatusErr::Error;
use tasklet::task::TaskStepStatusOk::Success;
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("Step 1", || {
info!("Hello from step 1");
Ok(Success) // Let the scheduler know this step was a success.
})
.add_step("Step 2", move || {
if exec_count % 2 == 0 {
exec_count += 1;
Err(Error(Some("Oh no this task failed".into()))) // Indicate that this step was a fail.
} else {
info!("Hello from step 2");
exec_count += 1;
Ok(Success) // Indicate that this step was a success.
}
})
.build(),
);
// Execute the scheduler.
scheduler.run().await;
}
Stavros Grigoriou (stav121)