1
0
Fork 0
mirror of synced 2024-10-03 02:37:40 +13:00

Renamed webhook variables

This commit is contained in:
Khushboo Verma 2023-11-20 19:49:15 +05:30
parent 2bcb6c18fb
commit 2c04c98801
4 changed files with 40 additions and 28 deletions

View file

@ -1486,7 +1486,7 @@ $commonCollections = [
[
'$id' => ID::custom('_key_enabled_type'),
'type' => Database::INDEX_KEY,
'attributes' => ['enabled','type'],
'attributes' => ['enabled', 'type'],
'lengths' => [],
'orders' => [Database::ORDER_ASC],
],
@ -4505,26 +4505,15 @@ $consoleCollections = array_merge([
'filters' => [],
],
[
'$id' => ID::custom('status'),
'$id' => ID::custom('enabled'),
'type' => Database::VAR_BOOLEAN,
'format' => '',
'size' => 0,
'signed' => true,
'size' => 0,
'format' => '',
'filters' => [],
'required' => false,
'default' => true,
'array' => false,
'filters' => [],
],
[
'$id' => ID::custom('errors'),
'type' => Database::VAR_INTEGER,
'format' => '',
'size' => 0,
'signed' => true,
'required' => false,
'default' => 0,
'array' => false,
'filters' => [],
],
[
'$id' => ID::custom('logs'),
@ -4537,6 +4526,17 @@ $consoleCollections = array_merge([
'array' => false,
'filters' => [],
],
[
'$id' => ID::custom('attempts'),
'type' => Database::VAR_INTEGER,
'format' => '',
'size' => 0,
'signed' => true,
'required' => false,
'default' => 0,
'array' => false,
'filters' => [],
],
],
'indexes' => [
[

View file

@ -1053,9 +1053,9 @@ App::put('/v1/projects/:projectId/webhooks/:webhookId')
->setAttribute('security', $security)
->setAttribute('httpUser', $httpUser)
->setAttribute('httpPass', $httpPass)
->setAttribute('errors', 0)
->setAttribute('status', true)
->setAttribute('logs', '');
->setAttribute('enabled', true)
->setAttribute('logs', '')
->setAttribute('attempts', 0);
$dbForConsole->updateDocument('webhooks', $webhook->getId(), $webhook);
$dbForConsole->deleteCachedDocument('projects', $project->getId());

View file

@ -12,6 +12,7 @@ use Utopia\Queue\Message;
class Webhooks extends Action
{
private array $errors = [];
private const failedAttemptsCount = 10;
public static function getName(): string
{
@ -27,7 +28,7 @@ class Webhooks extends Action
->desc('Webhooks worker')
->inject('message')
->inject('dbForConsole')
->callback(fn($message, Database $dbForConsole) => $this->action($message, $dbForConsole));
->callback(fn ($message, Database $dbForConsole) => $this->action($message, $dbForConsole));
}
/**
@ -50,7 +51,7 @@ class Webhooks extends Action
$user = new Document($payload['user'] ?? []);
foreach ($project->getAttribute('webhooks', []) as $webhook) {
if ($webhook->getAttribute('status') === true && array_intersect($webhook->getAttribute('events', []), $events)) {
if ($webhook->getAttribute('enabled') === true && array_intersect($webhook->getAttribute('events', []), $events)) {
$this->execute($events, $webhookPayload, $webhook, $user, $project, $dbForConsole);
}
}
@ -71,7 +72,7 @@ class Webhooks extends Action
*/
private function execute(array $events, string $payload, Document $webhook, Document $user, Document $project, Database $dbForConsole): void
{
if ($webhook->getAttribute('status') === false) {
if ($webhook->getAttribute('enabled') === false) {
return;
}
@ -120,13 +121,13 @@ class Webhooks extends Action
}
if (false === \curl_exec($ch)) {
$errorCount = $webhook->getAttribute('errors', 0) + 1;
$lastErrorLogs = \curl_error($ch) . ' in events ' . implode(', ', $events) . ' for webhook ' . $webhook->getAttribute('name');
$webhook->setAttribute('errors', $errorCount);
$errorCount = $dbForConsole->increaseDocumentAttribute('webhooks', $webhook->getId(), 'attempts', 1);
$lastErrorLogs = \curl_error($ch) . ' in events ' . implode(', ', $events);
$webhook->setAttribute('attempts', $errorCount);
$webhook->setAttribute('logs', $lastErrorLogs);
if ($errorCount > 9) {
$webhook->setAttribute('status', false);
if ($errorCount >= self::failedAttemptsCount) {
$webhook->setAttribute('enabled', false);
}
$dbForConsole->updateDocument('webhooks', $webhook->getId(), $webhook);

View file

@ -76,7 +76,18 @@ class Webhook extends Model
'default' => '',
'example' => 'ad3d581ca230e2b7059c545e5a',
])
;
->addRule('enabled', [
'type' => self::TYPE_BOOLEAN,
'description' => 'Indicates if this webhook is enabled.',
'default' => true,
'example' => true,
])
->addRule('logs', [
'type' => self::TYPE_STRING,
'description' => 'Webhooks last failed delivery attempt logs.',
'default' => '',
'example' => 'Failed to connect to remote server.',
]);
}
/**