Cloudflare Docs
Workers
Visit Workers on GitHub
Set theme to dark (⇧+D)

Setting Cron Triggers

Set a Cron Trigger for your Worker.
export default {
async scheduled(controller, env, ctx) {
console.log('cron processed');
},
};
addEventListener("scheduled", (event) => {
event.waitUntil(triggerEvent(event.scheduledTime));
});
async function triggerEvent(scheduledTime) {
// Fetch some data
// Update API
console.log("cron processed");
}

​​ Test Cron Triggers using Wrangler

The recommended way of testing Cron Triggers is using Wrangler.

Cron Triggers can be tested using Wrangler by passing in the --test-scheduled flag to wrangler dev. This will expose a /__scheduled route which can be used to test using a HTTP request. To simulate different cron patterns, a cron query parameter can be passed in.

$ wrangler dev --test-scheduled
$ curl "http://localhost:8787/__scheduled?cron=*+*+*+*+*"

​​ Setting Cron Triggers in Wrangler

If you are deploying with Wrangler, set the cron syntax (once per hour as shown below) by adding this to your wrangler.toml file:

name = "worker"
# ...
[triggers]
crons = ["0 * * * *"]

You also can set a different Cron Trigger for each environment in your wrangler.toml. You need to put the [triggers] table under your chosen environment. For example:

[env.dev.triggers]
crons = ["0 * * * *"]