Crates.io | periodic_do |
lib.rs | periodic_do |
version | 0.1.0 |
source | src |
created_at | 2022-03-18 09:58:17.892058 |
updated_at | 2022-03-18 09:58:17.892058 |
description | Run task periodically until it reaches a terminal state |
homepage | https://github.com/qwfy/periodic_do |
repository | https://github.com/qwfy/periodic_do |
max_upload_size | |
id | 552618 |
size | 13,517 |
This is a library for running periodic tasks:
Job
trait for your domain specific data type T
, which represents the task to be runT
via a tokio::sync::mpsc::channel
to periodic_do::loop_forever
T
for two things:
T
up to be run? If so, run it and update the job stateT
reached a terminal state? In which case it will be forgottenUsable, but has rough edges, evaluate before using it in production.
use tokio;
#[tokio::main]
async fn main() -> Result<(), Error> {
let (sender, receiver) = tokio::sync::mpsc::channel(1000);
let unfinished_jobs = load_unfinished_jobs_from_database().await?;
// Start the scheduler
let capacity = periodic_do::Capacity {
max_running_jobs: 10,
sweep_sleep_seconds_default: 5,
sweep_sleep_seconds_min: 1,
sweep_sleep_seconds_max: 60,
};
tokio::spawn(async move {
periodic_do::loop_forever(
capacity, receiver, unfinished_jobs
).await
});
loop {
// T implements `Job` trait
let some_task: T = get_a_task_from_some_where().await;
sender.send(some_task).await
}
Ok(())
}