قالب وردپرس درنا توس
Home / Tips and Tricks / Using CRON Jobs in CloudFlare Workers via Wrangler – CloudSavvy IT

Using CRON Jobs in CloudFlare Workers via Wrangler – CloudSavvy IT



Cloudflare Workers have proven to be a unique and powerful solution for serverless computing. With the new addition of CRON tasks, you can now run Cloudflare Workers on a schedule without relying on an external trigger. In this article, we explore how to set up a new script, deploy that script in Cloudflare, and eventually activate that script with a CRON task.

Get started with Wrangler

Before we can deploy a new script in Cloudflare, we need to install and configure the Wrangler command-line tool to implement our new worker script.

Install the Wrangler command line utility

To create a site deployment, Cloudflare created the Wrangler command line tool. Wrangler requires installation via either NPM (NodeJS package manager) or Cargo (Rust language package manager).

NPM installation

npm i @cloudflare/wrangler -g

Charge

cargo install wrangler

cargo install wrangler --features sys-openssl

Create Wrangler login and site

First, we are going to log into Cloudflare with Wrangler. For this we use the new wrangler login feature. This will prompt you to open a web page, and if you are already authenticated in Cloudflare, the authentication will be done very quickly. Otherwise, log in and Wrangler will automatically configure the API token.

wrangler login

As you can see from the configuration location, there is one .toml file that now contains your API token to make all subsequent operations seamless. This file is located in the user’s home directory and is stored in the subdirectory .wrangler\config\default.toml.

Create a Cloudflare Worker script to query WordPress CRON

In this article, we are going to create a simple Cloudflare Worker that just uses the wp-cron.php file according to a schedule. The way WordPress works is that on a page request, it will attempt to perform internal CRON tasks that have been defined. This means that tasks will only run intermittently with a low-volume site, and this can be detrimental to a high-volume site. This may not always be feasible on some hosting, so you can define the same setting but activate the WordPress CRON system externally.

We need to disable the default WordPress CRON installation by using the following configuration in wp-config.php. This takes effect when the file is saved.

define('DISABLE_WP_CRON', true);

Typically worker scripts are only triggered when called. In this case, we schedule a CRON trigger to run this every five minutes.

Cloudflare Workers scheduled by CRON triggers run on underutilized machines to use resources as efficiently as possible.

Create the CRON trigger configuration

Cloudflare Worker CRON triggers use the well-known Unix CRON syntax. This makes it instantly familiar and with a wealth of learning resources behind it. You can even define multiple CRON triggers for one worker script, up to a current limit of three. The configuration syntax looks like the code below showing a trigger every 5 minutes.

[triggers]crons = ["*/5 * * * *"]

Now that we have our trigger setup, how can we bring everything together to implement our script in the Cloudflare environment?

Deploy the WordPress CRON script and schedule the CRON job

To create all the necessary files and set up a simple javascript worker, we can use the built-in Generate command that Wrangler has. This will create a folder named, wordpress-cron that contains everything needed to implement a javascript-based worker.

# Generate simple javascript Cloudflare Worker scaffold
wrangler generate wordpress-cron

Then change it index.js file that is generated. Within the script we simply call the wp-cron.php file with the parameter doing_wp_cron. When called, it will run all CRON tasks.

addEventListener("fetch", event => {
  return event.respondWith(
    fetch("")
  )
})

Finally, you must use your wrangler.toml file to record the necessary information. Update the configuration file with your acccount_id and zone_id. To get these values, go to the main dashboard of your site and scroll down to find the API section. Both the account ID and the zone ID are there.

name = "wordpress-cron"
type = "javascript"
account_id = "account_id"
workers_dev = false
route = "*mysite.com/wordpress-cron*"
zone_id = "zone_id"
[triggers]
crons = ["*/5 * * * *"]

This route suits any protocol, http or https, and any additional parameters or paths outside of the wordpress-cron section. Feel free to make this whatever you want. In reality, this allows you to manually trigger the script, but the CRON trigger will usually handle it.

Finally, we need to implement this worker, which you can do with the publish command as shown below.

wrangler publish

After the script is deployed and run for the first time, you can view statistics and information on the Cloudflare Workers dashboard page.

Conclusion

This simple example of using CRON triggers with Cloudflare Workers is just scratching the surface of the possibilities. Explore Cloudflare Workers and learn how this serverless technology can fit into your workflow today!


Source link