Send an email if the job fails to execute
This commit is contained in:
@@ -14,6 +14,8 @@ use Illuminate\Support\Str;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Symfony\Component\Lock\LockFactory;
|
||||
use Symfony\Component\Lock\Store\SemaphoreStore;
|
||||
use UserFrosting\Sprinkle\Core\Mail\TwigMailMessage;
|
||||
use UserFrosting\Sprinkle\Core\Mail\EmailRecipient;
|
||||
use UserFrosting\UniformResourceLocator\Resource as ResourceInstance;
|
||||
|
||||
/**
|
||||
@@ -160,8 +162,10 @@ class Worker
|
||||
if (!$this->currentJob->run()) {
|
||||
if ($this->debug) $this->debug->debug('Job failed!');
|
||||
if (!$this->quiet) $io->writeln('<error> [ERROR] Job failed!</error>');
|
||||
$this->ci->errorLogger->error('Job execution failed!', [ $this->queuedJob, $this->currentJob ]);
|
||||
|
||||
$this->queuedJob->flag_failed = true;
|
||||
$this->sendErrorEmail();
|
||||
} else {
|
||||
if ($this->debug) $this->debug->debug('Job completed!');
|
||||
if (!$this->quiet) $io->writeln('<info> [INFO] Job completed!</info>');
|
||||
@@ -179,6 +183,8 @@ class Worker
|
||||
|
||||
$this->queuedJob->flag_failed = true;
|
||||
$this->queuedJob->save();
|
||||
|
||||
$this->sendErrorEmail();
|
||||
} finally {
|
||||
$this->currentJob = null;
|
||||
}
|
||||
@@ -212,6 +218,8 @@ class Worker
|
||||
|
||||
$this->queuedJob->flag_failed = true;
|
||||
$this->queuedJob->save();
|
||||
|
||||
$this->sendErrorEmail();
|
||||
} finally {
|
||||
$this->currentJob = null;
|
||||
}
|
||||
@@ -222,4 +230,27 @@ class Worker
|
||||
if ($this->debug) $this->debug->debug('Exiting now');
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Send error email to site admins & requester
|
||||
*/
|
||||
protected function sendErrorEmail()
|
||||
{
|
||||
$message = new TwigMailMessage($this->ci->view, 'mail/job-failed.html.twig');
|
||||
|
||||
$message->from($this->ci->config['address_book.admin']);
|
||||
|
||||
$siteAdminRole = $this->ci->classMapper->getClassMapping('role')::query()
|
||||
->where('slug', 'site-admin')
|
||||
->first();
|
||||
$siteAdmins = $this->ci->classMapper->getClassMapping('user')::query()
|
||||
->forRole($siteAdminRole->id)
|
||||
->get();
|
||||
|
||||
foreach($siteAdmins as $admin) {
|
||||
$message->addEmailRecipient(new EmailRecipient($admin->email, $admin->full_name));
|
||||
}
|
||||
|
||||
$this->ci->mailer->send($message);
|
||||
}
|
||||
}
|
||||
|
||||
16
templates/mail/job-failed.html.twig
Normal file
16
templates/mail/job-failed.html.twig
Normal file
@@ -0,0 +1,16 @@
|
||||
{% block subject %}
|
||||
{{site.title}} - JOB FAILED
|
||||
{% endblock %}
|
||||
|
||||
{% block body %}
|
||||
<p>
|
||||
Dear Site Admins,
|
||||
</p>
|
||||
<p>
|
||||
An error has occurred whilst trying to run a queued job. Please review the server error logs.
|
||||
</p>
|
||||
<p>
|
||||
With regards,<br>
|
||||
The {{site.title}} Team
|
||||
</p>
|
||||
{% endblock %}
|
||||
Reference in New Issue
Block a user