Crate recurring_tasks

Source
Expand description

Scheduled async tasks / jobs manager to run forever, but not run jobs if already/still running

Simple example:

use std::time::{Duration, SystemTime};

use tracing::info;

use recurring_tasks::{AsyncTask, TaskManager};

pub struct HeartbeatTask;

#[async_trait::async_trait]
impl AsyncTask for HeartbeatTask {
    async fn run(&self) -> Result<(), String> {
        info!("Heartbeat");
        Ok(())
    }
}

#[tokio::main]
async fn main() {
    let task_manager = TaskManager::default();

    // run a heartbeat task every 5 seconds
    task_manager
        .add("Heartbeat", Duration::from_secs(5), HeartbeatTask {})
        .await;

    // this will run until ctl-c! not suitable for a cargo test example ;)
    //task_manager.run_with_signal().await;
    println!("Shutdown");
}

For a fancier example, see the repo: db query task

This crate is intended to be very direct and specific. For a more elaborate scheduling crate, using crontab syntax, consider tokio-cron-scheduler. There are also a variety of additional alternatives out there, each with different priorities.

Structs§

TaskManager
Task manager that schedules and runs tasks on schedule, indefinitely

Traits§

AsyncTask
Trait for tasks that can be run asynchronously, with the Task Manager