//nitro-cloudflare-cronsbyatinux

nitro-cloudflare-crons

Example of using Cloudflare Workers Crons Triggers with Nitro.

32
2
32
TypeScript

Using Cloudflare Crons Triggers with Nitro ⏲️

This is an example of using Nitro scheduled tasks with Cloudflare Workers Crons Triggers.

Setup

Install dependencies

pnpm i

Development

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.

Example

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 = ["* * * * *"]

Deploy

Build your Nitro API:

pnpm build

Deploy with Wrangler CLI:

npx wrangler deploy

License

MIT

[beta]v0.14.0