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
[TBF] Kevin Ossen₿rück
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:
npm create cloudflare@latest my-cron-worker
npm create cloudflare@latest my-cron-worker
During setup, select the "Hello World example" and choose "JavaScript" as your language. citeturn0search3 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:
export default {
async scheduled(event, env, ctx) {
console.log("Hello, World!");
},
};
export default {
async scheduled(event, env, ctx) {
console.log("Hello, World!");
},
};
This code logs "Hello, World!" each time the scheduled event triggers. citeturn0search2 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:
name = "my-cron-worker"

[triggers]
crons = ["0 * * * *"]
name = "my-cron-worker"

[triggers]
crons = ["0 * * * *"]
This schedule runs the Worker at the top of every hour. citeturn0search0 4. Deploy Your Worker To deploy your Worker with the cron trigger, use Wrangler:
npx wrangler deploy
npx wrangler deploy
Ensure you're authenticated with your Cloudflare account before deploying. citeturn0search3 5. Testing the Cron Trigger While cron triggers run based on the defined schedule, you can test them locally:
npx wrangler dev --test-scheduled
npx wrangler dev --test-scheduled
This command starts a local server and exposes a /__scheduled route. You can simulate the cron trigger by making an HTTP request:
curl "http://localhost:8787/__scheduled?cron=*+*+*+*+*"
curl "http://localhost:8787/__scheduled?cron=*+*+*+*+*"
This simulates a cron pattern matching every minute. citeturn0search2 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.
Chaika
Chaika4d ago
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
MattIPv4
MattIPv44d ago
The random citeturn0search3 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:
amorfati
amorfatiOP4d ago
thank you. i'll go with that. Do you know if you can use D1 with Durable Objects?
Chaika
Chaika4d ago
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
amorfati
amorfatiOP3d ago
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

Did you find this page helpful?