Skip to content

Commit

Permalink
Add alert to display scheduled job errors
Browse files Browse the repository at this point in the history
  • Loading branch information
jitendrapurohit committed Oct 7, 2021
1 parent 078cc1f commit e34e501
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions CRM/Utils/Check/Component/Env.php
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,51 @@ public function checkExtensions() {
return $messages;
}

/**
* @return CRM_Utils_Check_Message[]
*/
public function checkScheduledJobLogErrors() {
$jobs = civicrm_api3('Job', 'get', [
'sequential' => 1,
'return' => ["id", "name", "last_run"],
'is_active' => 1,
'options' => ['limit' => 0],
]);
$html = '';
foreach ($jobs['values'] as $job) {
$lastExecutionMessage = civicrm_api3('JobLog', 'get', [
'sequential' => 1,
'return' => ["description"],
'job_id' => $job['id'],
'options' => ['sort' => "id desc", 'limit' => 1],
])['values'][0]['description'] ?? NULL;
if (!empty($lastExecutionMessage) && strpos($lastExecutionMessage, 'Failure') !== false) {
$viewLogURL = CRM_Utils_System::url('civicrm/admin/joblog', "jid={$job['id']}&reset=1");
$html .= "<tr><td>{$job['name']} </td><td>{$lastExecutionMessage}</td><td>{$job['last_run']}</td><td> <a href='{$viewLogURL}'>View Job Log</a></td></tr>";
}
}
if (empty($html)) {
return [];
}

$message = "<p>The following scheduled jobs failed on the last run:</p>
<p><table><thead><tr><th>Job</th><th>Message</th><th>Date</th><th></th>
</tr></thead><tbody>
$html
</tbody></table></p>
";

$msg = new CRM_Utils_Check_Message(
__FUNCTION__,
ts($message),
ts('Scheduled Job Failures'),
\Psr\Log\LogLevel::WARNING,
'fa-server'
);
$messages[] = $msg;
return $messages;
}

/**
* Checks if there are pending extension upgrades.
*
Expand Down

0 comments on commit e34e501

Please sign in to comment.