Skip to content

Commit

Permalink
CheckEnv - Give new installs a grace period before 'Cron Not Running'…
Browse files Browse the repository at this point in the history
… error
  • Loading branch information
colemanw committed Jul 13, 2020
1 parent c797aec commit bf76eb8
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 22 deletions.
1 change: 1 addition & 0 deletions CRM/Core/JobManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public function execute($auth = TRUE) {
$statusPref = [
'name' => 'checkLastCron',
'check_info' => gmdate('U'),
'prefs' => '',
];
CRM_Core_BAO_StatusPreference::create($statusPref);
}
Expand Down
58 changes: 36 additions & 22 deletions CRM/Utils/Check/Component/Env.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,49 +258,63 @@ public function checkDefaultMailbox() {
}

/**
* Checks if cron has run in a reasonable amount of time
* Checks if cron has run in the past hour (3600 seconds)
* @return array
* @throws CRM_Core_Exception
*/
public function checkLastCron() {
$messages = [];

$statusPreference = new CRM_Core_DAO_StatusPreference();
$statusPreference->domain_id = CRM_Core_Config::domainID();
$statusPreference->name = 'checkLastCron';
$statusPreference->name = __FUNCTION__;

$level = \Psr\Log\LogLevel::INFO;
$now = gmdate('U');

// Get timestamp of last cron run
if ($statusPreference->find(TRUE) && !empty($statusPreference->check_info)) {
$lastCron = $statusPreference->check_info;
$msg = ts('Last cron run at %1.', [1 => CRM_Utils_Date::customFormat(date('c', $lastCron))]);
$msg = ts('Last cron run at %1.', [1 => CRM_Utils_Date::customFormat(date('c', $statusPreference->check_info))]);
}
// If cron record doesn't exist, this is a new install. Make a placeholder record (prefs='new').
else {
$lastCron = 0;
$msg = ts('No cron runs have been recorded.');
$statusPreference = CRM_Core_BAO_StatusPreference::create([
'name' => __FUNCTION__,
'check_info' => $now,
'prefs' => 'new',
]);
}
$lastCron = $statusPreference->check_info;

if ($lastCron > gmdate('U') - 3600) {
$messages[] = new CRM_Utils_Check_Message(
__FUNCTION__,
$msg,
ts('Cron Running OK'),
\Psr\Log\LogLevel::INFO,
'fa-clock-o'
);
if ($statusPreference->prefs !== 'new' && $lastCron > $now - 3600) {
$title = ts('Cron Running OK');
}
else {
// If placeholder record found, give one day "grace period" for admin to set-up cron
if ($statusPreference->prefs === 'new') {
$msg = ts('No cron runs have been recorded.');
}
if ($statusPreference->prefs === 'new' && $lastCron > $now - 86400) {
$title = ts('Set-up Cron');
}
else {
$title = ts('Cron Not Running');
// After 1 day (86400 seconds) increase the error level
$level = ($lastCron > $now - 86400) ? \Psr\Log\LogLevel::WARNING : \Psr\Log\LogLevel::ERROR;
}
$cronLink = 'target="_blank" href="' . htmlentities(CRM_Utils_System::docURL2('sysadmin/setup/jobs/', TRUE)) . '""';
$msg .= '<p>' . ts('To enable scheduling support, please <a %1>set up the cron job</a>.', [
1 => $cronLink,
]) . '</p>';
$message = new CRM_Utils_Check_Message(
__FUNCTION__,
$msg,
ts('Cron Not Running'),
($lastCron > gmdate('U') - 86400) ? \Psr\Log\LogLevel::WARNING : \Psr\Log\LogLevel::ERROR,
'fa-clock-o'
);
$messages[] = $message;
}

$messages[] = new CRM_Utils_Check_Message(
__FUNCTION__,
$msg,
$title,
$level,
'fa-clock-o'
);
return $messages;
}

Expand Down

0 comments on commit bf76eb8

Please sign in to comment.