From 13a879c2743fb1fecc1df0f112ccf9a0458411e7 Mon Sep 17 00:00:00 2001 From: shimon Date: Tue, 30 Jan 2024 11:19:10 +0200 Subject: [PATCH 1/2] Add count for messages(sms) metric --- app/init.php | 1 + src/Appwrite/Platform/Workers/Messaging.php | 25 +++++++++++++-------- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/app/init.php b/app/init.php index a32ab08344..80c3670eec 100644 --- a/app/init.php +++ b/app/init.php @@ -195,6 +195,7 @@ const FUNCTION_ALLOWLIST_HEADERS_RESPONSE = ['content-type', 'content-length']; // Usage metrics const METRIC_TEAMS = 'teams'; const METRIC_USERS = 'users'; +const METRIC_MESSAGES = 'messages'; const METRIC_SESSIONS = 'sessions'; const METRIC_DATABASES = 'databases'; const METRIC_COLLECTIONS = 'collections'; diff --git a/src/Appwrite/Platform/Workers/Messaging.php b/src/Appwrite/Platform/Workers/Messaging.php index 92f9e8fdf4..e50e58013e 100644 --- a/src/Appwrite/Platform/Workers/Messaging.php +++ b/src/Appwrite/Platform/Workers/Messaging.php @@ -5,6 +5,7 @@ namespace Appwrite\Platform\Workers; use Exception; use Utopia\App; use Utopia\CLI\Console; +use Utopia\Database\Document; use Utopia\DSN\DSN; use Utopia\Messaging\Messages\SMS; use Utopia\Messaging\Adapters\SMS\Mock; @@ -15,6 +16,7 @@ use Utopia\Messaging\Adapters\SMS\Twilio; use Utopia\Messaging\Adapters\SMS\Vonage; use Utopia\Platform\Action; use Utopia\Queue\Message; +use Appwrite\Event\Usage; class Messaging extends Action { @@ -43,15 +45,17 @@ class Messaging extends Action $this ->desc('Messaging worker') ->inject('message') - ->callback(fn($message) => $this->action($message)); + ->inject('queueForUsage') + ->callback(fn(Message $message, Usage $queueForUsage) => $this->action($message, $queueForUsage)); } /** * @param Message $message + * @param Usage $queueForUsage * @return void * @throws Exception */ - public function action(Message $message): void + public function action(Message $message, Usage $queueForUsage): void { $payload = $message->getPayload() ?? []; @@ -59,19 +63,17 @@ class Messaging extends Action throw new Exception('Project not set in payload'); } - Console::log($payload['project']['$id']); + $project = new Document($payload['project'] ?? []); + + Console::log($project->getId()); + $denyList = App::getEnv('_APP_SMS_PROJECTS_DENY_LIST', ''); $denyList = explode(',', $denyList); - if (in_array($payload['project']['$id'], $denyList)) { + if (in_array($project->getId(), $denyList)) { Console::error("Project is in the deny list. Skipping ..."); return; } - if (empty($payload)) { - Console::error('Payload arg not found'); - return; - } - if (empty($payload['recipient'])) { Console::error('Recipient arg not found'); return; @@ -112,6 +114,11 @@ class Messaging extends Action try { $sms->send($message); + + $queueForUsage + ->setProject($project) + ->addMetric(METRIC_MESSAGES, 1) + ->trigger(); } catch (\Exception $error) { throw new Exception('Error sending message: ' . $error->getMessage(), 500); } From bfe042dc3eab0e5297ddad7ef57da0acbd775601 Mon Sep 17 00:00:00 2001 From: shimon Date: Tue, 30 Jan 2024 13:29:15 +0200 Subject: [PATCH 2/2] fix --- src/Appwrite/Platform/Workers/Messaging.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Appwrite/Platform/Workers/Messaging.php b/src/Appwrite/Platform/Workers/Messaging.php index e50e58013e..1f3e29c8d5 100644 --- a/src/Appwrite/Platform/Workers/Messaging.php +++ b/src/Appwrite/Platform/Workers/Messaging.php @@ -59,13 +59,17 @@ class Messaging extends Action { $payload = $message->getPayload() ?? []; + if (empty($payload)) { + throw new Exception('Missing payload'); + } + if (empty($payload['project'])) { throw new Exception('Project not set in payload'); } $project = new Document($payload['project'] ?? []); - Console::log($project->getId()); + Console::log('Project: ' . $project->getId()); $denyList = App::getEnv('_APP_SMS_PROJECTS_DENY_LIST', ''); $denyList = explode(',', $denyList);