Utilizing CRON Jobs in CloudFlare Staff via Wrangler

Utilizing CRON Jobs in CloudFlare Staff via Wrangler

Cloudflare Staff have proven themselves to be a singular and highly effective resolution to serverless computing. With the brand new addition of CRON jobs, now you can run Cloudflare Staff on a schedule with out counting on an exterior set off. On this article, we discover the best way to arrange a brand new script, deploy that script to Cloudflare, and in the end set off that script with a CRON job.

Getting Began with Wrangler

Earlier than we will deploy a brand new script to Cloudflare, we have to get the Wrangler command-line instrument put in and configured to deploy our new employee script.

Putting in the Wrangler Command-Line Instrument

To create a web site deployment, Cloudflare has created the Wrangler command-line tool. Wrangler requires set up by way of both NPM (NodeJS package manager) or Cargo (Rust language package manager).

NPM Set up

npm i @cloudflare/wrangler -g


cargo set up wrangler


cargo set up wrangler --features sys-openssl

Wrangler Login and Website Creation

First, we’re going to login to Cloudflare utilizing Wrangler. To do that we use the brand new wrangler login function. This can immediate for a webpage to open, and in case you are already authenticated in Cloudflare, the authentication will occur in a short time. In any other case, log in and Wrangler will configure the API token robotically.

wrangler login


As you may see from the configuration location, there’s a .toml file that now accommodates your API token to make all subsequent operations seamless. This file is contained within the person’s dwelling listing and saved within the sub-directory .wranglerconfigdefault.toml.

Making a Cloudflare Employee Script to Question WordPress CRON

On this article, we’re going to create a easy Cloudflare Employee that may merely question the wp-cron.php file on a schedule. The way in which that WordPress works is that on a web page request it should try to run any inner CRON jobs which are outlined. Which means jobs are run solely occasionally with a low quantity web site, and could be detrimental to a high-volume web site. This will likely not at all times be possible on some internet hosting, so you may outline the identical setting however externally set off the WordPress CRON system.

We have to disable the default WordPress CRON setup by utilizing the next configuration in wp-config.php. This takes impact the second that the file is saved.

outline('DISABLE_WP_CRON', true);

Sometimes, employee scripts are solely triggered when they’re known as. On this case, we will likely be scheduling a CRON set off to run this each 5 minutes.

Cloudflare Staff scheduled by CRON triggers will run on underutilized machines to take advantage of environment friendly use of sources.

Creating the CRON Set off Configuration

Cloudflare Worker CRON triggers use the acquainted Unix CRON syntax. This makes it instantly acquainted and with a wealth of studying sources behind it. You may even outline a number of CRON triggers for a single employee script, as much as a present restrict of three. The configuration syntax appears to be like just like the under code that exhibits a set off operating each fifth minute.

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

Now that we have now our set off configuration how will we put every thing collectively to deploy our script to the Cloudflare atmosphere?

Deploying the WordPress CRON Script and Scheduling the CRON Job

To create all the required information and arrange a easy javascript employee, we will use the built-in generate command that wrangler has. This can create a listing named, wordpress-cron that accommodates every thing wanted to deploy a javascript primarily based employee.

# Generate easy javascript Cloudflare Employee scaffold
wrangler generate wordpress-cron

Subsequent, modify the index.js file that’s generated. Throughout the script, we’ll merely name the wp-cron.php file with the parameter of doing_wp_cron. When that is known as, any CRON jobs that must be run will likely be.

addEventListener("fetch", occasion => {
  return occasion.respondWith(

Lastly, you will want to replace your wrangler.toml file to incorporate the required info. Replace the configuration file to incorporate your acccount_id and zone_id. To get these values, find the primary dashboard of your web site and scroll down to search out the API part. Each the account ID and zone ID will likely be positioned there.

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

This route will match any protocol, http or https, and any extra parameters or paths past the wordpress-cron part. Be happy to make this no matter you want to. In actuality, this lets you manually set off the script, however the CRON set off will deal with it more often than not.

Lastly, we have to deploy this employee, which you are able to do so utilizing the publish command as proven under.

wrangler publish

As soon as the script has been deployed and had its first run, you may see statistics and data on the Cloudflare Staff dashboard web page.



This straightforward instance of utilizing CRON triggers with Cloudflare Staff is merely scratching the floor of the chances. Begin exploring Cloudflare Staff and the way this serverless expertise can match into your workflow as we speak!

Source link