true, 'last_success' => 0, 'last_failure' => 0, 'last_error' => '', 'oldest_overdue' => 0, 'overdue' => 0, 'failed' => 0, ), $facts ); } /** * The verdict, in the shape of the other health checks. * * @param array $facts Facts. * @param int $now Now (Unix). * @return array{status:string, label:string, description:string} */ public static function evaluate( array $facts, int $now ): array { $f = self::normalise( $facts ); if ( ! $f['available'] ) { return self::result( Checks::CRITICAL, 'Background jobs are not available', 'Tidelist needs Action Scheduler (it comes with WooCommerce) to publish batches and take adverts down. Update WooCommerce.' ); } if ( $f['oldest_overdue'] >= self::CRITICAL_AFTER ) { return self::result( Checks::CRITICAL, 'Background jobs are not running', sprintf( 'The oldest waiting job is %s late (%d waiting). Adverts are not taken down or updated on time. WP-Cron probably does not run: see the troubleshooting guide, or ask your host to call wp-cron.php every 5 minutes.', self::duration( $f['oldest_overdue'] ), $f['overdue'] ) ); } if ( $f['oldest_overdue'] >= self::LATE_AFTER ) { return self::result( Checks::RECOMMENDED, 'Background jobs are running late', sprintf( 'The oldest waiting job is %s late (%d waiting). On a quiet site WP-Cron only runs when someone visits: a real cron job fixes that.', self::duration( $f['oldest_overdue'] ), $f['overdue'] ) ); } if ( $f['failed'] > 0 ) { $why = '' !== $f['last_error'] ? ' Last error: ' . $f['last_error'] : ''; return self::result( Checks::RECOMMENDED, 'Some background jobs failed', sprintf( '%d job(s) failed.%s Open WooCommerce → Status → Scheduled Actions → Failed to see them.', $f['failed'], $why ) ); } if ( $f['last_success'] > 0 && $now - $f['last_success'] > self::STALE_AFTER ) { return self::result( Checks::RECOMMENDED, 'No background job has finished lately', sprintf( 'The last job finished %s ago. The daily upkeep should run every day. Check that WP-Cron works.', self::duration( $now - $f['last_success'] ) ) ); } return self::result( Checks::GOOD, 'Background jobs are running', 'Waiting jobs are on time and none failed.' ); } /** * A length of time in words: "45 min", "3 h 10 min", "2 days". * * @param int $seconds Seconds. */ public static function duration( int $seconds ): string { $seconds = max( 0, $seconds ); if ( $seconds < 3600 ) { return max( 1, (int) round( $seconds / 60 ) ) . ' min'; } if ( $seconds < 172800 ) { $hours = intdiv( $seconds, 3600 ); $minutes = intdiv( $seconds % 3600, 60 ); return $hours . ' h' . ( $minutes > 0 ? ' ' . $minutes . ' min' : '' ); } return intdiv( $seconds, 86400 ) . ' days'; } /** * Result array. * * @param string $status Status. * @param string $label Label. * @param string $description Description. */ private static function result( string $status, string $label, string $description ): array { return array( 'status' => $status, 'label' => $label, 'description' => $description, ); } }