film42 / sidekiq-rs

A port of sidekiq to rust using tokio
MIT License
95 stars 10 forks source link

Initial implementation of periodic jobs #4

Closed film42 closed 2 years ago

film42 commented 2 years ago

The core idea is that we use a sorted set in redis to keep a PeriodicJob json string that contains a cron string plus enough data to enqueue a job with some args. The sorted set is sorted by the unix timestamp of the next scheduled time for the periodic job.

When a sorted periodic jobs score passes now(), a process will pull the periodic job json, find the next timestamp the job should run, and use ZADD to update the periodic job in redis. Redis will tell us if we changed the value. If so, the process will enqueue a job.

Example:

    // Reset cron jobs
    periodic::destroy_all(redis).await?;

    // Cron jobs
    periodic::builder("0 * * * * *")?
        .name("Payment report processing for a random user")
        .queue("yolo")
        .args(PaymentReportArgs {
            user_guid: "USR-123-PERIODIC".to_string(),
        })?
        .register(&mut processor, PaymentReportWorker::new(logger))
        .await?;