From dbbcd67896693d4ac79e1fe12e69dcc7f449e582 Mon Sep 17 00:00:00 2001 From: Steve Boyd Date: Thu, 27 Jun 2024 15:06:22 +1200 Subject: [PATCH] NEW Send an email notification when a job is broken --- lang/en.yml | 2 ++ src/Services/EmailService.php | 31 ++++++++++++++++--------------- src/Services/QueuedJobService.php | 9 +++++++++ templates/QueuedJobsBrokenJobs.ss | 7 +++++++ 4 files changed, 34 insertions(+), 15 deletions(-) create mode 100644 templates/QueuedJobsBrokenJobs.ss diff --git a/lang/en.yml b/lang/en.yml index 0de2d11c..5d9d698a 100644 --- a/lang/en.yml +++ b/lang/en.yml @@ -19,6 +19,8 @@ en: PLURALNAME: 'Queued Job Rules' SINGULARNAME: 'Queued Job Rule' QueuedJobs: + BROKEN_JOBS: 'Broken job(s)' + BROKEN_JOBS_MSG: 'The following job(s) appear to be broken.' CREATE_JOB_TYPE: 'Create job of type' CREATE_NEW_JOB: 'Create new job' JOB_EXCEPT: 'Job caused exception %s in %s at line %s' diff --git a/src/Services/EmailService.php b/src/Services/EmailService.php index 0aae9d19..7f4b98ee 100644 --- a/src/Services/EmailService.php +++ b/src/Services/EmailService.php @@ -6,7 +6,7 @@ use SilverStripe\Control\Email\Email; use SilverStripe\Core\Config\Config; use SilverStripe\Core\Injector\Injectable; -use SilverStripe\Subsites\Model\Subsite; +use SilverStripe\ORM\DataList; /** * Class EmailService @@ -35,11 +35,6 @@ public function __construct() ); } - /** - * @param array $jobConfig - * @param string $title - * @return Email|null - */ public function createMissingDefaultJobReport(array $jobConfig, string $title): ?Email { $subject = sprintf('Default Job "%s" missing', $title); @@ -59,12 +54,6 @@ public function createMissingDefaultJobReport(array $jobConfig, string $title): ->setHTMLTemplate('QueuedJobsDefaultJob'); } - /** - * @param string $subject - * @param string $message - * @param int $jobID - * @return Email|null - */ public function createStalledJobReport(string $subject, string $message, int $jobID): ?Email { $email = $this->createReport($subject); @@ -81,12 +70,24 @@ public function createStalledJobReport(string $subject, string $message, int $jo ->setHTMLTemplate('QueuedJobsStalledJob'); } + public function createBrokenJobsReport(string $subject, string $message, DataList $jobs): ?Email + { + $email = $this->createReport($subject); + if ($email === null) { + return null; + } + return $email + ->setData([ + 'Message' => $message, + 'Jobs' => $jobs, + 'Site' => Director::absoluteBaseURL(), + ]) + ->setHTMLTemplate('QueuedJobsBrokenJobs'); + } + /** * Create a generic email report * useful for reporting queue service issues - * - * @param string $subject - * @return Email|null */ public function createReport(string $subject): ?Email { diff --git a/src/Services/QueuedJobService.php b/src/Services/QueuedJobService.php index 20eddc46..43aadafc 100644 --- a/src/Services/QueuedJobService.php +++ b/src/Services/QueuedJobService.php @@ -519,6 +519,15 @@ public function checkJobHealth($queue = null) ] ); + // Send broken job email notification + $subject = _t(__CLASS__ . '.BROKEN_JOBS', 'Broken job(s)'); + $message = _t(__CLASS__ . '.BROKEN_JOBS_MSG', 'The following job(s) appear to be broken.'); + $email = EmailService::singleton()->createBrokenJobsReport($subject, $message, $brokenJobs); + if ($email) { + $email->send(); + } + + // Mark broken jobs as notified $placeholders = implode(', ', array_fill(0, count($brokenIDs ?? []), '?')); $query = SQLUpdate::create( '"QueuedJobDescriptor"', diff --git a/templates/QueuedJobsBrokenJobs.ss b/templates/QueuedJobsBrokenJobs.ss new file mode 100644 index 00000000..ee4dc924 --- /dev/null +++ b/templates/QueuedJobsBrokenJobs.ss @@ -0,0 +1,7 @@ +$Message + +<% loop $Jobs %> +- $JobTitle (ID: $ID) +<% end_loop %> + +Log in to $Site to see further details and take any necessary actions.