UNPKG

create-cloudflare

Version:

A CLI for creating and deploying new applications to Cloudflare.

38 lines (34 loc) 1.49 kB
/** * Welcome to Cloudflare Workers! * * This is a template for a Scheduled Worker: a Worker that can run on a * configurable interval: * https://developers.cloudflare.com/workers/platform/triggers/cron-triggers/ * * - Run `npm run dev` in your terminal to start a development server * - Run `curl "http://localhost:8787/__scheduled?cron=*+*+*+*+*"` to see your worker in action * - Run `npm run deploy` to publish your worker * * Learn more at https://developers.cloudflare.com/workers/ */ export default { async fetch(req) { const url = new URL(req.url) url.pathname = "/__scheduled"; url.searchParams.append("cron", "* * * * *"); return new Response(`To test the scheduled handler, ensure you have used the "--test-scheduled" then try running "curl ${url.href}".`); }, // The scheduled handler is invoked at the interval set in our wrangler.jsonc's // [[triggers]] configuration. async scheduled(event, env, ctx) { // A Cron Trigger can make requests to other endpoints on the Internet, // publish to a Queue, query a D1 Database, and much more. // // We'll keep it simple and make an API call to a Cloudflare API: let resp = await fetch('https://api.cloudflare.com/client/v4/ips'); let wasSuccessful = resp.ok ? 'success' : 'fail'; // You could store this result in KV, write to a D1 Database, or publish to a Queue. // In this template, we'll just log the result: console.log(`trigger fired at ${event.cron}: ${wasSuccessful}`); }, };