From de6fced412c8451d9ee2f2aaf1315cbb50f63a32 Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Thu, 16 Jan 2025 11:35:22 +0530 Subject: [PATCH 01/10] feat: optimise events payloads --- composer.lock | 32 ++++++++++---------- src/Appwrite/Event/Event.php | 5 ++- src/Appwrite/Event/Webhook.php | 6 +--- src/Appwrite/Platform/Workers/Builds.php | 7 +++-- src/Appwrite/Platform/Workers/Databases.php | 7 +++-- src/Appwrite/Platform/Workers/Deletes.php | 8 ++--- src/Appwrite/Platform/Workers/Messaging.php | 6 ++-- src/Appwrite/Platform/Workers/Migrations.php | 6 ++-- src/Appwrite/Platform/Workers/Usage.php | 10 +++--- src/Appwrite/Platform/Workers/UsageDump.php | 17 +---------- src/Appwrite/Platform/Workers/Webhooks.php | 8 ++--- 11 files changed, 50 insertions(+), 62 deletions(-) diff --git a/composer.lock b/composer.lock index b77915f0a4..e5c79ee1dc 100644 --- a/composer.lock +++ b/composer.lock @@ -1430,16 +1430,16 @@ }, { "name": "open-telemetry/gen-otlp-protobuf", - "version": "1.2.1", + "version": "1.5.0", "source": { "type": "git", "url": "https://github.com/opentelemetry-php/gen-otlp-protobuf.git", - "reference": "66c3b98e998a726691c92e6405a82e6e7b8b169d" + "reference": "585bafddd4ae6565de154610b10a787a455c9ba0" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/opentelemetry-php/gen-otlp-protobuf/zipball/66c3b98e998a726691c92e6405a82e6e7b8b169d", - "reference": "66c3b98e998a726691c92e6405a82e6e7b8b169d", + "url": "https://api.github.com/repos/opentelemetry-php/gen-otlp-protobuf/zipball/585bafddd4ae6565de154610b10a787a455c9ba0", + "reference": "585bafddd4ae6565de154610b10a787a455c9ba0", "shasum": "" }, "require": { @@ -1489,7 +1489,7 @@ "issues": "https://github.com/open-telemetry/opentelemetry-php/issues", "source": "https://github.com/open-telemetry/opentelemetry-php" }, - "time": "2024-10-30T11:49:49+00:00" + "time": "2025-01-15T23:07:07+00:00" }, { "name": "open-telemetry/sdk", @@ -4095,16 +4095,16 @@ }, { "name": "utopia-php/platform", - "version": "0.7.1", + "version": "0.7.2", "source": { "type": "git", "url": "https://github.com/utopia-php/platform.git", - "reference": "3433a0f1a54988f2a59c735f507745cb2c24638a" + "reference": "6f9243848f1c6466f6509fd01c7e18306a6d8caf" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/platform/zipball/3433a0f1a54988f2a59c735f507745cb2c24638a", - "reference": "3433a0f1a54988f2a59c735f507745cb2c24638a", + "url": "https://api.github.com/repos/utopia-php/platform/zipball/6f9243848f1c6466f6509fd01c7e18306a6d8caf", + "reference": "6f9243848f1c6466f6509fd01c7e18306a6d8caf", "shasum": "" }, "require": { @@ -4139,9 +4139,9 @@ ], "support": { "issues": "https://github.com/utopia-php/platform/issues", - "source": "https://github.com/utopia-php/platform/tree/0.7.1" + "source": "https://github.com/utopia-php/platform/tree/0.7.2" }, - "time": "2024-10-22T10:27:49+00:00" + "time": "2025-01-15T05:56:26+00:00" }, { "name": "utopia-php/pools", @@ -5126,16 +5126,16 @@ }, { "name": "laravel/pint", - "version": "v1.19.0", + "version": "v1.20.0", "source": { "type": "git", "url": "https://github.com/laravel/pint.git", - "reference": "8169513746e1bac70c85d6ea1524d9225d4886f0" + "reference": "53072e8ea22213a7ed168a8a15b96fbb8b82d44b" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/laravel/pint/zipball/8169513746e1bac70c85d6ea1524d9225d4886f0", - "reference": "8169513746e1bac70c85d6ea1524d9225d4886f0", + "url": "https://api.github.com/repos/laravel/pint/zipball/53072e8ea22213a7ed168a8a15b96fbb8b82d44b", + "reference": "53072e8ea22213a7ed168a8a15b96fbb8b82d44b", "shasum": "" }, "require": { @@ -5188,7 +5188,7 @@ "issues": "https://github.com/laravel/pint/issues", "source": "https://github.com/laravel/pint" }, - "time": "2024-12-30T16:20:10+00:00" + "time": "2025-01-14T16:20:53+00:00" }, { "name": "matthiasmullie/minify", diff --git a/src/Appwrite/Event/Event.php b/src/Appwrite/Event/Event.php index e3a2e394cf..a830fd66b1 100644 --- a/src/Appwrite/Event/Event.php +++ b/src/Appwrite/Event/Event.php @@ -136,7 +136,10 @@ class Event */ public function setProject(Document $project): self { - $this->project = $project; + $this->project = new Document([ + '$id' => $project->getId(), + '$internalId' => $project->getInternalId() + ]); return $this; } diff --git a/src/Appwrite/Event/Webhook.php b/src/Appwrite/Event/Webhook.php index 36c6923cae..052d8f0fb9 100644 --- a/src/Appwrite/Event/Webhook.php +++ b/src/Appwrite/Event/Webhook.php @@ -20,11 +20,7 @@ class Webhook extends Event { /** Filter out context and trim project to keep the payload small */ $this->context = []; - $this->project = new Document([ - '$id' => $this->project->getId(), - '$internalId' => $this->project->getInternalId(), - ]); - + return parent::trigger(); } } diff --git a/src/Appwrite/Platform/Workers/Builds.php b/src/Appwrite/Platform/Workers/Builds.php index bef78a7514..4f5d6eb694 100644 --- a/src/Appwrite/Platform/Workers/Builds.php +++ b/src/Appwrite/Platform/Workers/Builds.php @@ -46,6 +46,7 @@ class Builds extends Action $this ->desc('Builds worker') ->inject('message') + ->inject('project') ->inject('dbForPlatform') ->inject('queueForEvents') ->inject('queueForFunctions') @@ -54,11 +55,12 @@ class Builds extends Action ->inject('dbForProject') ->inject('deviceForFunctions') ->inject('log') - ->callback(fn ($message, Database $dbForPlatform, Event $queueForEvents, Func $queueForFunctions, Usage $usage, Cache $cache, Database $dbForProject, Device $deviceForFunctions, Log $log) => $this->action($message, $dbForPlatform, $queueForEvents, $queueForFunctions, $usage, $cache, $dbForProject, $deviceForFunctions, $log)); + ->callback(fn ($message, Document $project, Database $dbForPlatform, Event $queueForEvents, Func $queueForFunctions, Usage $usage, Cache $cache, Database $dbForProject, Device $deviceForFunctions, Log $log) => $this->action($message, $project, $dbForPlatform, $queueForEvents, $queueForFunctions, $usage, $cache, $dbForProject, $deviceForFunctions, $log)); } /** * @param Message $message + * @param Document $project * @param Database $dbForPlatform * @param Event $queueForEvents * @param Func $queueForFunctions @@ -70,7 +72,7 @@ class Builds extends Action * @return void * @throws \Utopia\Database\Exception */ - public function action(Message $message, Database $dbForPlatform, Event $queueForEvents, Func $queueForFunctions, Usage $queueForUsage, Cache $cache, Database $dbForProject, Device $deviceForFunctions, Log $log): void + public function action(Message $message, Document $project, Database $dbForPlatform, Event $queueForEvents, Func $queueForFunctions, Usage $queueForUsage, Cache $cache, Database $dbForProject, Device $deviceForFunctions, Log $log): void { $payload = $message->getPayload() ?? []; @@ -79,7 +81,6 @@ class Builds extends Action } $type = $payload['type'] ?? ''; - $project = new Document($payload['project'] ?? []); $resource = new Document($payload['resource'] ?? []); $deployment = new Document($payload['deployment'] ?? []); $template = new Document($payload['template'] ?? []); diff --git a/src/Appwrite/Platform/Workers/Databases.php b/src/Appwrite/Platform/Workers/Databases.php index 5b73e6a75c..9345f31165 100644 --- a/src/Appwrite/Platform/Workers/Databases.php +++ b/src/Appwrite/Platform/Workers/Databases.php @@ -34,21 +34,23 @@ class Databases extends Action $this ->desc('Databases worker') ->inject('message') + ->inject('project') ->inject('dbForPlatform') ->inject('dbForProject') ->inject('log') - ->callback(fn (Message $message, Database $dbForPlatform, Database $dbForProject, Log $log) => $this->action($message, $dbForPlatform, $dbForProject, $log)); + ->callback(fn (Message $message, Document $project, Database $dbForPlatform, Database $dbForProject, Log $log) => $this->action($message, $project, $dbForPlatform, $dbForProject, $log)); } /** * @param Message $message + * @param Document $project * @param Database $dbForPlatform * @param Database $dbForProject * @param Log $log * @return void * @throws \Exception */ - public function action(Message $message, Database $dbForPlatform, Database $dbForProject, Log $log): void + public function action(Message $message, Document $project, Database $dbForPlatform, Database $dbForProject, Log $log): void { $payload = $message->getPayload() ?? []; @@ -57,7 +59,6 @@ class Databases extends Action } $type = $payload['type']; - $project = new Document($payload['project']); $collection = new Document($payload['collection'] ?? []); $document = new Document($payload['document'] ?? []); $database = new Document($payload['database'] ?? []); diff --git a/src/Appwrite/Platform/Workers/Deletes.php b/src/Appwrite/Platform/Workers/Deletes.php index 9aaf19f412..35ee50c791 100644 --- a/src/Appwrite/Platform/Workers/Deletes.php +++ b/src/Appwrite/Platform/Workers/Deletes.php @@ -43,6 +43,7 @@ class Deletes extends Action $this ->desc('Deletes worker') ->inject('message') + ->inject('project') ->inject('dbForPlatform') ->inject('getProjectDB') ->inject('timelimit') @@ -55,8 +56,8 @@ class Deletes extends Action ->inject('auditRetention') ->inject('log') ->callback( - fn ($message, $dbForPlatform, callable $getProjectDB, callable $timelimit, Device $deviceForFiles, Device $deviceForFunctions, Device $deviceForBuilds, Device $deviceForCache, CertificatesAdapter $certificates, string $executionRetention, string $auditRetention, Log $log) => - $this->action($message, $dbForPlatform, $getProjectDB, $timelimit, $deviceForFiles, $deviceForFunctions, $deviceForBuilds, $deviceForCache, $certificates, $executionRetention, $auditRetention, $log) + fn ($message, Document $project, Database $dbForPlatform, callable $getProjectDB, callable $timelimit, Device $deviceForFiles, Device $deviceForFunctions, Device $deviceForBuilds, Device $deviceForCache, CertificatesAdapter $certificates, string $executionRetention, string $auditRetention, Log $log) => + $this->action($message, $project, $dbForPlatform, $getProjectDB, $timelimit, $deviceForFiles, $deviceForFunctions, $deviceForBuilds, $deviceForCache, $certificates, $executionRetention, $auditRetention, $log) ); } @@ -64,7 +65,7 @@ class Deletes extends Action * @throws Exception * @throws Throwable */ - public function action(Message $message, Database $dbForPlatform, callable $getProjectDB, callable $timelimit, Device $deviceForFiles, Device $deviceForFunctions, Device $deviceForBuilds, Device $deviceForCache, CertificatesAdapter $certificates, string $executionRetention, string $auditRetention, Log $log): void + public function action(Message $message, Document $project, Database $dbForPlatform, callable $getProjectDB, callable $timelimit, Device $deviceForFiles, Device $deviceForFunctions, Device $deviceForBuilds, Device $deviceForCache, CertificatesAdapter $certificates, string $executionRetention, string $auditRetention, Log $log): void { $payload = $message->getPayload() ?? []; @@ -78,7 +79,6 @@ class Deletes extends Action $resource = $payload['resource'] ?? null; $resourceType = $payload['resourceType'] ?? null; $document = new Document($payload['document'] ?? []); - $project = new Document($payload['project'] ?? []); $log->addTag('projectId', $project->getId()); $log->addTag('type', $type); diff --git a/src/Appwrite/Platform/Workers/Messaging.php b/src/Appwrite/Platform/Workers/Messaging.php index 1ff032c3e1..febc4a4e55 100644 --- a/src/Appwrite/Platform/Workers/Messaging.php +++ b/src/Appwrite/Platform/Workers/Messaging.php @@ -58,15 +58,17 @@ class Messaging extends Action $this ->desc('Messaging worker') ->inject('message') + ->inject('project') ->inject('log') ->inject('dbForProject') ->inject('deviceForFiles') ->inject('queueForUsage') - ->callback(fn (Message $message, Log $log, Database $dbForProject, Device $deviceForFiles, Usage $queueForUsage) => $this->action($message, $log, $dbForProject, $deviceForFiles, $queueForUsage)); + ->callback(fn (Message $message, Document $project, Log $log, Database $dbForProject, Device $deviceForFiles, Usage $queueForUsage) => $this->action($message, $project, $log, $dbForProject, $deviceForFiles, $queueForUsage)); } /** * @param Message $message + * @param Document $project * @param Log $log * @param Database $dbForProject * @param Device $deviceForFiles @@ -76,6 +78,7 @@ class Messaging extends Action */ public function action( Message $message, + Document $project, Log $log, Database $dbForProject, Device $deviceForFiles, @@ -89,7 +92,6 @@ class Messaging extends Action } $type = $payload['type'] ?? ''; - $project = new Document($payload['project'] ?? []); switch ($type) { case MESSAGE_SEND_TYPE_INTERNAL: diff --git a/src/Appwrite/Platform/Workers/Migrations.php b/src/Appwrite/Platform/Workers/Migrations.php index f6af0eb5f2..078c9fa0ff 100644 --- a/src/Appwrite/Platform/Workers/Migrations.php +++ b/src/Appwrite/Platform/Workers/Migrations.php @@ -51,16 +51,17 @@ class Migrations extends Action $this ->desc('Migrations worker') ->inject('message') + ->inject('project') ->inject('dbForProject') ->inject('dbForPlatform') ->inject('logError') - ->callback(fn (Message $message, Database $dbForProject, Database $dbForPlatform, callable $logError) => $this->action($message, $dbForProject, $dbForPlatform, $logError)); + ->callback(fn (Message $message, Document $project, Database $dbForProject, Database $dbForPlatform, callable $logError) => $this->action($message, $project, $dbForProject, $dbForPlatform, $logError)); } /** * @throws Exception */ - public function action(Message $message, Database $dbForProject, Database $dbForPlatform, callable $logError): void + public function action(Message $message, Document $project, Database $dbForProject, Database $dbForPlatform, callable $logError): void { $payload = $message->getPayload() ?? []; @@ -69,7 +70,6 @@ class Migrations extends Action } $events = $payload['events'] ?? []; - $project = new Document($payload['project'] ?? []); $migration = new Document($payload['migration'] ?? []); if ($project->getId() === 'console') { diff --git a/src/Appwrite/Platform/Workers/Usage.php b/src/Appwrite/Platform/Workers/Usage.php index 8199fe73a7..be916f33e9 100644 --- a/src/Appwrite/Platform/Workers/Usage.php +++ b/src/Appwrite/Platform/Workers/Usage.php @@ -34,10 +34,11 @@ class Usage extends Action $this ->desc('Usage worker') ->inject('message') + ->inject('project') ->inject('getProjectDB') ->inject('queueForUsageDump') - ->callback(function (Message $message, callable $getProjectDB, UsageDump $queueForUsageDump) { - $this->action($message, $getProjectDB, $queueForUsageDump); + ->callback(function (Message $message, Document $project, callable $getProjectDB, UsageDump $queueForUsageDump) { + $this->action($message, $project, $getProjectDB, $queueForUsageDump); }); $this->aggregationInterval = (int) System::getEnv('_APP_USAGE_AGGREGATION_INTERVAL', '20'); @@ -46,21 +47,20 @@ class Usage extends Action /** * @param Message $message + * @param Document $project * @param callable $getProjectDB * @param UsageDump $queueForUsageDump * @return void * @throws \Utopia\Database\Exception * @throws Exception */ - public function action(Message $message, callable $getProjectDB, UsageDump $queueForUsageDump): void + public function action(Message $message, Document $project, callable $getProjectDB, UsageDump $queueForUsageDump): void { $payload = $message->getPayload() ?? []; if (empty($payload)) { throw new Exception('Missing payload'); } - $document = $payload['project'] ?? []; - $project = new Document($document); if (empty($project->getAttribute('database'))) { var_dump($payload); diff --git a/src/Appwrite/Platform/Workers/UsageDump.php b/src/Appwrite/Platform/Workers/UsageDump.php index 3e50ba0363..2f1d13f29a 100644 --- a/src/Appwrite/Platform/Workers/UsageDump.php +++ b/src/Appwrite/Platform/Workers/UsageDump.php @@ -59,26 +59,11 @@ class UsageDump extends Action foreach ($payload['stats'] ?? [] as $stats) { - //$project = new Document($stats['project'] ?? []); - - /** - * Start temp bug fallback - */ - $document = $stats['project'] ?? []; - if (!empty($document['$uid'])) { - $document['$id'] = $document['$uid']; - } - - $project = new Document($document); - - if (empty($project->getAttribute('database'))) { - continue; - } + $project = new Document($stats['project'] ?? []); /** * End temp bug fallback */ - $numberOfKeys = !empty($stats['keys']) ? count($stats['keys']) : 0; $receivedAt = $stats['receivedAt'] ?? 'NONE'; if ($numberOfKeys === 0) { diff --git a/src/Appwrite/Platform/Workers/Webhooks.php b/src/Appwrite/Platform/Workers/Webhooks.php index 50f64ea02d..a76e4f17b0 100644 --- a/src/Appwrite/Platform/Workers/Webhooks.php +++ b/src/Appwrite/Platform/Workers/Webhooks.php @@ -32,22 +32,24 @@ class Webhooks extends Action $this ->desc('Webhooks worker') ->inject('message') + ->inject('project') ->inject('dbForPlatform') ->inject('queueForMails') ->inject('queueForUsage') ->inject('log') - ->callback(fn (Message $message, Database $dbForPlatform, Mail $queueForMails, Usage $queueForUsage, Log $log) => $this->action($message, $dbForPlatform, $queueForMails, $queueForUsage, $log)); + ->callback(fn (Message $message, Document $project, Database $dbForPlatform, Mail $queueForMails, Usage $queueForUsage, Log $log) => $this->action($message, $project, $dbForPlatform, $queueForMails, $queueForUsage, $log)); } /** * @param Message $message + * @param Document $project * @param Database $dbForPlatform * @param Mail $queueForMails * @param Log $log * @return void * @throws Exception */ - public function action(Message $message, Database $dbForPlatform, Mail $queueForMails, Usage $queueForUsage, Log $log): void + public function action(Message $message, Document $project, Database $dbForPlatform, Mail $queueForMails, Usage $queueForUsage, Log $log): void { $this->errors = []; $payload = $message->getPayload() ?? []; @@ -60,8 +62,6 @@ class Webhooks extends Action $webhookPayload = json_encode($payload['payload']); $user = new Document($payload['user'] ?? []); - $project = new Document($payload['project']); - $project = $dbForPlatform->getDocument('projects', $project->getId()); $log->addTag('projectId', $project->getId()); foreach ($project->getAttribute('webhooks', []) as $webhook) { From 8ea1f436c1dd501b6d5b6469ab18d6b453e9f6a8 Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Thu, 16 Jan 2025 11:37:02 +0530 Subject: [PATCH 02/10] feat: linter --- src/Appwrite/Event/Webhook.php | 3 +-- src/Appwrite/Platform/Workers/Usage.php | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Appwrite/Event/Webhook.php b/src/Appwrite/Event/Webhook.php index 052d8f0fb9..c7035070b8 100644 --- a/src/Appwrite/Event/Webhook.php +++ b/src/Appwrite/Event/Webhook.php @@ -2,7 +2,6 @@ namespace Appwrite\Event; -use Utopia\Database\Document; use Utopia\Queue\Connection; class Webhook extends Event @@ -20,7 +19,7 @@ class Webhook extends Event { /** Filter out context and trim project to keep the payload small */ $this->context = []; - + return parent::trigger(); } } diff --git a/src/Appwrite/Platform/Workers/Usage.php b/src/Appwrite/Platform/Workers/Usage.php index be916f33e9..3687eeab67 100644 --- a/src/Appwrite/Platform/Workers/Usage.php +++ b/src/Appwrite/Platform/Workers/Usage.php @@ -34,7 +34,7 @@ class Usage extends Action $this ->desc('Usage worker') ->inject('message') - ->inject('project') + ->inject('project') ->inject('getProjectDB') ->inject('queueForUsageDump') ->callback(function (Message $message, Document $project, callable $getProjectDB, UsageDump $queueForUsageDump) { From e8f97baf262531bf45020a082302103fc2091383 Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Thu, 16 Jan 2025 13:14:47 +0530 Subject: [PATCH 03/10] fix: incorrect project payloads --- app/init.php | 1 + src/Appwrite/Event/Audit.php | 2 + src/Appwrite/Event/Build.php | 2 + src/Appwrite/Event/Certificate.php | 2 + src/Appwrite/Event/Database.php | 2 + src/Appwrite/Event/Delete.php | 2 + src/Appwrite/Event/Event.php | 18 ++++++++- src/Appwrite/Event/Func.php | 2 + src/Appwrite/Event/Mail.php | 2 + src/Appwrite/Event/Messaging.php | 2 + src/Appwrite/Event/Migration.php | 2 + src/Appwrite/Event/Usage.php | 4 +- src/Appwrite/Event/UsageDump.php | 2 + src/Appwrite/Event/Webhook.php | 1 - src/Appwrite/Platform/Tasks/Maintenance.php | 44 ++------------------- src/Appwrite/Platform/Workers/Deletes.php | 12 ++++++ 16 files changed, 55 insertions(+), 45 deletions(-) diff --git a/app/init.php b/app/init.php index df2a66aa00..df0e9d3a08 100644 --- a/app/init.php +++ b/app/init.php @@ -202,6 +202,7 @@ const DELETE_TYPE_TOPIC = 'topic'; const DELETE_TYPE_TARGET = 'target'; const DELETE_TYPE_EXPIRED_TARGETS = 'invalid_targets'; const DELETE_TYPE_SESSION_TARGETS = 'session_targets'; +const DELETE_TYPE_MAINTENANCE = 'maintenance'; // Message types const MESSAGE_SEND_TYPE_INTERNAL = 'internal'; diff --git a/src/Appwrite/Event/Audit.php b/src/Appwrite/Event/Audit.php index 406f64b370..5cbe379666 100644 --- a/src/Appwrite/Event/Audit.php +++ b/src/Appwrite/Event/Audit.php @@ -150,6 +150,8 @@ class Audit extends Event return false; } + $this->trimFields(); + $client = new Client($this->queue, $this->connection); return $client->enqueue([ diff --git a/src/Appwrite/Event/Build.php b/src/Appwrite/Event/Build.php index 1fbf20a9f9..bdbcf6826c 100644 --- a/src/Appwrite/Event/Build.php +++ b/src/Appwrite/Event/Build.php @@ -116,6 +116,8 @@ class Build extends Event return false; } + $this->trimFields(); + $client = new Client($this->queue, $this->connection); return $client->enqueue([ diff --git a/src/Appwrite/Event/Certificate.php b/src/Appwrite/Event/Certificate.php index 5d30c3d5ac..2629653c59 100644 --- a/src/Appwrite/Event/Certificate.php +++ b/src/Appwrite/Event/Certificate.php @@ -78,6 +78,8 @@ class Certificate extends Event return false; } + $this->trimFields(); + $client = new Client($this->queue, $this->connection); return $client->enqueue([ diff --git a/src/Appwrite/Event/Database.php b/src/Appwrite/Event/Database.php index 1b0ea6851c..e78fe967e7 100644 --- a/src/Appwrite/Event/Database.php +++ b/src/Appwrite/Event/Database.php @@ -121,6 +121,8 @@ class Database extends Event $this->setQueue($dsn->getHost()); + $this->trimFields(); + $client = new Client($this->queue, $this->connection); try { diff --git a/src/Appwrite/Event/Delete.php b/src/Appwrite/Event/Delete.php index 1a4c9318e3..2874043163 100644 --- a/src/Appwrite/Event/Delete.php +++ b/src/Appwrite/Event/Delete.php @@ -144,6 +144,8 @@ class Delete extends Event return false; } + $this->trimFields(); + $client = new Client($this->queue, $this->connection); return $client->enqueue([ diff --git a/src/Appwrite/Event/Event.php b/src/Appwrite/Event/Event.php index a830fd66b1..4029502ea7 100644 --- a/src/Appwrite/Event/Event.php +++ b/src/Appwrite/Event/Event.php @@ -135,10 +135,22 @@ class Event * @return self */ public function setProject(Document $project): self + { + $this->project = $project; + return $this; + } + + /** + * Trims the fields of the project document to only include the necessary fields. + * + * @return self + */ + public function trimFields(): self { $this->project = new Document([ - '$id' => $project->getId(), - '$internalId' => $project->getInternalId() + '$id' => $this->project->getId(), + '$internalId' => $this->project->getInternalId(), + 'database' => $this->project->getAttribute('database') ]); return $this; @@ -327,6 +339,8 @@ class Event return false; } + $this->trimFields(); + $client = new Client($this->queue, $this->connection); return $client->enqueue([ diff --git a/src/Appwrite/Event/Func.php b/src/Appwrite/Event/Func.php index 0ad639a9f5..7c6a2e9194 100644 --- a/src/Appwrite/Event/Func.php +++ b/src/Appwrite/Event/Func.php @@ -217,6 +217,8 @@ class Func extends Event return false; } + $this->trimFields(); + $client = new Client($this->queue, $this->connection); $events = $this->getEvent() ? Event::generateEvents($this->getEvent(), $this->getParams()) : null; diff --git a/src/Appwrite/Event/Mail.php b/src/Appwrite/Event/Mail.php index a0fca75688..0db576ba68 100644 --- a/src/Appwrite/Event/Mail.php +++ b/src/Appwrite/Event/Mail.php @@ -408,6 +408,8 @@ class Mail extends Event return false; } + $this->trimFields(); + $client = new Client($this->queue, $this->connection); return $client->enqueue([ diff --git a/src/Appwrite/Event/Messaging.php b/src/Appwrite/Event/Messaging.php index 755b8c9158..ac2df7f92a 100644 --- a/src/Appwrite/Event/Messaging.php +++ b/src/Appwrite/Event/Messaging.php @@ -186,6 +186,8 @@ class Messaging extends Event return false; } + $this->trimFields(); + $client = new Client($this->queue, $this->connection); return $client->enqueue([ diff --git a/src/Appwrite/Event/Migration.php b/src/Appwrite/Event/Migration.php index 789b8e2160..376264bce3 100644 --- a/src/Appwrite/Event/Migration.php +++ b/src/Appwrite/Event/Migration.php @@ -79,6 +79,8 @@ class Migration extends Event return false; } + $this->trimFields(); + $client = new Client($this->queue, $this->connection); return $client->enqueue([ diff --git a/src/Appwrite/Event/Usage.php b/src/Appwrite/Event/Usage.php index 89e900d2ab..f17c048a2e 100644 --- a/src/Appwrite/Event/Usage.php +++ b/src/Appwrite/Event/Usage.php @@ -62,10 +62,12 @@ class Usage extends Event return false; } + $this->trimFields(); + $client = new Client($this->queue, $this->connection); $result = $client->enqueue([ - 'project' => $this->getProject(), + 'project' => $this->project, 'reduce' => $this->reduce, 'metrics' => $this->metrics, ]); diff --git a/src/Appwrite/Event/UsageDump.php b/src/Appwrite/Event/UsageDump.php index 2998e4e104..83206e4add 100644 --- a/src/Appwrite/Event/UsageDump.php +++ b/src/Appwrite/Event/UsageDump.php @@ -42,6 +42,8 @@ class UsageDump extends Event return false; } + $this->trimFields(); + $client = new Client($this->queue, $this->connection); return $client->enqueue([ diff --git a/src/Appwrite/Event/Webhook.php b/src/Appwrite/Event/Webhook.php index c7035070b8..9d14dcdc3d 100644 --- a/src/Appwrite/Event/Webhook.php +++ b/src/Appwrite/Event/Webhook.php @@ -19,7 +19,6 @@ class Webhook extends Event { /** Filter out context and trim project to keep the payload small */ $this->context = []; - return parent::trigger(); } } diff --git a/src/Appwrite/Platform/Tasks/Maintenance.php b/src/Appwrite/Platform/Tasks/Maintenance.php index c789cbdaac..a7558934b5 100644 --- a/src/Appwrite/Platform/Tasks/Maintenance.php +++ b/src/Appwrite/Platform/Tasks/Maintenance.php @@ -64,11 +64,9 @@ class Maintenance extends Action */ protected function notifyProjects(Delete $queueForDeletes, int $usageStatsRetentionHourly): void { - $this->notifyDeleteTargets($queueForDeletes); - $this->notifyDeleteExecutionLogs($queueForDeletes); - $this->notifyDeleteAuditLogs($queueForDeletes); - $this->notifyDeleteUsageStats($usageStatsRetentionHourly, $queueForDeletes); - $this->notifyDeleteExpiredSessions($queueForDeletes); + $queueForDeletes + ->setUsageRetentionHourlyDateTime(DateTime::addSeconds(new \DateTime(), -1 * $usageStatsRetentionHourly)) + ->trigger(); } protected function foreachProject(Database $dbForPlatform, callable $callback): void @@ -98,28 +96,6 @@ class Maintenance extends Action Console::info("Found {$count} projects " . ($executionEnd - $executionStart) . " seconds"); } - private function notifyDeleteExecutionLogs(Delete $queueForDeletes): void - { - $queueForDeletes - ->setType(DELETE_TYPE_EXECUTIONS) - ->trigger(); - } - - private function notifyDeleteAuditLogs(Delete $queueForDeletes): void - { - $queueForDeletes - ->setType(DELETE_TYPE_AUDIT) - ->trigger(); - } - - private function notifyDeleteUsageStats(int $usageStatsRetentionHourly, Delete $queueForDeletes): void - { - $queueForDeletes - ->setType(DELETE_TYPE_USAGE) - ->setUsageRetentionHourlyDateTime(DateTime::addSeconds(new \DateTime(), -1 * $usageStatsRetentionHourly)) - ->trigger(); - } - private function notifyDeleteConnections(Delete $queueForDeletes): void { $queueForDeletes @@ -128,13 +104,6 @@ class Maintenance extends Action ->trigger(); } - private function notifyDeleteExpiredSessions(Delete $queueForDeletes): void - { - $queueForDeletes - ->setType(DELETE_TYPE_SESSIONS) - ->trigger(); - } - private function renewCertificates(Database $dbForPlatform, Certificate $queueForCertificate): void { $time = DateTime::now(); @@ -177,11 +146,4 @@ class Maintenance extends Action ->setDatetime(DateTime::addSeconds(new \DateTime(), -1 * $interval)) ->trigger(); } - - private function notifyDeleteTargets(Delete $queueForDeletes): void - { - $queueForDeletes - ->setType(DELETE_TYPE_EXPIRED_TARGETS) - ->trigger(); - } } diff --git a/src/Appwrite/Platform/Workers/Deletes.php b/src/Appwrite/Platform/Workers/Deletes.php index 35ee50c791..45e7725c9b 100644 --- a/src/Appwrite/Platform/Workers/Deletes.php +++ b/src/Appwrite/Platform/Workers/Deletes.php @@ -153,11 +153,23 @@ class Deletes extends Action case DELETE_TYPE_SESSION_TARGETS: $this->deleteSessionTargets($project, $getProjectDB, $document); break; + case DELETE_TYPE_MAINTENANCE: + $this->performMaintenance($project, $getProjectDB, $executionRetention, $auditRetention, $hourlyUsageRetentionDatetime); + break; default: throw new \Exception('No delete operation for type: ' . \strval($type)); } } + private function performMaintenance(Document $project, callable $getProjectDB, string $executionRetention, string $auditRetention, string $hourlyUsageRetentionDatetime): void + { + $this->deleteExpiredTargets($project, $getProjectDB); + $this->deleteExecutionLogs($project, $getProjectDB, $executionRetention); + $this->deleteAuditLogs($project, $getProjectDB, $auditRetention); + $this->deleteUsageStats($project, $getProjectDB, $hourlyUsageRetentionDatetime); + $this->deleteExpiredSessions($project, $getProjectDB); + } + /** * @param Database $dbForPlatform * @param callable $getProjectDB From 2a0894f3d1df1015f3a7d2f5acc04cccbb4b2096 Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Thu, 16 Jan 2025 13:29:08 +0530 Subject: [PATCH 04/10] fix: incorrect project payloads --- src/Appwrite/Event/Event.php | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/Appwrite/Event/Event.php b/src/Appwrite/Event/Event.php index 4029502ea7..4efc138ec6 100644 --- a/src/Appwrite/Event/Event.php +++ b/src/Appwrite/Event/Event.php @@ -147,11 +147,13 @@ class Event */ public function trimFields(): self { - $this->project = new Document([ - '$id' => $this->project->getId(), - '$internalId' => $this->project->getInternalId(), - 'database' => $this->project->getAttribute('database') - ]); + if ($this->project) { + $this->project = new Document([ + '$id' => $this->project->getId(), + '$internalId' => $this->project->getInternalId(), + 'database' => $this->project->getAttribute('database') + ]); + } return $this; } From f52b2d5dcde55de4928740371e6d39b5dce0262a Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Thu, 16 Jan 2025 16:59:30 +0530 Subject: [PATCH 05/10] chore: review comments --- src/Appwrite/Platform/Workers/Deletes.php | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/src/Appwrite/Platform/Workers/Deletes.php b/src/Appwrite/Platform/Workers/Deletes.php index 45e7725c9b..539bbd61f9 100644 --- a/src/Appwrite/Platform/Workers/Deletes.php +++ b/src/Appwrite/Platform/Workers/Deletes.php @@ -154,22 +154,17 @@ class Deletes extends Action $this->deleteSessionTargets($project, $getProjectDB, $document); break; case DELETE_TYPE_MAINTENANCE: - $this->performMaintenance($project, $getProjectDB, $executionRetention, $auditRetention, $hourlyUsageRetentionDatetime); + $this->deleteExpiredTargets($project, $getProjectDB); + $this->deleteExecutionLogs($project, $getProjectDB, $executionRetention); + $this->deleteAuditLogs($project, $getProjectDB, $auditRetention); + $this->deleteUsageStats($project, $getProjectDB, $hourlyUsageRetentionDatetime); + $this->deleteExpiredSessions($project, $getProjectDB); break; default: throw new \Exception('No delete operation for type: ' . \strval($type)); } } - private function performMaintenance(Document $project, callable $getProjectDB, string $executionRetention, string $auditRetention, string $hourlyUsageRetentionDatetime): void - { - $this->deleteExpiredTargets($project, $getProjectDB); - $this->deleteExecutionLogs($project, $getProjectDB, $executionRetention); - $this->deleteAuditLogs($project, $getProjectDB, $auditRetention); - $this->deleteUsageStats($project, $getProjectDB, $hourlyUsageRetentionDatetime); - $this->deleteExpiredSessions($project, $getProjectDB); - } - /** * @param Database $dbForPlatform * @param callable $getProjectDB From 8bdafb46d6d39a23c5ad6624758de129c953b4d6 Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Fri, 17 Jan 2025 10:38:39 +0530 Subject: [PATCH 06/10] chore: review comments & refactoring --- app/controllers/api/functions.php | 4 ++ phpunit.xml | 2 +- src/Appwrite/Event/Audit.php | 20 ++----- src/Appwrite/Event/Build.php | 20 ++----- src/Appwrite/Event/Certificate.php | 20 ++----- src/Appwrite/Event/Database.php | 51 +++++++--------- src/Appwrite/Event/Delete.php | 21 ++----- src/Appwrite/Event/Event.php | 58 ++++++++++++------- src/Appwrite/Event/Func.php | 43 ++------------ src/Appwrite/Event/Mail.php | 20 ++----- src/Appwrite/Event/Messaging.php | 21 ++----- src/Appwrite/Event/Migration.php | 20 ++----- src/Appwrite/Event/Realtime.php | 3 + src/Appwrite/Event/Usage.php | 33 +++++------ src/Appwrite/Event/UsageDump.php | 19 ++---- src/Appwrite/Event/Webhook.php | 15 +++-- .../Functions/FunctionsCustomServerTest.php | 2 +- 17 files changed, 138 insertions(+), 234 deletions(-) diff --git a/app/controllers/api/functions.php b/app/controllers/api/functions.php index d4d5fc64cf..f459875fca 100644 --- a/app/controllers/api/functions.php +++ b/app/controllers/api/functions.php @@ -400,6 +400,10 @@ App::post('/v1/functions') $allEvents = Event::generateEvents('rules.[ruleId].create', [ 'ruleId' => $rule->getId(), ]); + + + var_dump("-------------------- Create Function ---------------"); + var_dump($project); $target = Realtime::fromPayload( // Pass first, most verbose event pattern event: $allEvents[0], diff --git a/phpunit.xml b/phpunit.xml index 4c4e55ea4e..598b730908 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -6,7 +6,7 @@ convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" - stopOnFailure="false" + stopOnFailure="true" > diff --git a/src/Appwrite/Event/Audit.php b/src/Appwrite/Event/Audit.php index 5cbe379666..4b9aa9f5c5 100644 --- a/src/Appwrite/Event/Audit.php +++ b/src/Appwrite/Event/Audit.php @@ -2,7 +2,6 @@ namespace Appwrite\Event; -use Utopia\Queue\Client; use Utopia\Queue\Connection; class Audit extends Event @@ -139,22 +138,13 @@ class Audit extends Event } /** - * Executes the event and sends it to the audit worker. + * Prepare payload for queue. * - * @return string|bool - * @throws \InvalidArgumentException + * @return array */ - public function trigger(): string|bool + protected function preparePayload(): array { - if ($this->paused) { - return false; - } - - $this->trimFields(); - - $client = new Client($this->queue, $this->connection); - - return $client->enqueue([ + return [ 'project' => $this->project, 'user' => $this->user, 'payload' => $this->payload, @@ -164,6 +154,6 @@ class Audit extends Event 'userAgent' => $this->userAgent, 'event' => $this->event, 'hostname' => $this->hostname - ]); + ]; } } diff --git a/src/Appwrite/Event/Build.php b/src/Appwrite/Event/Build.php index bdbcf6826c..831adf8e41 100644 --- a/src/Appwrite/Event/Build.php +++ b/src/Appwrite/Event/Build.php @@ -3,7 +3,6 @@ namespace Appwrite\Event; use Utopia\Database\Document; -use Utopia\Queue\Client; use Utopia\Queue\Connection; class Build extends Event @@ -105,28 +104,19 @@ class Build extends Event } /** - * Executes the function event and sends it to the functions worker. + * Prepare payload for queue. * - * @return string|bool - * @throws \InvalidArgumentException + * @return array */ - public function trigger(): string|bool + protected function preparePayload(): array { - if ($this->paused) { - return false; - } - - $this->trimFields(); - - $client = new Client($this->queue, $this->connection); - - return $client->enqueue([ + return [ 'project' => $this->project, 'resource' => $this->resource, 'deployment' => $this->deployment, 'type' => $this->type, 'template' => $this->template - ]); + ]; } /** diff --git a/src/Appwrite/Event/Certificate.php b/src/Appwrite/Event/Certificate.php index 2629653c59..6a395417ed 100644 --- a/src/Appwrite/Event/Certificate.php +++ b/src/Appwrite/Event/Certificate.php @@ -3,7 +3,6 @@ namespace Appwrite\Event; use Utopia\Database\Document; -use Utopia\Queue\Client; use Utopia\Queue\Connection; class Certificate extends Event @@ -67,25 +66,16 @@ class Certificate extends Event } /** - * Executes the event and sends it to the certificates worker. + * Prepare the payload for the event * - * @return string|bool - * @throws \InvalidArgumentException + * @return array */ - public function trigger(): string|bool + protected function preparePayload(): array { - if ($this->paused) { - return false; - } - - $this->trimFields(); - - $client = new Client($this->queue, $this->connection); - - return $client->enqueue([ + return [ 'project' => $this->project, 'domain' => $this->domain, 'skipRenewCheck' => $this->skipRenewCheck - ]); + ]; } } diff --git a/src/Appwrite/Event/Database.php b/src/Appwrite/Event/Database.php index e78fe967e7..24123de6c1 100644 --- a/src/Appwrite/Event/Database.php +++ b/src/Appwrite/Event/Database.php @@ -4,7 +4,6 @@ namespace Appwrite\Event; use Utopia\Database\Document; use Utopia\DSN\DSN; -use Utopia\Queue\Client; use Utopia\Queue\Connection; class Database extends Event @@ -100,18 +99,8 @@ class Database extends Event return $this->document; } - /** - * Executes the event and send it to the database worker. - * - * @return string|bool - * @throws \InvalidArgumentException - */ - public function trigger(): string|bool + public function getQueue(): string { - if ($this->paused) { - return false; - } - try { $dsn = new DSN($this->getProject()->getAttribute('database')); } catch (\InvalidArgumentException) { @@ -119,25 +108,25 @@ class Database extends Event $dsn = new DSN('mysql://' . $this->getProject()->getAttribute('database')); } - $this->setQueue($dsn->getHost()); + $this->queue = $dsn->getHost(); + return $this->queue; + } - $this->trimFields(); - - $client = new Client($this->queue, $this->connection); - - try { - $result = $client->enqueue([ - 'project' => $this->project, - 'user' => $this->user, - 'type' => $this->type, - 'collection' => $this->collection, - 'document' => $this->document, - 'database' => $this->database, - 'events' => Event::generateEvents($this->getEvent(), $this->getParams()) - ]); - return $result; - } catch (\Throwable $th) { - return false; - } + /** + * Prepare the payload for the event + * + * @return array + */ + protected function preparePayload(): array + { + return [ + 'project' => $this->project, + 'user' => $this->user, + 'type' => $this->type, + 'collection' => $this->collection, + 'document' => $this->document, + 'database' => $this->database, + 'events' => Event::generateEvents($this->getEvent(), $this->getParams()) + ]; } } diff --git a/src/Appwrite/Event/Delete.php b/src/Appwrite/Event/Delete.php index 2874043163..f0af20f21b 100644 --- a/src/Appwrite/Event/Delete.php +++ b/src/Appwrite/Event/Delete.php @@ -3,7 +3,6 @@ namespace Appwrite\Event; use Utopia\Database\Document; -use Utopia\Queue\Client; use Utopia\Queue\Connection; class Delete extends Event @@ -131,24 +130,14 @@ class Delete extends Event return $this->document; } - /** - * Executes this event and sends it to the deletes worker. + * Prepare the payload for the event * - * @return string|bool - * @throws \InvalidArgumentException + * @return array */ - public function trigger(): string|bool + protected function preparePayload(): array { - if ($this->paused) { - return false; - } - - $this->trimFields(); - - $client = new Client($this->queue, $this->connection); - - return $client->enqueue([ + return [ 'project' => $this->project, 'type' => $this->type, 'document' => $this->document, @@ -156,6 +145,6 @@ class Delete extends Event 'resourceType' => $this->resourceType, 'datetime' => $this->datetime, 'hourlyUsageRetentionDatetime' => $this->hourlyUsageRetentionDatetime - ]); + ]; } } diff --git a/src/Appwrite/Event/Event.php b/src/Appwrite/Event/Event.php index 4efc138ec6..8dc0acbe2f 100644 --- a/src/Appwrite/Event/Event.php +++ b/src/Appwrite/Event/Event.php @@ -140,24 +140,6 @@ class Event return $this; } - /** - * Trims the fields of the project document to only include the necessary fields. - * - * @return self - */ - public function trimFields(): self - { - if ($this->project) { - $this->project = new Document([ - '$id' => $this->project->getId(), - '$internalId' => $this->project->getInternalId(), - 'database' => $this->project->getAttribute('database') - ]); - } - - return $this; - } - /** * Get project for this event. * @@ -329,6 +311,27 @@ class Event return $this->params; } + /** + * Get trimmed values for sensitive/large payload fields. + * Override this method in child classes to add more fields to trim. + * + * @return array + */ + protected function trimPayload(): array + { + $trimmed = []; + + if ($this->project) { + $trimmed['project'] = new Document([ + '$id' => $this->project->getId(), + '$internalId' => $this->project->getInternalId(), + 'database' => $this->project->getAttribute('database') + ]); + } + + return $trimmed; + } + /** * Execute Event. * @@ -341,18 +344,29 @@ class Event return false; } - $this->trimFields(); + $client = new Client($this->getQueue(), $this->connection); - $client = new Client($this->queue, $this->connection); + // Merge the base payload with any trimmed values + $payload = array_merge($this->preparePayload(), $this->trimPayload()); - return $client->enqueue([ + return $client->enqueue($payload); + } + + /** + * Prepare payload for queue. Can be overridden by child classes to customize payload. + * + * @return array + */ + protected function preparePayload(): array + { + return [ 'project' => $this->project, 'user' => $this->user, 'userId' => $this->userId, 'payload' => $this->payload, 'context' => $this->context, 'events' => Event::generateEvents($this->getEvent(), $this->getParams()) - ]); + ]; } /** diff --git a/src/Appwrite/Event/Func.php b/src/Appwrite/Event/Func.php index 7c6a2e9194..2e72cb86bf 100644 --- a/src/Appwrite/Event/Func.php +++ b/src/Appwrite/Event/Func.php @@ -3,7 +3,6 @@ namespace Appwrite\Event; use Utopia\Database\Document; -use Utopia\Queue\Client; use Utopia\Queue\Connection; class Func extends Event @@ -172,29 +171,6 @@ class Func extends Event return $this; } - /** - * Returns set custom data for the function event. - * - * @return string - */ - public function getData(): string - { - return $this->data; - } - - /** - * Sets JWT for the function event. - * - * @param string $jwt - * @return self - */ - public function setJWT(string $jwt): self - { - $this->jwt = $jwt; - - return $this; - } - /** * Returns set JWT for the function event. * @@ -206,24 +182,15 @@ class Func extends Event } /** - * Executes the function event and sends it to the functions worker. + * Prepare payload for the function event. * - * @return string|bool - * @throws \InvalidArgumentException + * @return array */ - public function trigger(): string|bool + protected function preparePayload(): array { - if ($this->paused) { - return false; - } - - $this->trimFields(); - - $client = new Client($this->queue, $this->connection); - $events = $this->getEvent() ? Event::generateEvents($this->getEvent(), $this->getParams()) : null; - return $client->enqueue([ + return [ 'project' => $this->project, 'user' => $this->user, 'userId' => $this->userId, @@ -238,6 +205,6 @@ class Func extends Event 'path' => $this->path, 'headers' => $this->headers, 'method' => $this->method, - ]); + ]; } } diff --git a/src/Appwrite/Event/Mail.php b/src/Appwrite/Event/Mail.php index 0db576ba68..1c9e539cdb 100644 --- a/src/Appwrite/Event/Mail.php +++ b/src/Appwrite/Event/Mail.php @@ -2,7 +2,6 @@ namespace Appwrite\Event; -use Utopia\Queue\Client; use Utopia\Queue\Connection; class Mail extends Event @@ -397,22 +396,13 @@ class Mail extends Event } /** - * Executes the event and sends it to the mails worker. + * Prepare the payload for the event * - * @return string|bool - * @throws \InvalidArgumentException + * @return array */ - public function trigger(): string|bool + protected function preparePayload(): array { - if ($this->paused) { - return false; - } - - $this->trimFields(); - - $client = new Client($this->queue, $this->connection); - - return $client->enqueue([ + return [ 'project' => $this->project, 'recipient' => $this->recipient, 'name' => $this->name, @@ -423,6 +413,6 @@ class Mail extends Event 'variables' => $this->variables, 'attachment' => $this->attachment, 'events' => Event::generateEvents($this->getEvent(), $this->getParams()) - ]); + ]; } } diff --git a/src/Appwrite/Event/Messaging.php b/src/Appwrite/Event/Messaging.php index ac2df7f92a..61dbe9c427 100644 --- a/src/Appwrite/Event/Messaging.php +++ b/src/Appwrite/Event/Messaging.php @@ -3,7 +3,6 @@ namespace Appwrite\Event; use Utopia\Database\Document; -use Utopia\Queue\Client; use Utopia\Queue\Connection; class Messaging extends Event @@ -176,21 +175,13 @@ class Messaging extends Event } /** - * Executes the event and sends it to the messaging worker. - * @return string|bool - * @throws \InvalidArgumentException + * Prepare the payload for the event + * + * @return array */ - public function trigger(): string | bool + protected function preparePayload(): array { - if ($this->paused) { - return false; - } - - $this->trimFields(); - - $client = new Client($this->queue, $this->connection); - - return $client->enqueue([ + return [ 'type' => $this->type, 'project' => $this->project, 'user' => $this->user, @@ -198,6 +189,6 @@ class Messaging extends Event 'message' => $this->message, 'recipients' => $this->recipients, 'providerType' => $this->providerType, - ]); + ]; } } diff --git a/src/Appwrite/Event/Migration.php b/src/Appwrite/Event/Migration.php index 376264bce3..5fb2d5a106 100644 --- a/src/Appwrite/Event/Migration.php +++ b/src/Appwrite/Event/Migration.php @@ -3,7 +3,6 @@ namespace Appwrite\Event; use Utopia\Database\Document; -use Utopia\Queue\Client; use Utopia\Queue\Connection; class Migration extends Event @@ -68,25 +67,16 @@ class Migration extends Event } /** - * Executes the migration event and sends it to the migrations worker. + * Prepare the payload for the migration event. * - * @return string|bool - * @throws \InvalidArgumentException + * @return array */ - public function trigger(): string|bool + protected function preparePayload(): array { - if ($this->paused) { - return false; - } - - $this->trimFields(); - - $client = new Client($this->queue, $this->connection); - - return $client->enqueue([ + return [ 'project' => $this->project, 'user' => $this->user, 'migration' => $this->migration, - ]); + ]; } } diff --git a/src/Appwrite/Event/Realtime.php b/src/Appwrite/Event/Realtime.php index f4f00b59d4..481c7918ca 100644 --- a/src/Appwrite/Event/Realtime.php +++ b/src/Appwrite/Event/Realtime.php @@ -43,6 +43,9 @@ class Realtime extends Event $collection = $this->getContext('collection'); $bucket = $this->getContext('bucket'); + // var_dump("-------------------- In Realtime trigger ---------------"); + // var_dump($this->getProject()); + $target = RealtimeAdapter::fromPayload( // Pass first, most verbose event pattern event: $allEvents[0], diff --git a/src/Appwrite/Event/Usage.php b/src/Appwrite/Event/Usage.php index f17c048a2e..5609859f37 100644 --- a/src/Appwrite/Event/Usage.php +++ b/src/Appwrite/Event/Usage.php @@ -3,7 +3,6 @@ namespace Appwrite\Event; use Utopia\Database\Document; -use Utopia\Queue\Client; use Utopia\Queue\Connection; class Usage extends Event @@ -51,6 +50,20 @@ class Usage extends Event return $this; } + /** + * Prepare the payload for the usage event. + * + * @return array + */ + protected function preparePayload(): array + { + return [ + 'project' => $this->project, + 'reduce' => $this->reduce, + 'metrics' => $this->metrics, + ]; + } + /** * Sends metrics to the usage worker. * @@ -58,22 +71,8 @@ class Usage extends Event */ public function trigger(): string|bool { - if ($this->paused) { - return false; - } - - $this->trimFields(); - - $client = new Client($this->queue, $this->connection); - - $result = $client->enqueue([ - 'project' => $this->project, - 'reduce' => $this->reduce, - 'metrics' => $this->metrics, - ]); - + parent::trigger(); $this->metrics = []; - - return $result; + return true; } } diff --git a/src/Appwrite/Event/UsageDump.php b/src/Appwrite/Event/UsageDump.php index 83206e4add..6f44de4eda 100644 --- a/src/Appwrite/Event/UsageDump.php +++ b/src/Appwrite/Event/UsageDump.php @@ -2,7 +2,6 @@ namespace Appwrite\Event; -use Utopia\Queue\Client; use Utopia\Queue\Connection; class UsageDump extends Event @@ -32,22 +31,14 @@ class UsageDump extends Event } /** - * Sends metrics to the usage worker. + * Prepare the payload for the usage dump event. * - * @return string|bool + * @return array */ - public function trigger(): string|bool + protected function preparePayload(): array { - if ($this->paused) { - return false; - } - - $this->trimFields(); - - $client = new Client($this->queue, $this->connection); - - return $client->enqueue([ + return [ 'stats' => $this->stats, - ]); + ]; } } diff --git a/src/Appwrite/Event/Webhook.php b/src/Appwrite/Event/Webhook.php index 9d14dcdc3d..3e0dbe446f 100644 --- a/src/Appwrite/Event/Webhook.php +++ b/src/Appwrite/Event/Webhook.php @@ -15,10 +15,17 @@ class Webhook extends Event ->setClass(Event::WEBHOOK_CLASS_NAME); } - public function trigger(): string|bool + /** + * Trim the payload for the webhook event. + * + * @return array + */ + public function trimPayload(): array { - /** Filter out context and trim project to keep the payload small */ - $this->context = []; - return parent::trigger(); + $trimmed = parent::trimPayload(); + if (isset($this->context)) { + $trimmed['context'] = []; + } + return $trimmed; } } diff --git a/tests/e2e/Services/Functions/FunctionsCustomServerTest.php b/tests/e2e/Services/Functions/FunctionsCustomServerTest.php index 9b9f03a100..d8d1eb8eb5 100644 --- a/tests/e2e/Services/Functions/FunctionsCustomServerTest.php +++ b/tests/e2e/Services/Functions/FunctionsCustomServerTest.php @@ -375,7 +375,7 @@ class FunctionsCustomServerTest extends Scope $this->assertEquals(200, $deployment['headers']['status-code']); $this->assertEquals('ready', $deployment['body']['status']); - }, 500000, 1000); + }, 50000, 1000); $function = $this->getFunction($functionId); From a2962b6b95db0ec079cbfacf121a7895dd37c6f6 Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Fri, 17 Jan 2025 10:42:06 +0530 Subject: [PATCH 07/10] chore: review comments --- app/controllers/api/functions.php | 2 -- phpunit.xml | 2 +- src/Appwrite/Event/Event.php | 1 + 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/app/controllers/api/functions.php b/app/controllers/api/functions.php index f459875fca..f7606ed66b 100644 --- a/app/controllers/api/functions.php +++ b/app/controllers/api/functions.php @@ -402,8 +402,6 @@ App::post('/v1/functions') ]); - var_dump("-------------------- Create Function ---------------"); - var_dump($project); $target = Realtime::fromPayload( // Pass first, most verbose event pattern event: $allEvents[0], diff --git a/phpunit.xml b/phpunit.xml index 598b730908..4c4e55ea4e 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -6,7 +6,7 @@ convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" - stopOnFailure="true" + stopOnFailure="false" > diff --git a/src/Appwrite/Event/Event.php b/src/Appwrite/Event/Event.php index 8dc0acbe2f..5cd5f8e7d6 100644 --- a/src/Appwrite/Event/Event.php +++ b/src/Appwrite/Event/Event.php @@ -344,6 +344,7 @@ class Event return false; } + /** The getter is required since events like Databases need to override the queue name depending on the project */ $client = new Client($this->getQueue(), $this->connection); // Merge the base payload with any trimmed values From f66f75b8085f537b745b47af5ae70ae765551388 Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Fri, 17 Jan 2025 10:43:02 +0530 Subject: [PATCH 08/10] chore: review comments --- app/controllers/api/functions.php | 1 - src/Appwrite/Event/Realtime.php | 3 --- 2 files changed, 4 deletions(-) diff --git a/app/controllers/api/functions.php b/app/controllers/api/functions.php index f7606ed66b..efd6967408 100644 --- a/app/controllers/api/functions.php +++ b/app/controllers/api/functions.php @@ -401,7 +401,6 @@ App::post('/v1/functions') 'ruleId' => $rule->getId(), ]); - $target = Realtime::fromPayload( // Pass first, most verbose event pattern event: $allEvents[0], diff --git a/src/Appwrite/Event/Realtime.php b/src/Appwrite/Event/Realtime.php index 481c7918ca..f4f00b59d4 100644 --- a/src/Appwrite/Event/Realtime.php +++ b/src/Appwrite/Event/Realtime.php @@ -43,9 +43,6 @@ class Realtime extends Event $collection = $this->getContext('collection'); $bucket = $this->getContext('bucket'); - // var_dump("-------------------- In Realtime trigger ---------------"); - // var_dump($this->getProject()); - $target = RealtimeAdapter::fromPayload( // Pass first, most verbose event pattern event: $allEvents[0], From 455bc3370cf40a58c27e4a972920585a2923c2ad Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Fri, 17 Jan 2025 11:16:21 +0530 Subject: [PATCH 09/10] chore: fix tests --- src/Appwrite/Event/Func.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/Appwrite/Event/Func.php b/src/Appwrite/Event/Func.php index 2e72cb86bf..b3945fccb8 100644 --- a/src/Appwrite/Event/Func.php +++ b/src/Appwrite/Event/Func.php @@ -181,6 +181,18 @@ class Func extends Event return $this->jwt; } + /** + * Sets JWT for the function event. + * + * @param string $jwt + * @return self + */ + public function setJWT(string $jwt): self + { + $this->jwt = $jwt; + return $this; + } + /** * Prepare payload for the function event. * From ef0b836987864e8ab167c88b3f4d34be9e6bea7b Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Fri, 17 Jan 2025 11:19:34 +0530 Subject: [PATCH 10/10] chore: composer update --- composer.lock | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/composer.lock b/composer.lock index e60804b733..980dcdcd29 100644 --- a/composer.lock +++ b/composer.lock @@ -3379,16 +3379,16 @@ }, { "name": "utopia-php/compression", - "version": "0.1.2", + "version": "0.1.3", "source": { "type": "git", "url": "https://github.com/utopia-php/compression.git", - "reference": "6062f70596415f8d5de40a589367b0eb2a435f98" + "reference": "66f093557ba66d98245e562036182016c7dcfe8a" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/compression/zipball/6062f70596415f8d5de40a589367b0eb2a435f98", - "reference": "6062f70596415f8d5de40a589367b0eb2a435f98", + "url": "https://api.github.com/repos/utopia-php/compression/zipball/66f093557ba66d98245e562036182016c7dcfe8a", + "reference": "66f093557ba66d98245e562036182016c7dcfe8a", "shasum": "" }, "require": { @@ -3419,9 +3419,9 @@ ], "support": { "issues": "https://github.com/utopia-php/compression/issues", - "source": "https://github.com/utopia-php/compression/tree/0.1.2" + "source": "https://github.com/utopia-php/compression/tree/0.1.3" }, - "time": "2024-11-08T14:59:54+00:00" + "time": "2025-01-15T15:15:51+00:00" }, { "name": "utopia-php/config", @@ -3678,16 +3678,16 @@ }, { "name": "utopia-php/framework", - "version": "0.33.15", + "version": "0.33.16", "source": { "type": "git", "url": "https://github.com/utopia-php/http.git", - "reference": "83b0628900c2c53e8c3efbf069f3e13050295edc" + "reference": "e91d4c560d1b809e25faa63d564fef034363b50f" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/http/zipball/83b0628900c2c53e8c3efbf069f3e13050295edc", - "reference": "83b0628900c2c53e8c3efbf069f3e13050295edc", + "url": "https://api.github.com/repos/utopia-php/http/zipball/e91d4c560d1b809e25faa63d564fef034363b50f", + "reference": "e91d4c560d1b809e25faa63d564fef034363b50f", "shasum": "" }, "require": { @@ -3719,9 +3719,9 @@ ], "support": { "issues": "https://github.com/utopia-php/http/issues", - "source": "https://github.com/utopia-php/http/tree/0.33.15" + "source": "https://github.com/utopia-php/http/tree/0.33.16" }, - "time": "2024-12-10T13:07:04+00:00" + "time": "2025-01-16T15:58:50+00:00" }, { "name": "utopia-php/image", @@ -4095,16 +4095,16 @@ }, { "name": "utopia-php/platform", - "version": "0.7.2", + "version": "0.7.1", "source": { "type": "git", "url": "https://github.com/utopia-php/platform.git", - "reference": "6f9243848f1c6466f6509fd01c7e18306a6d8caf" + "reference": "3433a0f1a54988f2a59c735f507745cb2c24638a" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/platform/zipball/6f9243848f1c6466f6509fd01c7e18306a6d8caf", - "reference": "6f9243848f1c6466f6509fd01c7e18306a6d8caf", + "url": "https://api.github.com/repos/utopia-php/platform/zipball/3433a0f1a54988f2a59c735f507745cb2c24638a", + "reference": "3433a0f1a54988f2a59c735f507745cb2c24638a", "shasum": "" }, "require": { @@ -4139,9 +4139,9 @@ ], "support": { "issues": "https://github.com/utopia-php/platform/issues", - "source": "https://github.com/utopia-php/platform/tree/0.7.2" + "source": "https://github.com/utopia-php/platform/tree/0.7.1" }, - "time": "2025-01-15T05:56:26+00:00" + "time": "2024-10-22T10:27:49+00:00" }, { "name": "utopia-php/pools",