7 releases

0.6.1 Jun 14, 2024
0.6.0 Jun 10, 2024
0.5.1 Jun 3, 2024
0.5.0 May 13, 2024
0.4.1 Apr 29, 2024

#255 in Asynchronous

Download history 124/week @ 2024-04-18 223/week @ 2024-04-25 168/week @ 2024-05-02 141/week @ 2024-05-09 27/week @ 2024-05-16 41/week @ 2024-05-23 177/week @ 2024-05-30 181/week @ 2024-06-06 202/week @ 2024-06-13 45/week @ 2024-06-20 10/week @ 2024-06-27 12/week @ 2024-07-04 3/week @ 2024-07-11

72 downloads per month

MIT license

160KB
3.5K SLoC

SACS - Simple Asynchronous Cron Scheduler

SACS is easy to use, lightweight scheduler and executor of repeatable async tasks for Tokio runtime.

CI status Audit status Crates.io publishing status docs.rs status Version at Crates.io License

Features

  • Runs tasks with different types of schedule: once, with delay, by interval, with a cron schedule.
  • Uses current Tokio runtime or creates new one with specified type, number of threads and limited parallelism.
  • Allows task cancellation, getting current state and runtime statistics of the task.
  • Task execution time may be limited.
  • Lightweight, small, easy to use.

Quick start

Just create Scheduler and add Task to it. Refer to the crate's documentation for more examples and details of possible usage.

use sacs::{
    scheduler::{Scheduler, ShutdownOpts, TaskScheduler},
    task::{CronOpts, Task, TaskSchedule},
    Result,
};
use std::time::Duration;
use tracing::info;

#[tokio::main]
async fn main() -> Result<()> {
    tracing_subscriber::fmt::init();

    // Create scheduler with default config
    let scheduler = Scheduler::default();

    // Create task with cron schedule: repeat it every 3 seconds
    let cron = TaskSchedule::Cron("*/3 * * * * *".try_into()?, CronOpts::default());
    let task = Task::new(cron, |id| {
        Box::pin(async move {
            info!("Job {id} started.");
            // Actual async workload here
            tokio::time::sleep(Duration::from_secs(2)).await;
            // ...
            info!("Job {id} finished.");
        })
    });

    // Post task to the scheduler and forget it :)
    let _task_id = scheduler.add(task).await?;

    // ... and do any other async work in parallel
    tokio::time::sleep(Duration::from_secs(10)).await;

    // It's not mandatory but good to shutdown scheduler
    // Wait for completion of all running jobs
    scheduler.shutdown(ShutdownOpts::WaitForFinish).await
}

TODO

  • Make TaskId and JobId more flexible and convenient to create and refer tasks.
  • Tracing.
  • Task with limited execution time.
  • More examples.

License

This project is licensed under the MIT license.

Dependencies

~6–16MB
~185K SLoC