src/Core/Framework/Webhook/Subscriber/RetryWebhookMessageFailedSubscriber.php line 37

  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Framework\Webhook\Subscriber;
  3. use Shopware\Core\Framework\Context;
  4. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  6. use Shopware\Core\Framework\Log\Package;
  7. use Shopware\Core\Framework\Webhook\EventLog\WebhookEventLogDefinition;
  8. use Shopware\Core\Framework\Webhook\Message\WebhookEventMessage;
  9. use Shopware\Core\Framework\Webhook\WebhookEntity;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Symfony\Component\Messenger\Event\WorkerMessageFailedEvent;
  12. /**
  13.  * @internal
  14.  */
  15. #[Package('core')]
  16. class RetryWebhookMessageFailedSubscriber implements EventSubscriberInterface
  17. {
  18.     private const MAX_WEBHOOK_ERROR_COUNT 10;
  19.     /**
  20.      * @internal
  21.      */
  22.     public function __construct(private readonly EntityRepository $webhookRepository, private readonly EntityRepository $webhookEventLogRepository)
  23.     {
  24.     }
  25.     public static function getSubscribedEvents(): array
  26.     {
  27.         return [
  28.             WorkerMessageFailedEvent::class => 'failed',
  29.         ];
  30.     }
  31.     public function failed(WorkerMessageFailedEvent $event): void
  32.     {
  33.         if ($event->willRetry()) {
  34.             return;
  35.         }
  36.         $message $event->getEnvelope()->getMessage();
  37.         if (!$message instanceof WebhookEventMessage) {
  38.             return;
  39.         }
  40.         $webhookId $message->getWebhookId();
  41.         $webhookEventLogId $message->getWebhookEventId();
  42.         $this->markWebhookEventFailed($webhookEventLogId);
  43.         /** @var WebhookEntity|null $webhook */
  44.         $webhook $this->webhookRepository
  45.             ->search(new Criteria([$webhookId]), Context::createDefaultContext())
  46.             ->get($webhookId);
  47.         if ($webhook === null || !$webhook->isActive()) {
  48.             return;
  49.         }
  50.         $webhookErrorCount $webhook->getErrorCount() + 1;
  51.         $params = [
  52.             'id' => $webhook->getId(),
  53.             'errorCount' => $webhookErrorCount,
  54.         ];
  55.         if ($webhookErrorCount >= self::MAX_WEBHOOK_ERROR_COUNT) {
  56.             $params array_merge($params, [
  57.                 'errorCount' => 0,
  58.                 'active' => false,
  59.             ]);
  60.         }
  61.         $this->webhookRepository->update([$params], Context::createDefaultContext());
  62.     }
  63.     private function markWebhookEventFailed(string $id): void
  64.     {
  65.         $this->webhookEventLogRepository->update([
  66.             ['id' => $id'deliveryStatus' => WebhookEventLogDefinition::STATUS_FAILED],
  67.         ], Context::createDefaultContext());
  68.     }
  69. }