Cron Trigger every 10 seconds
does workers platform support cron trigger at the seconds interval? Trying to set a scheduled trigger every 10 seconds
7 Replies
Cloudflare Docs
Cron Triggers · Cloudflare Workers docs
Enable your Worker to be executed on a schedule.
To create a "Hello World" cron worker using Cloudflare Workers, follow these steps:
1. Set Up Your Worker Project
First, install the
create-cloudflare
package to scaffold a new Worker project:
During setup, select the "Hello World example" and choose "JavaScript" as your language. citeturn0search3
2. Add a Scheduled Event Listener
In your project directory, navigate to the src
folder and open index.js
. Modify it to include a scheduled
event listener:
This code logs "Hello, World!" each time the scheduled event triggers. citeturn0search2
3. Configure the Cron Trigger
Define when your Worker should run by setting up a cron trigger. In your wrangler.toml
file, add the following configuration:
This schedule runs the Worker at the top of every hour. citeturn0search0
4. Deploy Your Worker
To deploy your Worker with the cron trigger, use Wrangler:
Ensure you're authenticated with your Cloudflare account before deploying. citeturn0search3
5. Testing the Cron Trigger
While cron triggers run based on the defined schedule, you can test them locally:
This command starts a local server and exposes a /__scheduled
route. You can simulate the cron trigger by making an HTTP request:
This simulates a cron pattern matching every minute. citeturn0search2
By following these steps, you've set up a Cloudflare Worker that logs "Hello, World!" based on a scheduled cron trigger.
🤗 I prefer Wrangler CLI together with IntelliJ IDEA as development environment for Workers.this seems like AI?
The minimum is one minute. For anything shorter then that, can use Durable Object Alarms. Workers Cron triggers also float a bit, aren't guaranteed to be exactly one minute
The random
citeturn0search3
dotted throughout definitely makes it feel like it, plus it being a very generic example of a cron trigger every hour instead of answering the actual Q about a trigger every 10s :think:thank you. i'll go with that. Do you know if you can use D1 with Durable Objects?
You can query D1 from DOs yea. Durable Objects are a bit special and are worth reading into a bit first to better use them. I'd make sure you give it a location hint matching the same region as your D1 DB if you're going to be querying it a lot
https://developers.cloudflare.com/durable-objects/what-are-durable-objects/
Durable Objects also support having Sqlite/"D1" storage within themselves, it's beta and lower limits: https://developers.cloudflare.com/durable-objects/best-practices/access-durable-objects-storage/#sqlite-storage-backend
Depends what you need
I'll keep the current D1 setup I have. Just wondering how I can debug DO with
alarm()
locally atm. Do you know if logs inside alarm()
show up in wrangler dev console?
works. Appears that I had an outdated alarm and didn't refresh it
switching to workflows