1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
//! 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));
//! ```
//!
//! # Each run
//!
//! The request carries an idempotency key in [`IDEMPOTENCY_KEY_HEADER`]: an
//! id for the run that is the same each time Encore sends it, as when it
//! sends it again after a failure. A job that must not do its work twice can
//! check whether it has seen the key before:
//!
//! ```ignore
//! async fn cleanup(headers: HeaderMap) -> StatusCode {
//! let key = headers.get(encore::cron::IDEMPOTENCY_KEY_HEADER);
//! // ...
//! }
//! ```
//!
//! [`TRIGGER_HEADER`] says why the job runs. Only Encore sends either: a
//! gateway removes them from the requests it takes from clients.
//!
//! # Running locally and under test
//!
//! A job is scheduled where the app is deployed, not on a developer's
//! machine, unless [`Job::run_in_local`] says otherwise:
//!
//! ```ignore
//! static REFRESH: Job = Job::new("refresh-rates", "/internal/rates", Schedule::Every("30m"))
//! .run_in_local(true);
//! ```
//!
//! Locally, it runs on schedule while the app has a session open, as `encore
//! run` keeps one, and a developer can turn that on or off for their checkout
//! whatever the declaration says. Nothing is scheduled by `encore test`.
//! `encore cron trigger <name>` runs a job now, as Encore runs it when it is
//! due, with both headers. Or request its path, or call what serves it.
/// The header with a run's idempotency key: an id that is the same each
/// time Encore sends the run. See the [module documentation](self).
pub const IDEMPOTENCY_KEY_HEADER: &str = "x-encore-cron-execution";
/// The header that says why a job runs: `scheduled`, when it was due, or
/// `manual`, when someone asked for it to run now.
pub const TRIGGER_HEADER: &str = "x-encore-cron-trigger";
/// When a [`Job`] runs.
///
/// Written out where the job is declared, with a string literal: Encore reads
/// it from the source.
/// 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.