Example of using Cloudflare Workers Crons Triggers with Nitro.
This is an example of using Nitro scheduled tasks with Cloudflare Workers Crons Triggers.
Install dependencies
pnpm i
Start the dev server
pnpm dev
You can specify the scheduled tasks in development mode in nitro.config.ts.
For production, make sure to add the same cron patterns in wrangler.toml.
This example adds a my-task scheduled task that runs every minute.
// server/tasks/my-task.ts
export default defineTask({
meta: {
name: "my-task",
description: "A specific task",
},
run({ payload, context }) {
console.log("Running my task...");
// Example of calling an external API that don't support crons
// $fetch('https://my-nuxt-app.com/api/my-endpoint', {
// method: 'POST',
// headers: {
// Authorization: 'token MY_SECRET_TOKEN'
// }
// })
return { result: "Success" };
},
});
In the nitro.config.ts:
// nitro.config.ts
export default defineNitroConfig({
experimental: {
tasks: true
},
scheduledTasks: {
'* * * * *': ['my-task']
},
});
In the wrangler.toml:
[triggers]
crons = ["* * * * *"]
Build your Nitro API:
pnpm build
Deploy with Wrangler CLI:
npx wrangler deploy
We use cookies
We use cookies to analyze traffic and improve your experience. You can accept or reject analytics cookies.