Skip to content
Permalink
Browse files

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.


git-svn-id: https://develop.svn.wordpress.org/trunk@45801 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information...
peterwilsoncc committed Aug 15, 2019
1 parent f756d80 commit f29952ab743535f81fd60a059f38f23b1d9e8bd3
Showing with 65 additions and 16 deletions.
  1. +65 −16 src/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 @@ public function get_test_scheduled_events() {
$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(
'<p>%s</p>',
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(
'<p>%s</p>',
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(
'<p>%s</p>',
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 @@ public function has_missed_cron() {
}
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 @@ public function has_missed_cron() {
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.
*

0 comments on commit f29952a

Please sign in to comment.
You can’t perform that action at this time.