Expand description
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:
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:
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:
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:
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.
Structs§
- Job
- A declared cron job. See the module documentation.
Enums§
Constants§
- IDEMPOTENCY_
KEY_ HEADER - Header containing the idempotency key for a cron execution.
- TRIGGER_
HEADER - Header indicating why a cron job ran:
"scheduled"for a scheduled run, or"manual"when triggered on demand.