Skip to content

Commit

Permalink
add: failed job notification
Browse files Browse the repository at this point in the history
  • Loading branch information
j-schumann committed Nov 24, 2017
1 parent 148566b commit 95f6508
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 24 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

The project follows Semantic Versioning (http://semver.org/)

## 4.3.0 - tbd
### Added
- event listener for failed slmQueue jobs that send an email to all queue admins

## 4.2.0 - 2017-11-17
### Added
- shutdown the slmQueue worker when the entityManager was closed (due to an
Expand Down
87 changes: 63 additions & 24 deletions src/Notification/AdminNotifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@

namespace AppBase\Notification;

use DateTime;
use IntlDateFormatter;
use RuntimeException;
use SlmQueue\Worker\Event\ProcessJobEvent;
use SlmQueue\Worker\Event\WorkerEventInterface;
use Vrok\Service\Email as EmailService;
use Vrok\Service\UserManager;
use Zend\EventManager\EventInterface;
Expand Down Expand Up @@ -87,16 +92,12 @@ public function attach(EventManagerInterface $events, $priority = 1)
$priority
);

/*
* https://github.com/juriansluiman/SlmQueue/pull/104 is merged, we can
* now listen for failed jobs
* @todo implement listener
$shared->attach(
'SlmQueue\Worker\WorkerInterface',
\SlmQueue\Worker\WorkerEvent::EVENT_PROCESS_JOB_POST,
WorkerEventInterface::EVENT_PROCESS_JOB,
[$this, 'onProcessJobPost'],
$priority
);*/
-100 // 100 = preProcess, -100 = postProcess
);
}

/**
Expand All @@ -105,7 +106,7 @@ public function attach(EventManagerInterface $events, $priority = 1)
*
* @param \Zend\EventManager\EventInterface $e
*
* @throws \RuntimeException when the queueAdmin group does not exist
* @throws RuntimeException when the queueAdmin group does not exist
*/
public function onBuriedJobsFound(EventInterface $e)
{
Expand All @@ -129,7 +130,7 @@ public function onBuriedJobsFound(EventInterface $e)
$group = $this->userManager->getGroupRepository()
->findOneBy(['name' => 'queueAdmin']);
if (! $group) {
throw new \RuntimeException(
throw new RuntimeException(
'Group "queueAdmin" not found when buried jobs where found!'
);
}
Expand Down Expand Up @@ -176,7 +177,7 @@ public function onLongRunningJobsFound(EventInterface $e)
->findOneBy(['name' => 'queueAdmin']);

if (! $group) {
throw new \RuntimeException(
throw new RuntimeException(
'Group "queueAdmin" not found when long running jobs where found!'
);
}
Expand Down Expand Up @@ -209,15 +210,15 @@ public function onProcessNotRunning(EventInterface $e)
$mail->setHtmlBody(['mail.supervisor.processNotRunning.body', [
'processName' => $processName,
'processState' => $processInfo ? $processInfo['statename'] : 'NOT_FOUND',
'now' => $dateFormat(new \DateTime(),
\IntlDateFormatter::LONG, \IntlDateFormatter::MEDIUM),
'now' => $dateFormat(new DateTime(),
IntlDateFormatter::LONG, IntlDateFormatter::MEDIUM),
'supervisorUrl' => $fullUrl('https').$url('supervisor'),
]]);

$group = $this->userManager->getGroupRepository()
->findOneBy(['name' => 'supervisorAdmin']);
if (! $group) {
throw new \RuntimeException(
throw new RuntimeException(
'Group "supervisorAdmin" not found when a process was not running!'
);
}
Expand All @@ -231,19 +232,57 @@ public function onProcessNotRunning(EventInterface $e)
}

/**
* @todo implement
* Checks the result of each processed job. If it failed an email with
* the saved message is sent to all queue admins.
*
* @param \Zend\EventManager\EventInterface $e
* @param ProcessJobEvent $e
*/
public function onProcessJobPost(EventInterface $e)
public function onProcessJobPost(ProcessJobEvent $e)
{
// \Doctrine\Common\Util\Debug::dump($e, 4);
// \Doctrine\Common\Util\Debug::dump($e->getJob(), 4);
// $log = $this->serviceLocator->get('ZendLog');
/* @var $log \Zend\Log\Logger */

// $log->debug(get_class($e));
// $log->debug(get_class($e->getTarget()));
// $log->debug(get_class($e->getParam('job')));
$result = $e->getResult();
if ($result != ProcessJobEvent::JOB_STATUS_FAILURE) {
return;
}

$queue = $e->getQueue();
// There is no way to retrieve the message given to $queue->bury(),
// not even with $queue->peek()
$sql = 'SELECT * FROM '.$queue->getOptions()->getTableName()
.' WHERE id = ?';
$jobs = $queue->connection->fetchAll($sql, [$e->getJob()->getId()]);
$job = $jobs[0];
$data = json_decode($job['data'], true);

$url = $this->emailService->getViewHelperManager()->get('url');
$fullUrl = $this->emailService->getViewHelperManager()->get('fullUrl');
$dateFormat = $this->emailService->getViewHelperManager()->get('DateFormat');

$mail = $this->emailService->createMail();
$mail->setSubject('mail.slmQueue.jobFailed.subject');

$mail->setHtmlBody(['mail.slmQueue.jobFailed.body', [
'jobName' => $data['metadata']['__name__'],
'message' => $job['message'],
'now' => $dateFormat(new DateTime(),
IntlDateFormatter::LONG, IntlDateFormatter::MEDIUM),
'queueUrl' => $fullUrl('https').$url('slm-queue/list-buried', [
'name' => $queue->getName(),
]),
]]);

$group = $this->userManager->getGroupRepository()
->findOneBy(['name' => 'queueAdmin']);
if (! $group) {
throw new RuntimeException(
'Group "queueAdmin" not found when a job failed!'
);
}

$admins = $group->getMembers();
foreach ($admins as $user) {
$mail->addTo($user->getEmail(), $user->getDisplayName());
}

$this->emailService->sendMail($mail);
}
}

0 comments on commit 95f6508

Please sign in to comment.