Last active
January 10, 2021 21:32
-
-
Save emarref/d8f7c2e071e334b39a5042b06927f881 to your computer and use it in GitHub Desktop.
Deliver failed Symfony messenger messages to Sentry
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
services: | |
Sentry\FlushableClientInterface: '@Sentry\ClientInterface' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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