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
208
209
210
211
//! Scheduled cron jobs that invoke an HTTP endpoint on your service.
//!
//! Declare a job as a module-level `static` with its target path and schedule:
//!
//! ```ignore
//! use encore::cron::{Job, Schedule};
//!
//! /// Removes sessions that have been inactive for 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 to the configured path on
//! the declaring service. Register your route using [`Job::path`] so the route
//! and cron declaration stay in sync:
//!
//! ```ignore
//! let app = Router::new().route(CLEANUP.path(), post(cleanup));
//! ```
//!
//! # Execution headers
//!
//! Each cron request includes an idempotency key in [`IDEMPOTENCY_KEY_HEADER`].
//! This ID remains the same across retries of the same scheduled run, so
//! handlers that must avoid duplicate work can deduplicate on it:
//!
//! ```ignore
//! async fn cleanup(headers: HeaderMap) -> StatusCode {
//! let key = headers.get(encore::cron::IDEMPOTENCY_KEY_HEADER);
//! // ...
//! }
//! ```
//!
//! [`TRIGGER_HEADER`] indicates whether the job was triggered on schedule or
//! manually. Gateways strip both headers from external client requests so only
//! Encore can set them.
//!
//! # Running locally and in tests
//!
//! By default, cron jobs only run on schedule in deployed environments. To run
//! a job on schedule during local development as well, enable
//! [`Job::run_in_local`]:
//!
//! ```ignore
//! static REFRESH: Job = Job::new("refresh-rates", "/internal/rates", Schedule::Every("30m"))
//! .run_in_local(true);
//! ```
//!
//! Locally, scheduled jobs run while an `encore run` session is active, and
//! developers can toggle local scheduling per checkout regardless of the
//! declaration. Cron jobs are not scheduled under `encore test`. Run a job
//! immediately with `encore cron trigger <name>` (which includes both headers),
//! or send a `POST` request directly to its handler path.
/// Header containing the idempotency key for a cron execution.
///
/// The value stays the same if Encore retries the same run. See the
/// [module documentation](self).
pub const IDEMPOTENCY_KEY_HEADER: &str = "x-encore-cron-execution";
/// Header indicating why a cron job ran: `"scheduled"` for a scheduled run, or
/// `"manual"` when triggered on demand.
pub const TRIGGER_HEADER: &str = "x-encore-cron-trigger";
/// Schedule defining when a [`Job`] runs.
///
/// Must be written inline with a string literal where the job is declared so
/// Encore can parse it statically.
/// A declared cron job. See the [module documentation](self).
///
/// Declare as a module-level `static` with an app-wide unique name using
/// lowercase letters, digits, and dashes.