Send an email if the job fails to execute

This commit is contained in:
2022-05-24 17:40:19 +01:00
parent 27ea6697bf
commit 95ecc63db8
2 changed files with 47 additions and 0 deletions

View File

@@ -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);
}
}