nitro icon indicating copy to clipboard operation
nitro copied to clipboard

Add the ability to use `ScheduledEvent` from Cloudflare

Open Barbapapazes opened this issue 2 years ago • 10 comments

Describe the feature

Hello,

I would like to know how I could use Scheduled Event, https://developers.cloudflare.com/workers/runtime-apis/scheduled-event, directly from Nitro.

Additional information

  • [ ] Would you be willing to help implement this feature?

Barbapapazes avatar Aug 14 '23 13:08 Barbapapazes

It is a good idea. I think we might support feature via a custom cloudflare:scheduledEvent event triggered from default handler and can be hooked with nitro plugins to unblock feature 👍🏼

For general feature in nitro, for a while i had been thinking to introduce Tasks and Cron job features. This way we can universally support events/cronjobs and integrate it with each platform and also easily test them locally.

pi0 avatar Aug 14 '23 13:08 pi0

Describe the feature

Hello,

I would like to know how I could use Scheduled Event, developers.cloudflare.com/workers/runtime-apis/scheduled-event, directly from Nitro.

Additional information

  • [ ] Would you be willing to help implement this feature?

AFAIK here's a workaround with custom nitro presets : https://nitro.unjs.io/deploy/custom-presets Add the scheduled function there and it should work

Hebilicious avatar Aug 14 '23 14:08 Hebilicious

@Hebilicious I wouldn't advice that.

pi0 avatar Aug 14 '23 15:08 pi0

@pi0 Do you mind explaining what is wrong with a custom preset approach ? There's other cloudflare features that also need to be accessed that way, like queues for example https://developers.cloudflare.com/queues/get-started/#5-create-your-consumer-worker

Hebilicious avatar Aug 14 '23 16:08 Hebilicious

It is a good idea. I think we might support feature via a custom cloudflare:scheduledEvent event triggered from default handler and can be hooked with nitro plugins to unblock feature 👍🏼

Not sure to fully understand. 🤔

Barbapapazes avatar Aug 14 '23 16:08 Barbapapazes

Describe the feature

Hello, I would like to know how I could use Scheduled Event, developers.cloudflare.com/workers/runtime-apis/scheduled-event, directly from Nitro.

Additional information

  • [ ] Would you be willing to help implement this feature?

AFAIK here's a workaround with custom nitro presets : nitro.unjs.io/deploy/custom-presets Add the scheduled function there and it should work

And how to support both a fetch and a ScheduledEvent? If I overwrite the entry, the fetch will not be generated.

Barbapapazes avatar Aug 14 '23 16:08 Barbapapazes

@Hebilicious Nitro is a "Platform Agnostic" and "Zero Config" framework to build web servers. The whole topic of this issue is to make API available not to workaround and use a custom preset! Even introducing hooks is a workaround I am proposing until making the platform agnostic feature.

@Barbapapazes We need to register both fetch and new scheduled event handlers from main cloudflare entrypoints (just an example below)

addEventListener('scheduled', event => {
  event.waitUntil(nitroApp.hooks.callHook('cloudflare:scheduled', event);
});

pi0 avatar Aug 14 '23 16:08 pi0

Describe the feature

Hello, I would like to know how I could use Scheduled Event, developers.cloudflare.com/workers/runtime-apis/scheduled-event, directly from Nitro.

Additional information

  • [ ] Would you be willing to help implement this feature?

AFAIK here's a workaround with custom nitro presets : nitro.unjs.io/deploy/custom-presets Add the scheduled function there and it should work

And how to support both a fetch and a ScheduledEvent? If I overwrite the entry, the fetch will not be generated.

You should be able to place all Cloudflare specific exports (queue, scheduled, fetch) on the same worker.

@pi0 First class support would be great. There's definitely room for handling scheduled functions (or queues) agnosticly, perhaps we could even use an API close to the Cloudflare one ?

Recommending against using a custom preset means you can't use nitro to deploy a worker with these features.

That being said if you're happy with the hook solution, I can open a PR.

Hebilicious avatar Aug 14 '23 16:08 Hebilicious

addEventListener('scheduled', event => {
  event.waitUntil(nitroApp.hooks.callHook('cloudflare:scheduled', event);
});

@Hebilicious Nitro is a "Platform Agnostic" and "Zero Config" framework to build web servers. The whole topic of this issue is to make API available not to workaround and use a custom preset! Even introducing hooks is a workaround I am proposing until making the platform agnostic feature.

@Barbapapazes We need to register both fetch and new scheduled event handlers from main cloudflare entrypoints (just an example below)

addEventListener('scheduled', event => {
  event.waitUntil(nitroApp.hooks.callHook('cloudflare:scheduled', event);
});

Okkk, I have something.

image

I didn't understand that it was possible to add this in the middle of a file. And it's possible because it's all event based. So many things to learn on Cloudflare.

Barbapapazes avatar Aug 14 '23 17:08 Barbapapazes

I'd like to know exactly how this feature is going. There was a situation where I needed to access the queue handler and email handler of cloudflare workers.

dalbodeule avatar May 28 '24 06:05 dalbodeule