Skip to content

Commit

Permalink
NEW Send an email notification when a job is broken
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz committed Jun 26, 2024
1 parent 06ea01f commit 46f7e73
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 15 deletions.
2 changes: 2 additions & 0 deletions lang/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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. Log in to %s to see further details and take any necessary actions.'
CREATE_JOB_TYPE: 'Create job of type'
CREATE_NEW_JOB: 'Create new job'
JOB_EXCEPT: 'Job caused exception %s in %s at line %s'
Expand Down
28 changes: 13 additions & 15 deletions src/Services/EmailService.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use SilverStripe\Control\Email\Email;
use SilverStripe\Core\Config\Config;
use SilverStripe\Core\Injector\Injectable;
use SilverStripe\Subsites\Model\Subsite;

/**
* Class EmailService
Expand Down Expand Up @@ -35,11 +34,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);
Expand All @@ -59,12 +53,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);
Expand All @@ -81,12 +69,22 @@ public function createStalledJobReport(string $subject, string $message, int $jo
->setHTMLTemplate('QueuedJobsStalledJob');
}

public function createBrokenJobsReport(string $subject, string $message): ?Email
{
$email = $this->createReport($subject);
if ($email === null) {
return null;
}
return $email
->setData([
'Message' => $message,
])
->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
{
Expand Down
20 changes: 20 additions & 0 deletions src/Services/QueuedJobService.php
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,26 @@ public function checkJobHealth($queue = null)
]
);

// Send broken job email notification
$subject = _t(__CLASS__ . '.BROKEN_JOBS', 'Broken job(s)');
$message = htmlentities(sprintf(_t(
__CLASS__ . '.BROKEN_JOBS_MSG',
implode(' ', [
'The following job(s) appear to be broken.',
'Log in to %s to see further details and take any necessary actions.',
])
), Director::absoluteBaseURL()));
foreach ($brokenJobs as $brokenJob) {
// Using htmlentities() because of the use of $Message.RAW in QueuedJobsBrokenJobs.ss
// in order for the <br> tags to render as HTML
$message .= sprintf("<br>- %s (ID: %s)", htmlentities($brokenJob->JobTitle), $brokenJob->ID);
}
$email = EmailService::singleton()->createBrokenJobsReport($subject, $message);
if ($email) {
$email->send();
}

// Mark broken jobs as notified
$placeholders = implode(', ', array_fill(0, count($brokenIDs ?? []), '?'));
$query = SQLUpdate::create(
'"QueuedJobDescriptor"',
Expand Down
1 change: 1 addition & 0 deletions templates/QueuedJobsBrokenJobs.ss
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$Message.RAW

0 comments on commit 46f7e73

Please sign in to comment.