performance – Thoughts On Disabling WP-CRON – Uptime Monitor Heartbeats

Question

TL;DR; Is disabling WP-CRON best all round?

I was looking for an uptime monitor solution over the weekend and eventually settled on Better Uptime. My first implementation used the standard web request monitor, checking for a HTTP200 status code response, but then remembered that WordPress can be broken but still present a page e.g. ‘Database connection failed’, which would imply a HTTP200 response so instead I looked into CRON heartbeats.

In the rabbit hole that followed, I discovered:

  1. A CRON heartbeat is better than a simple web request because it confirms the WordPress application is functioning, more so than a simple web request.
  2. The heartbeat itself is simply a GET request to a specified monitor URL.
  3. The WordPress WP-CRON system is not a ‘true CRON’ in that it only runs when a page request is processed; no page requests, no CRON, no heartbeat.
  4. My webhosting provider has a ‘proper CRON’ system but using that to send the heartbeat only checks function of my webhosting provider’s CRON system, not my WordPress application.
  5. Creating a WP-CRON action to send the heartbeat, then initiating that WP-CRON action from my webhosting provider’s CRON system seems to be the best solution; heartbeat schedule is guaranteed (unless there’s a failure) irrespective of page requests.
  6. If I am initiating one WP-CRON action regularly from my webhosting provider’s CRON system, I might as well run them all.
  7. If I’m initiating WP-CRON from my webhosting provider’s CRON system, there is no point running WP-CRON because that’s twice the server load for no additional benefit.
  8. If I disable WP-CRON then I save on the processing of WP-CRON tasks on every page load, improving WordPress performance.

In the end, I have a reliable heartbeat sent irrespective of page requests, WP-CRON is initiated reliably, again irrespective of page requests, meaning that, for example, backup jobs always run on time, and WordPress performance has been improved. What, no downside?!

So, is disabling WP-CRON best all round?

How To Setup A Heartbeat Uptime Monitor

  • STEP 1: Setup the monitor.
    Browse to Better Uptime, sign-up (free), create heartbeat monitor, note heartbeat URL for STEP 3.

  • STEP 2: Disable WP-CRON.
    Add following to wp_config.php:

    define('DISABLE_WP_CRON', true);

  • STEP 3: Create heartbeat.
    Add following to functions.php or Code Snippets:

function my_better_uptime_heartbeat() {
    wp_remote_get( '<heartbeat URL>' );
}
add_action( 'my_heartbeat', 'my_better_uptime_heartbeat' );
  • STEP 4: Create WP-CRON action.
    This can be done programmatically, via WP-CLI or using John Blackbourne’s WP Control plugin, I used the latter:

    Open CRON Schedule > Add Cron Schedule >

    • Internal Name: twicehourly
    • Interval: 1800
    • Display Name: Twice Hourly

    Open CRON Events > Add New >

    • Event Type: Standard cron event
    • Hook Name: my_heartbeat
    • Arguments:
    • Next Run: Now
    • Recurrence: Twice Hourly
  • STEP 5: Create hosting provider CRON task.
    This can be done programmatically using crontab via SSH or via your hosting provider’s ‘control panel’, I used the latter:

    System CRON > Add New:

    • Command: cd /<path_to_wordpress>/; wp cron event run --due-now >/dev/null 2>&1
    • Interval: 0,30 * * * * (every thirty minutes)

That’s it!

0
David Adams 2 months 2022-12-05T07:40:09-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse