vendor/sentry/sentry-symfony/src/EventListener/MessengerListener.php line 59

Open in your IDE?
  1. <?php
  2. namespace Sentry\SentryBundle\EventListener;
  3. use Sentry\FlushableClientInterface;
  4. use Sentry\State\HubInterface;
  5. use Symfony\Component\Messenger\Event\WorkerMessageFailedEvent;
  6. use Symfony\Component\Messenger\Event\WorkerMessageHandledEvent;
  7. use Symfony\Component\Messenger\Exception\HandlerFailedException;
  8. final class MessengerListener
  9. {
  10.     /**
  11.      * @var HubInterface
  12.      */
  13.     private $hub;
  14.     /**
  15.      * @var bool
  16.      */
  17.     private $captureSoftFails;
  18.     /**
  19.      * @param HubInterface $hub
  20.      * @param bool         $captureSoftFails
  21.      */
  22.     public function __construct(HubInterface $hubbool $captureSoftFails true)
  23.     {
  24.         $this->hub $hub;
  25.         $this->captureSoftFails $captureSoftFails;
  26.     }
  27.     /**
  28.      * @param WorkerMessageFailedEvent $event
  29.      */
  30.     public function onWorkerMessageFailed(WorkerMessageFailedEvent $event): void
  31.     {
  32.         if (! $this->captureSoftFails && $event->willRetry()) {
  33.             // Don't capture soft fails. I.e. those that will be scheduled for retry.
  34.             return;
  35.         }
  36.         $error $event->getThrowable();
  37.         if ($error instanceof HandlerFailedException) {
  38.             foreach ($error->getNestedExceptions() as $nestedException) {
  39.                 $this->hub->captureException($nestedException);
  40.             }
  41.         } else {
  42.             $this->hub->captureException($error);
  43.         }
  44.         $this->flush();
  45.     }
  46.     /**
  47.      * @param WorkerMessageHandledEvent $event
  48.      */
  49.     public function onWorkerMessageHandled(WorkerMessageHandledEvent $event): void
  50.     {
  51.         // Flush normally happens at shutdown... which only happens in the worker if it is run with a lifecycle limit
  52.         // such as --time=X or --limit=Y. Flush immediately in a background worker.
  53.         $this->flush();
  54.     }
  55.     private function flush(): void
  56.     {
  57.         $client $this->hub->getClient();
  58.         if ($client instanceof FlushableClientInterface) {
  59.             $client->flush();
  60.         }
  61.     }
  62. }