encore-dev 2.0.0-beta.5

Rust SDK for Encore applications
Documentation
//! Cron jobs: requests Encore makes of a service on a schedule.
//!
//! A job is declared as a `static`, with the path it requests and when:
//!
//! ```ignore
//! use encore::cron::{Job, Schedule};
//!
//! /// Removes the sessions nobody has used in a month.
//! static CLEANUP: Job =
//!     Job::new("cleanup", "/internal/cleanup", Schedule::Cron("0 4 * * *")).title("Nightly cleanup");
//!
//! static REFRESH: Job = Job::new("refresh-rates", "/internal/rates", Schedule::Every("30m"));
//! ```
//!
//! When the job is due, Encore sends a `POST` request for the path to the
//! service that declares the job. What runs is whatever the service serves
//! there, so the route and the job are best kept in step by declaring the one
//! from the other:
//!
//! ```ignore
//! let app = Router::new().route(CLEANUP.path(), post(cleanup));
//! ```
//!
//! # Running locally and under test
//!
//! Nothing is scheduled by `encore run` or `encore test`: a job runs where the
//! app is deployed. To exercise one, request its path, or call what serves it.

/// When a [`Job`] runs.
///
/// Written out where the job is declared, with a string literal: Encore reads
/// it from the source.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Schedule {
    /// At the times a [cron expression] names: five fields — minute, hour, day
    /// of month, month and day of week — in UTC. `"0 4 * * *"` is 04:00 every
    /// day, `"0 9 * * 1"` 09:00 on Mondays.
    ///
    /// [cron expression]: https://en.wikipedia.org/wiki/Cron#Cron_expression
    Cron(&'static str),
    /// Every so often: a duration of hours and minutes, as in `"30m"`, `"6h"`
    /// or `"1h30m"`.
    ///
    /// The runs are laid out over the day from midnight UTC, the same every
    /// day — `"6h"` is 00:00, 06:00, 12:00 and 18:00 — so the interval is a
    /// whole number of minutes that divides 24 hours evenly: `"45m"` and
    /// `"8h"` do, `"50m"` and `"7h"` do not.
    Every(&'static str),
}

/// A cron job. See the [module documentation](self).
///
/// Declared as a module-level `static`, with a name unique in the app:
/// lowercase letters, digits and dashes.
#[derive(Debug)]
pub struct Job {
    name: &'static str,
    path: &'static str,
    schedule: Schedule,
    title: Option<&'static str>,
}

impl Job {
    /// Declares the job `name`, which requests `path` of the service that
    /// declares it whenever `schedule` says it is due. The path starts with a
    /// `/`.
    ///
    /// The name and the path must be string literals, and the schedule
    /// written out here: Encore reads them from the source.
    pub const fn new(name: &'static str, path: &'static str, schedule: Schedule) -> Self {
        Self {
            name,
            path,
            schedule,
            title: None,
        }
    }

    /// What the job is called where people read it, in place of its name.
    pub const fn title(mut self, title: &'static str) -> Self {
        self.title = Some(title);
        self
    }

    /// The job's name.
    pub const fn name(&self) -> &'static str {
        self.name
    }

    /// The path the job requests: where the service serves it.
    pub const fn path(&self) -> &'static str {
        self.path
    }

    /// When the job runs.
    pub const fn schedule(&self) -> Schedule {
        self.schedule
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    static CLEANUP: Job = Job::new("cleanup", "/internal/cleanup", Schedule::Cron("0 4 * * *"))
        .title("Nightly cleanup");
    static REFRESH: Job = Job::new("refresh-rates", "/internal/rates", Schedule::Every("30m"));

    #[test]
    fn a_job_is_declared_in_a_static() {
        assert_eq!(CLEANUP.name(), "cleanup");
        assert_eq!(CLEANUP.path(), "/internal/cleanup");
        assert_eq!(CLEANUP.schedule(), Schedule::Cron("0 4 * * *"));
        assert_eq!(CLEANUP.title, Some("Nightly cleanup"));

        assert_eq!(REFRESH.schedule(), Schedule::Every("30m"));
        assert_eq!(REFRESH.title, None);
    }
}