Crate recurring_tasks

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;

use tracing::info;

use recurring_tasks::{AsyncTask, TaskManager, CancellationToken};

pub struct HeartbeatTask;

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

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

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

    // 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§

CancellationToken
A token which can be used to signal a cancellation request to one or more tasks.
TaskManager
Task manager that schedules and runs tasks on schedule, until cancelled

Traits§

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