Skip to content

Instantly share code, notes, and snippets.

@emarref
Last active January 10, 2021 21:32
Show Gist options
  • Save emarref/d8f7c2e071e334b39a5042b06927f881 to your computer and use it in GitHub Desktop.
Save emarref/d8f7c2e071e334b39a5042b06927f881 to your computer and use it in GitHub Desktop.
Deliver failed Symfony messenger messages to Sentry
services:
Sentry\FlushableClientInterface: '@Sentry\ClientInterface'
<?php declare(strict_types=1);
namespace App\Subscriber;
use Sentry\FlushableClientInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Messenger\Event\WorkerMessageFailedEvent;
use Symfony\Component\Messenger\Exception\HandlerFailedException;
class WorkerSubscriber implements EventSubscriberInterface
{
private FlushableClientInterface $client;
/**
* @param FlushableClientInterface $client
*/
public function __construct(FlushableClientInterface $client)
{
$this->client = $client;
}
public function onWorkerMessageFailed(WorkerMessageFailedEvent $event): void
{
if ($event->willRetry()) {
// Only capture the hard fails.
return;
}
$error = $event->getThrowable();
if ($error instanceof HandlerFailedException) {
$error = $error->getPrevious();
}
$this->client->captureException($error);
// Flush normally happens at shutdown... which never happens in the worker.
$this->client->flush();
}
public static function getSubscribedEvents(): array
{
return [
WorkerMessageFailedEvent::class => 'onWorkerMessageFailed',
];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment