CronJob each last day of month

This is the code to execute a Cronjob each 20 seconds.

    let mut cron = CronJob::new("EXPIRING_VOUCHERS", handle_expiring_vouchers);
    // Start the cronjob

But how would I make a cronjob which executes each last day of the month?


How would I do that in the CronJob crate? They only have functions like day_of_month().

I would just use the cron crate (on which cronjob depends) and spawn a thread myself.

If you have a look at the code of cronjob it's a really thin (and imo bad) wrapper for cron.

cronjob won't report an error and will panic if your schedule is invalid, the thread it spawns will also get zombified.

1 Like

Okay, so I'll take the first day of each month instead and then just subtract one month.
I currently have this

// Cronjob
let expression = "0 0 1 * *";
let schedule = Schedule::from_str(expression).unwrap();

The documentation of the cron-crate only explains on how to output the next launching times. But how do I just trigger a function with this cron?

[Very] Naively, take the next upcoming job. Subtract from it the current time, that gets you a duration, wait for that duration, run your code.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.