From a56256d158651e2ceed99995ff1cbb31c98cbd61 Mon Sep 17 00:00:00 2001 From: Peter Wilson Date: Thu, 15 Aug 2019 00:35:55 +0000 Subject: [PATCH] Site Health Check: Increase time allowance for cron checks. Introduces `WP_Site_Health::has_late_cron()` for late wp-cron jobs and extends the time allowance before a job is considered missed. In a standard configuration using loopback requests, a job is considered late once past due and missed over five minutes past due. Late and missed time frames are extended if `DISABLE_WP_CRON` is defined as `true` to allow for crontab tasks running less frequently. A job is considered late once it's 15 minutes past due and missed over one hour past due. A file for site health unit tests has been introduced with tests for cron in critical, late and missed states. Props rockfire, afragen, peterwilsoncc. Fixes #47223. Built from https://develop.svn.wordpress.org/trunk@45801 git-svn-id: http://core.svn.wordpress.org/trunk@45612 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/includes/class-wp-site-health.php | 81 +++++++++++++++++----- wp-includes/version.php | 2 +- 2 files changed, 66 insertions(+), 17 deletions(-) diff --git a/wp-admin/includes/class-wp-site-health.php b/wp-admin/includes/class-wp-site-health.php index d8c097c93d..a42732d277 100644 --- a/wp-admin/includes/class-wp-site-health.php +++ b/wp-admin/includes/class-wp-site-health.php @@ -11,14 +11,17 @@ class WP_Site_Health { private $mysql_min_version_check; private $mysql_rec_version_check; - public $is_mariadb = false; + public $is_mariadb = false; private $mysql_server_version = ''; private $health_check_mysql_required_version = '5.5'; private $health_check_mysql_rec_version = ''; public $schedules; public $crons; - public $last_missed_cron = null; + public $last_missed_cron = null; + public $last_late_cron = null; + private $timeout_missed_cron = null; + private $timeout_late_cron = null; /** * WP_Site_Health constructor. @@ -28,6 +31,14 @@ class WP_Site_Health { public function __construct() { $this->prepare_sql_data(); + $this->timeout_late_cron = 0; + $this->timeout_missed_cron = - 5 * MINUTE_IN_SECONDS; + + if ( defined( 'DISABLE_WP_CRON' ) && DISABLE_WP_CRON ) { + $this->timeout_late_cron = - 15 * MINUTE_IN_SECONDS; + $this->timeout_missed_cron = - 1 * HOUR_IN_SECONDS; + } + add_filter( 'admin_body_class', array( $this, 'admin_body_class' ) ); add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); @@ -1416,21 +1427,32 @@ class WP_Site_Health { $this->has_missed_cron()->get_error_message() ) ); - } else { - if ( $this->has_missed_cron() ) { - $result['status'] = 'recommended'; + } elseif ( $this->has_missed_cron() ) { + $result['status'] = 'recommended'; - $result['label'] = __( 'A scheduled event has failed' ); + $result['label'] = __( 'A scheduled event has failed' ); - $result['description'] = sprintf( - '

%s

', - sprintf( - /* translators: %s: The name of the failed cron event. */ - __( 'The scheduled event, %s, failed to run. Your site still works, but this may indicate that scheduling posts or automated updates may not work as intended.' ), - $this->last_missed_cron - ) - ); - } + $result['description'] = sprintf( + '

%s

', + sprintf( + /* translators: %s: The name of the failed cron event. */ + __( 'The scheduled event, %s, failed to run. Your site still works, but this may indicate that scheduling posts or automated updates may not work as intended.' ), + $this->last_missed_cron + ) + ); + } elseif ( $this->has_late_cron() ) { + $result['status'] = 'recommended'; + + $result['label'] = __( 'A scheduled event is late' ); + + $result['description'] = sprintf( + '

%s

', + sprintf( + /* translators: %s: The name of the late cron event. */ + __( 'The scheduled event, %s, is late to run. Your site still works, but this may indicate that scheduling posts or automated updates may not work as intended.' ), + $this->last_late_cron + ) + ); } return $result; @@ -1926,7 +1948,7 @@ class WP_Site_Health { } foreach ( $this->crons as $id => $cron ) { - if ( ( $cron->time - time() ) < 0 ) { + if ( ( $cron->time - time() ) < $this->timeout_missed_cron ) { $this->last_missed_cron = $cron->hook; return true; } @@ -1935,6 +1957,33 @@ class WP_Site_Health { return false; } + /** + * Check if any scheduled tasks are late. + * + * Returns a boolean value of `true` if a scheduled task is late and ends processing. If the list of + * crons is an instance of WP_Error, return the instance instead of a boolean value. + * + * @return bool|WP_Error true if a cron is late, false if it wasn't. WP_Error if the cron is set to that. + */ + public function has_late_cron() { + if ( is_wp_error( $this->crons ) ) { + return $this->crons; + } + + foreach ( $this->crons as $id => $cron ) { + $cron_offset = $cron->time - time(); + if ( + $cron_offset >= $this->timeout_missed_cron && + $cron_offset < $this->timeout_late_cron + ) { + $this->last_late_cron = $cron->hook; + return true; + } + } + + return false; + } + /** * Run a loopback test on our site. * diff --git a/wp-includes/version.php b/wp-includes/version.php index 8ee2821544..bd8f7ceb38 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -13,7 +13,7 @@ * * @global string $wp_version */ -$wp_version = '5.3-alpha-45800'; +$wp_version = '5.3-alpha-45801'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.