Giter VIP home page Giter VIP logo

Comments (2)

universse avatar universse commented on July 22, 2024 2

hi, you can write something like this. i'm using version 0.8.0-next.13.

import { start } from 'worktop/cfw'
import type { Module } from 'worktop/cfw'

const worker: Module.Worker = start(API.run)

worker.scheduled = (e, env, ctx) => {
  ctx.waitUntil(triggerScheduledEvent(e, env, ctx))
}

export default worker

async function triggerScheduledEvent(e, env, ctx) {
  switch (e.cron) {
    // Up to three schedules.
    case `0 7,19 * * *`:
      break

    case `*/10 * * * *`:
      break

    case `*/45 * * * *`:
      break
  }
}

from worktop.

lukeed avatar lukeed commented on July 22, 2024 1

Yup, what @universse shared is correct if you want the same Worker to handle both trigger types.

There's also a define export from the worktop/cfw module, which provides type information while writing the fetch and/or scheduled handlers. There's no "start" equivalent that's meant to handle both triggers at once because – IMO – Worker Cron handlers should not coexist with public fetch code... I've seen too many times where developers accidentally leak out information and/or make themselves susceptible to public requests accidentally calling should-be-private functions. So scheduled and fetch should be split out into separate Workers, in which case, a scheduled Worker looks like this:

import { define } from 'worktop/cfw';

export default define({
  scheduled(event, bindings) {
    // i have access to type defs
  }
});

// or 

export default define<MyBindings>({
  scheduled(event, bindings) {
    // bindings === MyBindings
  }
});

Separately, I find Worker Crons exceptionally useless. They're not configurable – you can only dispatch GET requests (effectively) and there's no "custom payload" that you can include in the dispatched message. Every other Cron service allows you to schedule a POST request w/ arbitrary data for your handler to process (and this can be done programmatically)... not Cloudflare. Instead, most handlers need to "wake up" and then switch on the CRON pattern itself (lol wat) or wait on some blocking external request to figure out what it should be doing/handling. It's so silly. I advocated internally for this to get added to the roadmap & it was repeatedly a deadend.

Until that happens, most applications may be better off using a GCP scheduler that sends an authenticated HTTP request to your Worker(s)

from worktop.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.