{"id":124653,"date":"2020-12-02T17:00:16","date_gmt":"2020-12-02T14:00:16","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/using-cron-jobs-in-cloudflare-workers-through-wrangler-cloudsavvy-it\/"},"modified":"2020-12-02T17:00:16","modified_gmt":"2020-12-02T14:00:16","slug":"using-cron-jobs-in-cloudflare-workers-through-wrangler-cloudsavvy-it","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/using-cron-jobs-in-cloudflare-workers-through-wrangler-cloudsavvy-it\/","title":{"rendered":"#Using CRON Jobs in CloudFlare Workers through Wrangler \u2013 CloudSavvy IT"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_84 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a271fa512042\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a271fa512042\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/using-cron-jobs-in-cloudflare-workers-through-wrangler-cloudsavvy-it\/#Getting_Started_with_Wrangler\" >Getting Started with Wrangler<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/using-cron-jobs-in-cloudflare-workers-through-wrangler-cloudsavvy-it\/#Installing_the_Wrangler_Command-Line_Tool\" >Installing the Wrangler Command-Line Tool<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/using-cron-jobs-in-cloudflare-workers-through-wrangler-cloudsavvy-it\/#Wrangler_Login_and_Site_Creation\" >Wrangler Login and Site Creation<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/buradabiliyorum.com\/en\/using-cron-jobs-in-cloudflare-workers-through-wrangler-cloudsavvy-it\/#Creating_a_Cloudflare_Worker_Script_to_Query_WordPress_CRON\" >Creating a Cloudflare Worker Script to Query WordPress CRON<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/buradabiliyorum.com\/en\/using-cron-jobs-in-cloudflare-workers-through-wrangler-cloudsavvy-it\/#Creating_the_CRON_Trigger_Configuration\" >Creating the CRON Trigger Configuration<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/buradabiliyorum.com\/en\/using-cron-jobs-in-cloudflare-workers-through-wrangler-cloudsavvy-it\/#Deploying_the_WordPress_CRON_Script_and_Scheduling_the_CRON_Job\" >Deploying the WordPress CRON Script and Scheduling the CRON Job<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/buradabiliyorum.com\/en\/using-cron-jobs-in-cloudflare-workers-through-wrangler-cloudsavvy-it\/#Conclusion\" >Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#Using CRON Jobs in CloudFlare Workers through Wrangler \u2013 CloudSavvy IT&#8221;<\/strong><\/p>\n<div id=\"article-content-area\">\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-794\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/2f4a6c0c6ebd9c856345c1bdcc083835\/p\/uploads\/2019\/06\/be8df346.png\" alt=\"\" width=\"700\" height=\"300\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Cloudflare Workers have shown themselves to be a unique and powerful solution to serverless computing. With the new addition of <a rel=\"nofollow noopener noreferrer\" target=\"_blank\" href=\"https:\/\/developers.cloudflare.com\/workers\/platform\/cron-triggers\">CRON jobs<\/a>, 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 <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">script<\/a>, deploy that script to Cloudflare, and ultimately trigger that script with a CRON job.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Getting_Started_with_Wrangler\"><\/span>Getting Started with Wrangler<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Before we can deploy a new script to Cloudflare, we need to get the Wrangler command-line tool installed and configured to deploy our new worker script.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Installing_the_Wrangler_Command-Line_Tool\"><\/span>Installing the Wrangler Command-Line Tool<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>To create a site deployment, Cloudflare has created the <a rel=\"nofollow noopener noreferrer\" target=\"_blank\" href=\"https:\/\/github.com\/cloudflare\/wrangler\">Wrangler command-line tool<\/a>. Wrangler requires installation via either <a rel=\"nofollow noopener noreferrer\" target=\"_blank\" href=\"https:\/\/www.npmjs.com\/get-npm\">NPM (NodeJS package manager)<\/a> or <a rel=\"nofollow noopener noreferrer\" target=\"_blank\" href=\"https:\/\/doc.rust-lang.org\/cargo\/getting-started\/installation.html\">Cargo (Rust language package manager)<\/a>.<\/p>\n<p><strong>NPM Installation<\/strong><\/p>\n<pre><code class=\"language-powershell\">npm i @cloudflare\/wrangler -g&#13;\n<\/code><\/pre>\n<p><strong>Cargo<\/strong><\/p>\n<pre><code class=\"language-powershell\">cargo install wrangler&#13;\n<\/code><\/pre>\n<blockquote><p>\u00a0<\/p>\n<\/blockquote>\n<p><code>cargo install wrangler --features sys-openssl<\/code><\/p>\n<h3><span class=\"ez-toc-section\" id=\"Wrangler_Login_and_Site_Creation\"><\/span>Wrangler Login and Site Creation<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>First, we are going to login to Cloudflare using Wrangler. To do this we use the new <code>wrangler login<\/code> feature. This will prompt for a webpage to open, and if you are already authenticated in Cloudflare, the authentication will happen very quickly. Otherwise, log in and Wrangler will configure the API token automatically.<\/p>\n<pre><code class=\"language-powershell\">wrangler login&#13;\n<\/code><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-8203 aligncenter\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/9cf688a027cde5d14f916535d1e2c88a\/p\/uploads\/2020\/11\/9fdbb528-1.png\" alt=\"\" width=\"1087\" height=\"169\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-8204 aligncenter\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/c0c85e6f010f098b66daff20bf8df0a0\/p\/uploads\/2020\/11\/7db49749.png\" alt=\"\" width=\"1158\" height=\"318\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>\u00a0<\/p>\n<p>As you can see from the configuration location, there is a <code>.toml<\/code> file that now contains your API token to make all subsequent operations seamless. This file is contained in the user\u2019s home directory and stored in the sub-directory <code>.wrangler\\config\\default.toml<\/code>.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Creating_a_Cloudflare_Worker_Script_to_Query_WordPress_CRON\"><\/span>Creating a Cloudflare Worker Script to Query WordPress CRON<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>In this article, we are going to create a simple Cloudflare Worker that will merely query the <code>wp-cron.php<\/code> file on a schedule. The way that WordPress works is that on a page request it will attempt to run any internal CRON jobs that are defined. This means that jobs are run only on occasion with a low volume site, and 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 externally trigger the WordPress CRON system.<\/p>\n<p>We need to disable the default WordPress CRON setup by using the following configuration in <code>wp-config.php<\/code>. This takes effect the moment that the file is saved.<\/p>\n<pre><code class=\"language-php\">define('DISABLE_WP_CRON', true);&#13;\n<\/code><\/pre>\n<p>Typically, worker scripts are only triggered when they are called. In this case, we will be scheduling a CRON trigger to run this every five minutes.<\/p>\n<p>Cloudflare Workers scheduled by CRON triggers will run on underutilized machines to make the most efficient use of resources.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Creating_the_CRON_Trigger_Configuration\"><\/span>Creating the CRON Trigger Configuration<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><a rel=\"nofollow noopener noreferrer\" target=\"_blank\" href=\"https:\/\/developers.cloudflare.com\/workers\/cli-wrangler\/configuration#triggers\">Cloudflare Worker CRON triggers<\/a> use the familiar Unix CRON syntax. This makes it im<a href=\"https:\/\/buradabiliyorum.com\/en\/category\/social-mediaa\/\" data-internallinksmanager029f6b8e52c=\"1\" title=\"Social Media\" target=\"_blank\" rel=\"noopener\">media<\/a>tely familiar and with a wealth of learning resources behind it. You can even define multiple CRON triggers for a single worker script, up to a current limit of three. The configuration syntax looks like the below code that shows a trigger running every 5th minute.<\/p>\n<pre><code class=\"language-bash\">[triggers]crons = [\"*\/5 * * * *\"]&#13;\n<\/code><\/pre>\n<p>Now that we have our trigger configuration how do we put everything together to deploy our script to the Cloudflare environment?<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Deploying_the_WordPress_CRON_Script_and_Scheduling_the_CRON_Job\"><\/span>Deploying the WordPress CRON Script and Scheduling the CRON Job<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>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 directory named, <code>wordpress-cron<\/code> that contains everything needed to deploy a javascript based worker.<\/p>\n<pre><code class=\"language-bash\"># Generate simple javascript Cloudflare Worker scaffold&#13;\nwrangler generate wordpress-cron&#13;\n<\/code><\/pre>\n<p>Next, modify the <code>index.js<\/code> file that is generated. Within the script, we will simply call the <code>wp-cron.php<\/code> file with the parameter of <code>doing_wp_cron<\/code>. When this is called, any CRON jobs that need to be run will be.<\/p>\n<pre><code class=\"language-bash\">addEventListener(\"fetch\", event =&gt; {&#13;\n  return event.respondWith(&#13;\n    fetch(\"&lt;https:\/\/mysite.com\/wp-cron.php?doing_wp_cron&gt;\")&#13;\n  )&#13;\n})&#13;\n<\/code><\/pre>\n<p>Finally, you will need to update your <code>wrangler.toml<\/code> file to include the necessary information. Update the configuration file to include your <code>acccount_id<\/code> and <code>zone_id<\/code>. To get these values, locate the main dashboard of your site and scroll down to find the API section. Both the account ID and zone ID will be located there.<\/p>\n<pre><code class=\"language-bash\">name = \"wordpress-cron\"&#13;\ntype = \"javascript\"&#13;\naccount_id = \"account_id\"&#13;\nworkers_dev = false&#13;\nroute = \"*mysite.com\/wordpress-cron*\"&#13;\nzone_id = \"zone_id\"&#13;\n[triggers]&#13;\ncrons = [\"*\/5 * * * *\"]&#13;\n<\/code><\/pre>\n<p>This route will match any protocol, <code>http<\/code> or <code>https<\/code>, and any additional parameters or paths beyond the <code>wordpress-cron<\/code> section. Feel free to make this whatever you would like. In reality, this allows you to manually trigger the script, but the CRON trigger will handle it most of the time.<\/p>\n<p>Finally, we need to deploy this worker, which you can do so using the publish command as shown below.<\/p>\n<pre><code class=\"language-bash\">wrangler publish&#13;\n<\/code><\/pre>\n<p>Once the script has been deployed and had its first run, you can see statistics and information on the Cloudflare Workers dashboard page.<\/p>\n<p>\u00a0<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-8205 aligncenter\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/ceabb0844a7e0ade26d641ee79754091\/p\/uploads\/2020\/11\/bc3dd1d3.png\" alt=\"\" width=\"1124\" height=\"769\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>This simple example of using CRON triggers with Cloudflare Workers is merely scratching the surface of the possibilities. Start exploring Cloudflare Workers and how this serverless <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/technology\/\" data-internallinksmanager029f6b8e52c=\"4\" title=\"Technology\" target=\"_blank\" rel=\"noopener\">technology<\/a> can fit into your workflow today!\n<\/p><\/div>\n<blockquote><p><strong><span style=\"color: #ff6600;\">If you liked the article, do not forget to share it with your friends. Follow us on\u00a0<span style=\"color: #ff0000;\"><a style=\"color: #ff0000;\" href=\"https:\/\/news.google.com\/publications\/CAAqBwgKMLG0nwswvr63Aw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Google News<\/a><\/span>\u00a0too, click on the star and choose us from your favorites.<\/span><\/strong><\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener noreferrer\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong>\n<\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener noreferrer\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.cloudsavvyit.com\/8201\/using-cron-jobs-in-cloudflare-workers-through-wrangler\/\" target=\"_blank\" rel=\"noopener noreferrer\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#Using CRON Jobs in CloudFlare Workers through Wrangler \u2013 CloudSavvy IT&#8221; Cloudflare Workers have shown themselves to be a unique and powerful solution to serverless computing. With the new addition of CRON jobs, you can now run Cloudflare Workers on a schedule without relying on an external trigger. In this article, we explore how to&#8230;<\/p>\n","protected":false},"author":1,"featured_media":124654,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2019\/06\/be8df346.png","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-124653","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/124653","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=124653"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/124653\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/124654"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=124653"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=124653"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=124653"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}