Crates.io | aligned-task-scheduler |
lib.rs | aligned-task-scheduler |
version | 0.1.0 |
source | src |
created_at | 2024-12-10 01:49:04.833369 |
updated_at | 2024-12-10 01:49:04.833369 |
description | A scheduler that runs tasks at exact minute intervals with an optional offset, ensuring tasks are aligned to precise time boundaries. |
homepage | |
repository | https://github.com/starry-ABG/aligned-task-scheduler |
max_upload_size | |
id | 1477991 |
size | 13,496 |
AlignedTaskScheduler
is a scheduler built on the Tokio asynchronous runtime that runs tasks at precise minute intervals, with an optional offset in seconds. No matter when your application starts, it will first align to the next interval boundary before running tasks, ensuring tasks always trigger at exact times.
Add the dependency in your Cargo.toml
:
[dependencies]
aligned-task-scheduler = "0.1.0"
tokio = { version = "1", features = ["macros", "rt-multi-thread", "time"] }
In the example below, we create a scheduler that triggers a task every 5 minutes plus a 10-second offset. The task prints the aligned timestamp and the actual execution time:
use aligned_task_scheduler::AlignedTaskScheduler;
use std::time::SystemTime;
#[tokio::main]
async fn main() {
// Create a scheduler that runs tasks every 5 minutes + 10 seconds offset
let scheduler = AlignedTaskScheduler::new(5, 10);
// Provide an async closure that executes after each alignment
scheduler.run(|timestamp| async move {
println!("Task executed at aligned timestamp: {}", timestamp);
println!("Actual current time: {:?}", SystemTime::now());
// Insert your business logic here:
// e.g., fetch data, process results, write to a database, etc.
}).await;
}