From 08d32cba85ae5dcd2323789bb67b37965822d630 Mon Sep 17 00:00:00 2001 From: jitendrapurohit Date: Thu, 7 Oct 2021 14:49:45 +0530 Subject: [PATCH] Add alert to display scheduled job errors --- CRM/Utils/Check/Component/Env.php | 45 +++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/CRM/Utils/Check/Component/Env.php b/CRM/Utils/Check/Component/Env.php index c1d7b76794..3614ee9e00 100644 --- a/CRM/Utils/Check/Component/Env.php +++ b/CRM/Utils/Check/Component/Env.php @@ -688,6 +688,51 @@ class CRM_Utils_Check_Component_Env extends CRM_Utils_Check_Component { 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 .= "{$job['name']} {$lastExecutionMessage}{$job['last_run']} View Job Log"; + } + } + if (empty($html)) { + return []; + } + + $message = "

The following scheduled jobs failed on the last run:

+

+ + $html +
JobMessageDate

+ "; + + $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. * -- 2.25.1