This commit is contained in:
Yuri Kuznetsov
2024-02-20 14:03:41 +02:00
parent 8570b6f768
commit b5d007c61c
3 changed files with 45 additions and 21 deletions
+15 -19
View File
@@ -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);
}
+18 -1
View File
@@ -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');
}
}
+12 -1
View File
@@ -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;
}
}