38 releases

new 0.6.3 Jan 5, 2025
0.6.2 Dec 15, 2024
0.6.0 Nov 28, 2024
0.6.0-rc.5 Jul 25, 2024
0.3.4 Sep 20, 2022

#729 in Asynchronous

Download history 2037/week @ 2024-09-14 1653/week @ 2024-09-21 1229/week @ 2024-09-28 1623/week @ 2024-10-05 878/week @ 2024-10-12 1826/week @ 2024-10-19 1215/week @ 2024-10-26 2002/week @ 2024-11-02 1869/week @ 2024-11-09 1506/week @ 2024-11-16 1652/week @ 2024-11-23 940/week @ 2024-11-30 731/week @ 2024-12-07 566/week @ 2024-12-14 232/week @ 2024-12-21 177/week @ 2024-12-28

1,884 downloads per month

MIT license

150KB
3K SLoC

apalis-cron

A simple yet extensible library for cron-like job scheduling for rust. Since apalis-cron is build on top of apalis which supports tower middleware, you should be able to easily add middleware such as tracing, retries, load shed, concurrency etc.

Example

use apalis::{prelude::*, layers::retry::RetryPolicy};
use std::str::FromStr;
use apalis_cron::{CronStream, Schedule};
use chrono::{DateTime, Utc};

#[derive(Default, Debug, Clone)]
struct Reminder(DateTime<Utc>);
impl From<DateTime<Utc>> for Reminder {
   fn from(t: DateTime<Utc>) -> Self {
       Reminder(t)
   }
}
async fn handle_tick(job: Reminder, data: Data<usize>) {
    // Do something with the current tick
}

#[tokio::main]
async fn main() {
    let schedule = Schedule::from_str("@daily").unwrap();

    let worker = WorkerBuilder::new("morning-cereal")
        .retry(RetryPolicy::retries(5))
        .data(42usize)
        .backend(CronStream::new(schedule))
        .build_fn(handle_tick);

    worker.run().await;
}

Persisting cron jobs

Sometimes we may want to persist cron jobs for several reasons:

  • Distribute cronjobs between multiple servers
  • Store the results of the cronjob
  • Prevent task skipping in the case of a restart
#[tokio::main]
async fn main() {
    let schedule = Schedule::from_str("@daily").unwrap();
    let cron_stream = CronStream::new(schedule);

    // Lets create a storage for our cron jobs
    let pool = SqlitePool::connect("sqlite::memory:").await.unwrap();
    SqliteStorage::setup(&pool)
        .await
        .expect("unable to run migrations for sqlite");
    let sqlite = SqliteStorage::new(pool);

    let backend = cron_stream.pipe_to_storage(sqlite);

    let worker = WorkerBuilder::new("morning-cereal")
        .backend(backend)
        .build_fn(handle_tick);

    worker.run().await;
}

Dependencies

~4–13MB
~176K SLoC