vendor/symfony/messenger/EventListener/StopWorkerOnCustomStopExceptionListener.php line 43

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <[email protected]>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Messenger\EventListener;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use Symfony\Component\Messenger\Event\WorkerMessageFailedEvent;
  13. use Symfony\Component\Messenger\Event\WorkerRunningEvent;
  14. use Symfony\Component\Messenger\Exception\HandlerFailedException;
  15. use Symfony\Component\Messenger\Exception\StopWorkerExceptionInterface;
  16. /**
  17. * @author GrĂ©goire Pineau <[email protected]>
  18. */
  19. class StopWorkerOnCustomStopExceptionListener implements EventSubscriberInterface
  20. {
  21. private $stop = false;
  22. public function onMessageFailed(WorkerMessageFailedEvent $event): void
  23. {
  24. $th = $event->getThrowable();
  25. if ($th instanceof StopWorkerExceptionInterface) {
  26. $this->stop = true;
  27. }
  28. if ($th instanceof HandlerFailedException) {
  29. foreach ($th->getNestedExceptions() as $e) {
  30. if ($e instanceof StopWorkerExceptionInterface) {
  31. $this->stop = true;
  32. break;
  33. }
  34. }
  35. }
  36. }
  37. public function onWorkerRunning(WorkerRunningEvent $event): void
  38. {
  39. if ($this->stop) {
  40. $event->getWorker()->stop();
  41. }
  42. }
  43. public static function getSubscribedEvents(): array
  44. {
  45. return [
  46. WorkerMessageFailedEvent::class => 'onMessageFailed',
  47. WorkerRunningEvent::class => 'onWorkerRunning',
  48. ];
  49. }
  50. }