13 releases (4 breaking)

0.8.0 Dec 19, 2024
0.7.0 Nov 22, 2024
0.6.5 Nov 30, 2024
0.6.2 Jul 27, 2024

#411 in Asynchronous

Download history 32/week @ 2024-09-21 68/week @ 2024-09-28 59/week @ 2024-10-05 23/week @ 2024-10-12 148/week @ 2024-10-19 14/week @ 2024-10-26 4/week @ 2024-11-02 88/week @ 2024-11-16 64/week @ 2024-11-23 129/week @ 2024-11-30 30/week @ 2024-12-07 99/week @ 2024-12-14 13/week @ 2024-12-21 3/week @ 2024-12-28 15/week @ 2025-01-04

133 downloads per month

MIT license

175KB
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

  • Run 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 a 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 shut down scheduler
    // Wait for completion of all running jobs
    scheduler.shutdown(ShutdownOpts::WaitForFinish).await
}

Feature flags

  • tz - enables cron schedules with timezone, see cron-lite documentation for details.

TODO

  • Make TaskId and JobId more flexible and convenient to create and refer tasks.
  • Tracing.
  • Task with limited execution time.
  • Refactor CronSchedule to move large enum from stack to heap.
  • More examples.

License

This project is licensed under the MIT license.

Dependencies

~5–15MB
~171K SLoC