encore/cron.rs
1//! Cron jobs: requests Encore makes of a service on a schedule.
2//!
3//! A job is declared as a `static`, with the path it requests and when:
4//!
5//! ```ignore
6//! use encore::cron::{Job, Schedule};
7//!
8//! /// Removes the sessions nobody has used in a month.
9//! static CLEANUP: Job =
10//! Job::new("cleanup", "/internal/cleanup", Schedule::Cron("0 4 * * *")).title("Nightly cleanup");
11//!
12//! static REFRESH: Job = Job::new("refresh-rates", "/internal/rates", Schedule::Every("30m"));
13//! ```
14//!
15//! When the job is due, Encore sends a `POST` request for the path to the
16//! service that declares the job. What runs is whatever the service serves
17//! there, so the route and the job are best kept in step by declaring the one
18//! from the other:
19//!
20//! ```ignore
21//! let app = Router::new().route(CLEANUP.path(), post(cleanup));
22//! ```
23//!
24//! # Running locally and under test
25//!
26//! Nothing is scheduled by `encore run` or `encore test`: a job runs where the
27//! app is deployed. To exercise one, request its path, or call what serves it.
28
29/// When a [`Job`] runs.
30///
31/// Written out where the job is declared, with a string literal: Encore reads
32/// it from the source.
33#[derive(Debug, Clone, Copy, PartialEq, Eq)]
34pub enum Schedule {
35 /// At the times a [cron expression] names: five fields — minute, hour, day
36 /// of month, month and day of week — in UTC. `"0 4 * * *"` is 04:00 every
37 /// day, `"0 9 * * 1"` 09:00 on Mondays.
38 ///
39 /// [cron expression]: https://en.wikipedia.org/wiki/Cron#Cron_expression
40 Cron(&'static str),
41 /// Every so often: a duration of hours and minutes, as in `"30m"`, `"6h"`
42 /// or `"1h30m"`.
43 ///
44 /// The runs are laid out over the day from midnight UTC, the same every
45 /// day — `"6h"` is 00:00, 06:00, 12:00 and 18:00 — so the interval is a
46 /// whole number of minutes that divides 24 hours evenly: `"45m"` and
47 /// `"8h"` do, `"50m"` and `"7h"` do not.
48 Every(&'static str),
49}
50
51/// A cron job. See the [module documentation](self).
52///
53/// Declared as a module-level `static`, with a name unique in the app:
54/// lowercase letters, digits and dashes.
55#[derive(Debug)]
56pub struct Job {
57 name: &'static str,
58 path: &'static str,
59 schedule: Schedule,
60 title: Option<&'static str>,
61}
62
63impl Job {
64 /// Declares the job `name`, which requests `path` of the service that
65 /// declares it whenever `schedule` says it is due. The path starts with a
66 /// `/`.
67 ///
68 /// The name and the path must be string literals, and the schedule
69 /// written out here: Encore reads them from the source.
70 pub const fn new(name: &'static str, path: &'static str, schedule: Schedule) -> Self {
71 Self {
72 name,
73 path,
74 schedule,
75 title: None,
76 }
77 }
78
79 /// What the job is called where people read it, in place of its name.
80 pub const fn title(mut self, title: &'static str) -> Self {
81 self.title = Some(title);
82 self
83 }
84
85 /// The job's name.
86 pub const fn name(&self) -> &'static str {
87 self.name
88 }
89
90 /// The path the job requests: where the service serves it.
91 pub const fn path(&self) -> &'static str {
92 self.path
93 }
94
95 /// When the job runs.
96 pub const fn schedule(&self) -> Schedule {
97 self.schedule
98 }
99}
100
101#[cfg(test)]
102mod tests {
103 use super::*;
104
105 static CLEANUP: Job = Job::new("cleanup", "/internal/cleanup", Schedule::Cron("0 4 * * *"))
106 .title("Nightly cleanup");
107 static REFRESH: Job = Job::new("refresh-rates", "/internal/rates", Schedule::Every("30m"));
108
109 #[test]
110 fn a_job_is_declared_in_a_static() {
111 assert_eq!(CLEANUP.name(), "cleanup");
112 assert_eq!(CLEANUP.path(), "/internal/cleanup");
113 assert_eq!(CLEANUP.schedule(), Schedule::Cron("0 4 * * *"));
114 assert_eq!(CLEANUP.title, Some("Nightly cleanup"));
115
116 assert_eq!(REFRESH.schedule(), Schedule::Every("30m"));
117 assert_eq!(REFRESH.title, None);
118 }
119}