Automation

Setting Up Cloudflare Workers Cron Triggers

Learn how to utilize Cloudflare Workers with cron triggers to perform scheduled tasks effectively.

3 min read

Learn how to set up Cloudflare Workers with cron triggers to automate scheduled tasks without needing a dedicated server. This guide will walk you through each step, from prerequisites to deploying your Worker efficiently.

Prerequisites for Setting Up Cloudflare Workers Cron Triggers

Before you begin, make sure you have the following:

prerequisites

  • A Cloudflare account.
  • Wrangler CLI installed (Cloudflare’s command-line tool for managing Workers).
  • Basic knowledge of JavaScript and Cloudflare Workers concepts.

Creating a Cloudflare Worker with Scheduled Events

Follow these steps to create a Cloudflare Worker that triggers tasks on a schedule.

steps

  1. Install Wrangler CLI
    Make sure you have Wrangler installed on your system. You can install it using npm:

    bash
    npm install -g wrangler
  2. Generate a new Worker
    Run the following command to generate a new Worker project:

    bash
    wrangler generate my-worker
    cd my-worker
  3. Add the scheduled event listener
    Open the index.js file in your project directory and add code to handle scheduled events:

    javascript
    export default {
      async scheduled(event) {
        event.waitUntil(handleScheduled(event));
      }
    };
    
    async function handleScheduled(event) {
      // Example task: Log a message
      console.log("Cron job executed!");
    }

    This function will perform the desired tasks when triggered.

Configuring Cron Triggers in Wrangler

Set up cron triggers to define when the Worker should execute.

steps

  1. Update wrangler.toml
    Open the wrangler.toml file in your project. Add the triggers section with a crons array to specify your cron schedule:

    [[triggers]]
    crons = ["0 * * * *"]  # Runs every hour at minute 0
  2. Understand cron syntax
    Use crontab.guru to test and generate valid cron expressions. For example:

    • "0 * * * *": Every hour on the hour.
    • "0 12 * * *": At noon every day.
  3. Save and close your configuration
    Ensure the wrangler.toml file is saved with the proper syntax.

Deploying the Worker with Cron Triggers

Deploy your Worker so it can start running based on the configured cron schedule.

Deploying with Wrangler

wrangler publish
# Output:
# -- Uploading and deploying Worker your-worker-name
# -- Success: Published with the following Cron Triggers:
# -- 0 * * * * (hourly)

Verify the deployment to ensure the Worker has been published and the cron triggers are configured correctly.

Understanding Common Mistakes with Scheduled Workers

When configuring cron triggers, be mindful of the following issues:

Taking Your Scheduled Worker to Production

After development, follow these practices for a production-ready setup:


FAQ

Can I use Cloudflare Workers Cron Triggers for tasks requiring seconds-level granularity?

No, Cloudflare Workers Cron Triggers support a minimum granularity of one minute.

How do I adjust my cron triggers for my local timezone?

Cron triggers run based on UTC. Use a timezone conversion tool to map your local time to UTC before updating the wrangler.toml file.

How do I debug errors in my scheduled Worker?

You can use the Wrangler CLI's logging feature (wrangler tail) to monitor real-time logs and identify issues.

Can cron triggers run multiple tasks on varying schedules?

Yes, you can define multiple cron expressions in the crons array in wrangler.toml for different schedules.


Official reference: Cloudflare Workers cron triggers.