apalis-cron

Crates.ioapalis-cron
lib.rsapalis-cron
version0.6.0-rc.8
sourcesrc
created_at2022-08-13 07:01:45.43164
updated_at2024-10-11 04:12:09.681102
descriptionA simple yet extensible library for cron-like job scheduling for rust.
homepage
repositoryhttps://github.com/geofmureithi/apalis
max_upload_size
id644542
size8,822
Geoffrey Mureithi (geofmureithi)

documentation

README

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::layers::retry::RetryLayer;
use apalis::layers::retry::RetryPolicy;
use tower::ServiceBuilder;
use apalis_cron::Schedule;
use std::str::FromStr;
use apalis::prelude::*;
use apalis_cron::CronStream;
use chrono::{DateTime, Utc};

#[derive(Clone)]
struct FakeService;
impl FakeService {
    fn execute(&self, item: Reminder){}
}

#[derive(Default, Debug, Clone)]
struct Reminder(DateTime<Utc>);
impl From<DateTime<Utc>> for Reminder {
   fn from(t: DateTime<Utc>) -> Self {
       Reminder(t)
   }
}
async fn send_reminder(job: Reminder, svc: Data<FakeService>) {
    svc.execute(job);
}

#[tokio::main]
async fn main() {
    let schedule = Schedule::from_str("@daily").unwrap();
    let worker = WorkerBuilder::new("morning-cereal")
        .layer(RetryLayer::new(RetryPolicy::retries(5)))
        .data(FakeService)
        .stream(CronStream::new(schedule).into_stream())
        .build_fn(send_reminder);
    Monitor::<TokioExecutor>::new()
        .register(worker)
        .run()
        .await
        .unwrap();
}
Commit count: 533

cargo fmt