From 2c04c98801380b823a44d44f0b010e0280711f53 Mon Sep 17 00:00:00 2001 From: Khushboo Verma <43381712+vermakhushboo@users.noreply.github.com> Date: Mon, 20 Nov 2023 19:49:15 +0530 Subject: [PATCH] Renamed webhook variables --- app/config/collections.php | 32 +++++++++---------- app/controllers/api/projects.php | 6 ++-- src/Appwrite/Platform/Workers/Webhooks.php | 17 +++++----- .../Utopia/Response/Model/Webhook.php | 13 +++++++- 4 files changed, 40 insertions(+), 28 deletions(-) diff --git a/app/config/collections.php b/app/config/collections.php index 614a805d35..a11bd03eb1 100644 --- a/app/config/collections.php +++ b/app/config/collections.php @@ -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' => [ [ diff --git a/app/controllers/api/projects.php b/app/controllers/api/projects.php index c03f7ce165..42efaa1168 100644 --- a/app/controllers/api/projects.php +++ b/app/controllers/api/projects.php @@ -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()); diff --git a/src/Appwrite/Platform/Workers/Webhooks.php b/src/Appwrite/Platform/Workers/Webhooks.php index 128ab0de32..83ab54ae80 100644 --- a/src/Appwrite/Platform/Workers/Webhooks.php +++ b/src/Appwrite/Platform/Workers/Webhooks.php @@ -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); diff --git a/src/Appwrite/Utopia/Response/Model/Webhook.php b/src/Appwrite/Utopia/Response/Model/Webhook.php index 781bb9186d..7e8b259be8 100644 --- a/src/Appwrite/Utopia/Response/Model/Webhook.php +++ b/src/Appwrite/Utopia/Response/Model/Webhook.php @@ -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.', + ]); } /**