cloudflare / workers-sdk

⛅️ Home to Wrangler, the CLI for Cloudflare Workers®
https://developers.cloudflare.com/workers/
Apache License 2.0
2.7k stars 709 forks source link

Support cron triggers using preview and dev #570

Closed Kynson closed 2 years ago

Kynson commented 3 years ago

đź’ˇ Feature request

Describe the feature

Currently, there is no way to test scheduled jobs, since we can't fire a scheduled during development. I suggest adding support of "cron triggers testing" for wrangler dev or wrangler preview, which speeds up the development and testing.

Possible methods of triggering a cron job during development:

Describe the alternatives

The current work around for this issue is to simply deploy the worker to production. Or add the code I want to test to the fetch event handler, which is really inconvenient:

addEventListener('scheduled', (event) => {
  event.waitUntil(scheduledJob(event))
});
addEventListener('fetch', (event) => {
  event.respondWith(scheduledJob(event))
})
const scheduledJob = (event) => {
  // Job
}

Thanks.

Electroid commented 3 years ago

Thanks for making this issue! This is definitely something we need to support.

In the interim, a simple workaround would be to call a mock scheduled event from a fetch handler. Not nice, but it can get the job done for you if you need it today.

Kynson commented 3 years ago

Cloudflare recently released miniflare, which supports cron trigger testing. If anyone wants to test cron triggers at the moment, you may refer this link https://miniflare.dev/scheduled.html

Of cause, cron trigger support for wrangler dev/wrangler preview would also be appreciated

threepointone commented 2 years ago

Copy pasting from https://github.com/cloudflare/wrangler2/pull/738#issuecomment-1084590138, where we're landing support for local mode soon.