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

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Sentry\SentryBundle\EventListener;
  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 The current hub
  12.      */
  13.     private $hub;
  14.     /**
  15.      * @var bool Whether to capture errors thrown while processing a message that
  16.      *           will be retried
  17.      */
  18.     private $captureSoftFails;
  19.     /**
  20.      * @param HubInterface $hub              The current hub
  21.      * @param bool         $captureSoftFails Whether to capture errors thrown
  22.      *                                       while processing a message that
  23.      *                                       will be retried
  24.      */
  25.     public function __construct(HubInterface $hubbool $captureSoftFails true)
  26.     {
  27.         $this->hub $hub;
  28.         $this->captureSoftFails $captureSoftFails;
  29.     }
  30.     /**
  31.      * This method is called for each message that failed to be handled.
  32.      *
  33.      * @param WorkerMessageFailedEvent $event The event
  34.      */
  35.     public function handleWorkerMessageFailedEvent(WorkerMessageFailedEvent $event): void
  36.     {
  37.         if (!$this->captureSoftFails && $event->willRetry()) {
  38.             return;
  39.         }
  40.         $error $event->getThrowable();
  41.         if ($error instanceof HandlerFailedException) {
  42.             foreach ($error->getNestedExceptions() as $nestedException) {
  43.                 $this->hub->captureException($nestedException);
  44.             }
  45.         } else {
  46.             $this->hub->captureException($error);
  47.         }
  48.         $this->flushClient();
  49.     }
  50.     /**
  51.      * This method is called for each handled message.
  52.      *
  53.      * @param WorkerMessageHandledEvent $event The event
  54.      */
  55.     public function handleWorkerMessageHandledEvent(WorkerMessageHandledEvent $event): void
  56.     {
  57.         // Flush normally happens at shutdown... which only happens in the worker if it is run with a lifecycle limit
  58.         // such as --time=X or --limit=Y. Flush immediately in a background worker.
  59.         $this->flushClient();
  60.     }
  61.     private function flushClient(): void
  62.     {
  63.         $client $this->hub->getClient();
  64.         if (null !== $client) {
  65.             $client->flush();
  66.         }
  67.     }
  68. }