unjs / nitro

Next Generation Server Toolkit. Create web servers with everything you need and deploy them wherever you prefer.
https://nitro.unjs.io
MIT License
5.84k stars 491 forks source link

Add the ability to use `ScheduledEvent` from Cloudflare #1571

Open Barbapapazes opened 1 year ago

Barbapapazes commented 1 year ago

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

pi0 commented 1 year ago

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.

Hebilicious commented 1 year ago

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

pi0 commented 1 year ago

@Hebilicious I wouldn't advice that.

Hebilicious commented 1 year ago

@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

Barbapapazes commented 1 year ago

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 commented 1 year ago

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.

pi0 commented 1 year ago

@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);
});
Hebilicious commented 1 year ago

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.

Barbapapazes commented 1 year ago
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.

dalbodeule commented 3 months ago

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.