Tasks
6 days ago
SendingErrorHandler.php
2 years ago
SendingQueue.php
6 days ago
SendingThrottlingHandler.php
2 months ago
index.php
3 years ago
SendingErrorHandler.php
87 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 | |
| 3 | namespace MailPoet\Cron\Workers\SendingQueue; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Entities\ScheduledTaskEntity; |
| 9 | use MailPoet\Entities\SendingQueueEntity; |
| 10 | use MailPoet\Logging\LoggerFactory; |
| 11 | use MailPoet\Mailer\MailerError; |
| 12 | use MailPoet\Mailer\MailerLog; |
| 13 | use MailPoet\Newsletter\Sending\ScheduledTaskSubscribersRepository; |
| 14 | use MailPoet\Newsletter\Sending\SendingQueuesRepository; |
| 15 | |
| 16 | class SendingErrorHandler { |
| 17 | /** @var ScheduledTaskSubscribersRepository */ |
| 18 | private $scheduledTaskSubscribersRepository; |
| 19 | |
| 20 | /** @var SendingThrottlingHandler */ |
| 21 | private $throttlingHandler; |
| 22 | |
| 23 | /** @var SendingQueuesRepository */ |
| 24 | private $sendingQueuesRepository; |
| 25 | |
| 26 | /** @var LoggerFactory */ |
| 27 | private $loggerFactory; |
| 28 | |
| 29 | public function __construct( |
| 30 | ScheduledTaskSubscribersRepository $scheduledTaskSubscribersRepository, |
| 31 | SendingThrottlingHandler $throttlingHandler, |
| 32 | SendingQueuesRepository $sendingQueuesRepository, |
| 33 | LoggerFactory $loggerFactory |
| 34 | ) { |
| 35 | $this->scheduledTaskSubscribersRepository = $scheduledTaskSubscribersRepository; |
| 36 | $this->throttlingHandler = $throttlingHandler; |
| 37 | $this->sendingQueuesRepository = $sendingQueuesRepository; |
| 38 | $this->loggerFactory = $loggerFactory; |
| 39 | } |
| 40 | |
| 41 | public function processError( |
| 42 | MailerError $error, |
| 43 | ScheduledTaskEntity $task, |
| 44 | array $preparedSubscribersIds, |
| 45 | array $preparedSubscribers |
| 46 | ) { |
| 47 | if ($error->getLevel() === MailerError::LEVEL_HARD) { |
| 48 | return $this->processHardError($error); |
| 49 | } |
| 50 | $this->processSoftError($error, $task, $preparedSubscribersIds, $preparedSubscribers); |
| 51 | } |
| 52 | |
| 53 | private function processHardError(MailerError $error) { |
| 54 | if ($error->getRetryInterval() !== null) { |
| 55 | MailerLog::processNonBlockingError($error->getOperation(), $error->getMessageWithFailedSubscribers(), $error->getRetryInterval()); |
| 56 | } else { |
| 57 | $throttledBatchSize = null; |
| 58 | if ($error->getOperation() === MailerError::OPERATION_CONNECT) { |
| 59 | $throttledBatchSize = $this->throttlingHandler->throttleBatchSize(); |
| 60 | } |
| 61 | MailerLog::processError($error->getOperation(), $error->getMessageWithFailedSubscribers(), null, false, $throttledBatchSize); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | private function processSoftError(MailerError $error, ScheduledTaskEntity $task, $preparedSubscribersIds, $preparedSubscribers) { |
| 66 | foreach ($error->getSubscriberErrors() as $subscriberError) { |
| 67 | $subscriberIdIndex = array_search($subscriberError->getEmail(), $preparedSubscribers); |
| 68 | $message = $subscriberError->getMessage() ?: $error->getMessage(); |
| 69 | $this->scheduledTaskSubscribersRepository->saveError($task, $preparedSubscribersIds[$subscriberIdIndex], $message ?? ''); |
| 70 | } |
| 71 | |
| 72 | $queue = $task->getSendingQueue(); |
| 73 | |
| 74 | if ($queue instanceof SendingQueueEntity) { |
| 75 | if ($error->getOperation() === MailerError::OPERATION_DOMAIN_AUTHORIZATION) { |
| 76 | $this->loggerFactory->getLogger(LoggerFactory::TOPIC_NEWSLETTERS)->info( |
| 77 | 'Paused task in sending queue due to sender domain authorization error', |
| 78 | ['task_id' => $task->getId()] |
| 79 | ); |
| 80 | $this->sendingQueuesRepository->pause($queue); |
| 81 | return; |
| 82 | } |
| 83 | $this->sendingQueuesRepository->updateCounts($queue); |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 |