Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NEW Send an email notification when a job is broken #436

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.'
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
31 changes: 16 additions & 15 deletions src/Services/EmailService.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use SilverStripe\Control\Email\Email;
use SilverStripe\Core\Config\Config;
use SilverStripe\Core\Injector\Injectable;
use SilverStripe\Subsites\Model\Subsite;
GuySartorelli marked this conversation as resolved.
Show resolved Hide resolved
use SilverStripe\ORM\DataList;

/**
* Class EmailService
Expand Down Expand Up @@ -35,11 +35,6 @@ public function __construct()
);
}

/**
* @param array $jobConfig
GuySartorelli marked this conversation as resolved.
Show resolved Hide resolved
* @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 +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);
Expand All @@ -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,
GuySartorelli marked this conversation as resolved.
Show resolved Hide resolved
'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
{
Expand Down
9 changes: 9 additions & 0 deletions src/Services/QueuedJobService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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"',
Expand Down
7 changes: 7 additions & 0 deletions templates/QueuedJobsBrokenJobs.ss
Original file line number Diff line number Diff line change
@@ -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.
Loading