39 releases

0.6.4 Jan 15, 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

#735 in Asynchronous

Download history 1682/week @ 2024-10-06 825/week @ 2024-10-13 1827/week @ 2024-10-20 1228/week @ 2024-10-27 2080/week @ 2024-11-03 1770/week @ 2024-11-10 1509/week @ 2024-11-17 1654/week @ 2024-11-24 945/week @ 2024-12-01 725/week @ 2024-12-08 553/week @ 2024-12-15 293/week @ 2024-12-22 151/week @ 2024-12-29 699/week @ 2025-01-05 684/week @ 2025-01-12 418/week @ 2025-01-19

2,039 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–14MB
~181K SLoC