From b5d007c61c734ba38e15fb7a339171fdd8372181 Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Tue, 20 Feb 2024 14:03:41 +0200 Subject: [PATCH] ref --- application/Espo/Core/Webhook/Queue.php | 34 ++++++++----------- application/Espo/Entities/Webhook.php | 19 ++++++++++- .../Espo/Entities/WebhookQueueItem.php | 13 ++++++- 3 files changed, 45 insertions(+), 21 deletions(-) diff --git a/application/Espo/Core/Webhook/Queue.php b/application/Espo/Core/Webhook/Queue.php index 2981febbb4..f2f901b538 100644 --- a/application/Espo/Core/Webhook/Queue.php +++ b/application/Espo/Core/Webhook/Queue.php @@ -33,12 +33,10 @@ use Espo\Entities\User; use Espo\Entities\Webhook; use Espo\Entities\WebhookEventQueueItem; use Espo\Entities\WebhookQueueItem; - use Espo\Core\AclManager; use Espo\Core\Utils\Config; use Espo\Core\Utils\DateTime as DateTimeUtil; use Espo\Core\Utils\Log; - use Espo\ORM\EntityManager; use Espo\ORM\Query\Part\Condition as Cond; @@ -135,7 +133,7 @@ class Queue ->select('MIN:(number)') ->from(WebhookQueueItem::ENTITY_TYPE) ->where([ - 'status' => 'Pending', + 'status' => WebhookQueueItem::STATUS_PENDING, 'OR' => [ ['processAt' => null], ['processAt<=' => DateTimeUtil::getSystemNowString()], @@ -162,7 +160,7 @@ class Queue ->getRDBRepository(WebhookQueueItem::ENTITY_TYPE) ->where([ 'webhookId' => $webhookId, - 'status' => 'Pending', + 'status' => WebhookQueueItem::STATUS_PENDING, 'OR' => [ ['processAt' => null], ['processAt<=' => DateTimeUtil::getSystemNowString()], @@ -175,7 +173,7 @@ class Queue /** @var ?Webhook $webhook */ $webhook = $this->entityManager->getEntityById(Webhook::ENTITY_TYPE, $webhookId); - if (!$webhook || !$webhook->get('isActive')) { + if (!$webhook || !$webhook->isActive()) { foreach ($itemList as $item) { $this->deleteQueueItem($item); } @@ -187,9 +185,9 @@ class Queue $user = null; - if ($webhook->get('userId')) { + if ($webhook->getUserId()) { /** @var ?User $user */ - $user = $this->entityManager->getEntity(User::ENTITY_TYPE, $webhook->get('userId')); + $user = $this->entityManager->getEntityById(User::ENTITY_TYPE, $webhook->getUserId()); if (!$user) { foreach ($itemList as $item) { @@ -200,7 +198,7 @@ class Queue } $forbiddenAttributeList = $this->aclManager - ->getScopeForbiddenAttributeList($user, $webhook->get('entityType')); + ->getScopeForbiddenAttributeList($user, $webhook->getTargetEntityType()); } $actualItemList = []; @@ -267,7 +265,7 @@ class Queue } /** - * @param mixed[] $dataList + * @param stdClass[] $dataList * @param WebhookQueueItem[] $itemList */ private function send(Webhook $webhook, array $dataList, array $itemList): void @@ -278,9 +276,7 @@ class Queue catch (Exception $e) { $this->failQueueItemList($itemList, true); - $this->log->error( - "Webhook Queue: Webhook '" . $webhook->getId() . "' sending failed. Error: " . $e->getMessage() - ); + $this->log->error("Webhook Queue: Webhook '{$webhook->getId()}' sending failed. Error: {$e->getMessage()}"); return; } @@ -306,7 +302,7 @@ class Queue protected function logSending(Webhook $webhook, int $code): void { - $this->log->debug("Webhook Queue: Webhook '" . $webhook->getId() . "' sent, response code: {$code}."); + $this->log->debug("Webhook Queue: Webhook '{$webhook->getId()}' sent, response code: $code."); } /** @@ -341,7 +337,7 @@ class Queue $itemList = $this->entityManager ->getRDBRepository(WebhookQueueItem::ENTITY_TYPE) ->where([ - 'status' => 'Pending', + 'status' => WebhookQueueItem::STATUS_PENDING, 'webhookId' => $webhook->getId(), ]) ->order('number') @@ -357,8 +353,8 @@ class Queue protected function succeedQueueItem(WebhookQueueItem $item): void { $item->set([ - 'attempts' => $item->get('attempts') + 1, - 'status' => 'Success', + 'attempts' => $item->getAttempts() + 1, + 'status' => WebhookQueueItem::STATUS_SUCCESS, 'processedAt' => DateTimeUtil::getSystemNowString(), ]); @@ -367,7 +363,7 @@ class Queue protected function failQueueItem(WebhookQueueItem $item, bool $force = false): void { - $attempts = $item->get('attempts') + 1; + $attempts = $item->getAttempts() + 1; $maxAttemptsNumber = $this->config->get('webhookMaxAttemptNumber', self::MAX_ATTEMPT_NUMBER); $period = $this->config->get('webhookFailAttemptPeriod', self::FAIL_ATTEMPT_PERIOD); @@ -377,7 +373,6 @@ class Queue } $dt = new DateTime(); - $dt->modify($period); $item->set([ @@ -386,7 +381,8 @@ class Queue ]); if ($attempts >= $maxAttemptsNumber) { - $item->set('status', 'Failed'); + $item->set('status', WebhookQueueItem::STATUS_FAILED); + /** @noinspection PhpRedundantOptionalArgumentInspection */ $item->set('processAt', null); } diff --git a/application/Espo/Entities/Webhook.php b/application/Espo/Entities/Webhook.php index 23cd5805e4..755721d8e8 100644 --- a/application/Espo/Entities/Webhook.php +++ b/application/Espo/Entities/Webhook.php @@ -29,7 +29,9 @@ namespace Espo\Entities; -class Webhook extends \Espo\Core\ORM\Entity +use Espo\Core\ORM\Entity; + +class Webhook extends Entity { public const ENTITY_TYPE = 'Webhook'; @@ -47,4 +49,19 @@ class Webhook extends \Espo\Core\ORM\Entity { return $this->get('url'); } + + public function isActive(): bool + { + return $this->get('isActive'); + } + + public function getUserId(): ?string + { + return $this->get('userId'); + } + + public function getTargetEntityType(): string + { + return $this->get('entityType'); + } } diff --git a/application/Espo/Entities/WebhookQueueItem.php b/application/Espo/Entities/WebhookQueueItem.php index 7edb9301c1..4bb8502d8a 100644 --- a/application/Espo/Entities/WebhookQueueItem.php +++ b/application/Espo/Entities/WebhookQueueItem.php @@ -29,7 +29,18 @@ namespace Espo\Entities; -class WebhookQueueItem extends \Espo\Core\ORM\Entity +use Espo\Core\ORM\Entity; + +class WebhookQueueItem extends Entity { public const ENTITY_TYPE = 'WebhookQueueItem'; + + public const STATUS_PENDING = 'Pending'; + public const STATUS_SUCCESS = 'Success'; + public const STATUS_FAILED = 'Failed'; + + public function getAttempts(): int + { + return $this->get('attempts') ?? 0; + } }