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
This commit is contained in:
Peter Wilson 2019-08-15 00:34:53 +00:00
parent f756d801d8
commit f29952ab74
1 changed files with 65 additions and 16 deletions

View File

@ -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(
'<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 @@ 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.
*