From bbeca28026132da1996771da8126ecfb938240f0 Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Tue, 23 Dec 2025 15:55:57 +0530 Subject: [PATCH] refactor use env variables for queue and class names --- app/controllers/api/health.php | 48 +++++++++---------- src/Appwrite/Event/Audit.php | 5 +- src/Appwrite/Event/Build.php | 5 +- src/Appwrite/Event/Certificate.php | 5 +- src/Appwrite/Event/Database.php | 3 +- src/Appwrite/Event/Delete.php | 5 +- src/Appwrite/Event/Func.php | 5 +- src/Appwrite/Event/Mail.php | 5 +- src/Appwrite/Event/Messaging.php | 5 +- src/Appwrite/Event/Migration.php | 5 +- src/Appwrite/Event/StatsResources.php | 5 +- src/Appwrite/Event/StatsUsage.php | 5 +- src/Appwrite/Event/Webhook.php | 5 +- .../Platform/Tasks/ScheduleFunctions.php | 10 ++-- 14 files changed, 62 insertions(+), 54 deletions(-) diff --git a/app/controllers/api/health.php b/app/controllers/api/health.php index 56320159ea..97ddf8391c 100644 --- a/app/controllers/api/health.php +++ b/app/controllers/api/health.php @@ -945,18 +945,18 @@ App::get('/v1/health/queue/failed/:name') contentType: ContentType::JSON )) ->param('name', '', new WhiteList([ - Event::DATABASE_QUEUE_NAME, - Event::DELETE_QUEUE_NAME, - Event::AUDITS_QUEUE_NAME, - Event::MAILS_QUEUE_NAME, - Event::FUNCTIONS_QUEUE_NAME, - Event::STATS_RESOURCES_QUEUE_NAME, - Event::STATS_USAGE_QUEUE_NAME, - Event::WEBHOOK_QUEUE_NAME, - Event::CERTIFICATES_QUEUE_NAME, - Event::BUILDS_QUEUE_NAME, - Event::MESSAGING_QUEUE_NAME, - Event::MIGRATIONS_QUEUE_NAME + System::getEnv('_APP_DATABASE_QUEUE_NAME', Event::DATABASE_QUEUE_NAME), + System::getEnv('_APP_DELETE_QUEUE_NAME', Event::DELETE_QUEUE_NAME), + System::getEnv('_APP_AUDITS_QUEUE_NAME', Event::AUDITS_QUEUE_NAME), + System::getEnv('_APP_MAILS_QUEUE_NAME', Event::MAILS_QUEUE_NAME), + System::getEnv('_APP_FUNCTIONS_QUEUE_NAME', Event::FUNCTIONS_QUEUE_NAME), + System::getEnv('_APP_STATS_RESOURCES_QUEUE_NAME', Event::STATS_RESOURCES_QUEUE_NAME), + System::getEnv('_APP_STATS_USAGE_QUEUE_NAME', Event::STATS_USAGE_QUEUE_NAME), + System::getEnv('_APP_WEBHOOK_QUEUE_NAME', Event::WEBHOOK_QUEUE_NAME), + System::getEnv('_APP_CERTIFICATES_QUEUE_NAME', Event::CERTIFICATES_QUEUE_NAME), + System::getEnv('_APP_BUILDS_QUEUE_NAME', Event::BUILDS_QUEUE_NAME), + System::getEnv('_APP_MESSAGING_QUEUE_NAME', Event::MESSAGING_QUEUE_NAME), + System::getEnv('_APP_MIGRATIONS_QUEUE_NAME', Event::MIGRATIONS_QUEUE_NAME) ]), 'The name of the queue') ->param('threshold', 5000, new Integer(true), 'Queue size threshold. When hit (equal or higher), endpoint returns server error. Default value is 5000.', true) ->inject('response') @@ -993,18 +993,18 @@ App::get('/v1/health/queue/failed/:name') /** @var Event $queue */ $queue = match ($name) { - Event::DATABASE_QUEUE_NAME => $queueForDatabase, - Event::DELETE_QUEUE_NAME => $queueForDeletes, - Event::AUDITS_QUEUE_NAME => $queueForAudits, - Event::MAILS_QUEUE_NAME => $queueForMails, - Event::FUNCTIONS_QUEUE_NAME => $queueForFunctions, - Event::STATS_RESOURCES_QUEUE_NAME => $queueForStatsResources, - Event::STATS_USAGE_QUEUE_NAME => $queueForStatsUsage, - Event::WEBHOOK_QUEUE_NAME => $queueForWebhooks, - Event::CERTIFICATES_QUEUE_NAME => $queueForCertificates, - Event::BUILDS_QUEUE_NAME => $queueForBuilds, - Event::MESSAGING_QUEUE_NAME => $queueForMessaging, - Event::MIGRATIONS_QUEUE_NAME => $queueForMigrations, + System::getEnv('_APP_DATABASE_QUEUE_NAME', Event::DATABASE_QUEUE_NAME) => $queueForDatabase, + System::getEnv('_APP_DELETE_QUEUE_NAME', Event::DELETE_QUEUE_NAME) => $queueForDeletes, + System::getEnv('_APP_AUDITS_QUEUE_NAME', Event::AUDITS_QUEUE_NAME) => $queueForAudits, + System::getEnv('_APP_MAILS_QUEUE_NAME', Event::MAILS_QUEUE_NAME) => $queueForMails, + System::getEnv('_APP_FUNCTIONS_QUEUE_NAME', Event::FUNCTIONS_QUEUE_NAME) => $queueForFunctions, + System::getEnv('_APP_STATS_RESOURCES_QUEUE_NAME', Event::STATS_RESOURCES_QUEUE_NAME) => $queueForStatsResources, + System::getEnv('_APP_STATS_USAGE_QUEUE_NAME', Event::STATS_USAGE_QUEUE_NAME) => $queueForStatsUsage, + System::getEnv('_APP_WEBHOOK_QUEUE_NAME', Event::WEBHOOK_QUEUE_NAME) => $queueForWebhooks, + System::getEnv('_APP_CERTIFICATES_QUEUE_NAME', Event::CERTIFICATES_QUEUE_NAME) => $queueForCertificates, + System::getEnv('_APP_BUILDS_QUEUE_NAME', Event::BUILDS_QUEUE_NAME) => $queueForBuilds, + System::getEnv('_APP_MESSAGING_QUEUE_NAME', Event::MESSAGING_QUEUE_NAME) => $queueForMessaging, + System::getEnv('_APP_MIGRATIONS_QUEUE_NAME', Event::MIGRATIONS_QUEUE_NAME) => $queueForMigrations, }; $failed = $queue->getSize(failed: true); diff --git a/src/Appwrite/Event/Audit.php b/src/Appwrite/Event/Audit.php index dd48093dc5..b26083b013 100644 --- a/src/Appwrite/Event/Audit.php +++ b/src/Appwrite/Event/Audit.php @@ -3,6 +3,7 @@ namespace Appwrite\Event; use Utopia\Queue\Publisher; +use Utopia\System\System; class Audit extends Event { @@ -19,8 +20,8 @@ class Audit extends Event parent::__construct($publisher); $this - ->setQueue(Event::AUDITS_QUEUE_NAME) - ->setClass(Event::AUDITS_CLASS_NAME); + ->setQueue(System::getEnv('_APP_AUDITS_QUEUE_NAME', Event::AUDITS_QUEUE_NAME)) + ->setClass(System::getEnv('_APP_AUDITS_CLASS_NAME', Event::AUDITS_CLASS_NAME)); } /** diff --git a/src/Appwrite/Event/Build.php b/src/Appwrite/Event/Build.php index 38f423829e..4eaf108f15 100644 --- a/src/Appwrite/Event/Build.php +++ b/src/Appwrite/Event/Build.php @@ -5,6 +5,7 @@ namespace Appwrite\Event; use Utopia\Config\Config; use Utopia\Database\Document; use Utopia\Queue\Publisher; +use Utopia\System\System; class Build extends Event { @@ -18,8 +19,8 @@ class Build extends Event parent::__construct($publisher); $this - ->setQueue(Event::BUILDS_QUEUE_NAME) - ->setClass(Event::BUILDS_CLASS_NAME); + ->setQueue(System::getEnv('_APP_BUILDS_QUEUE_NAME', Event::BUILDS_QUEUE_NAME)) + ->setClass(System::getEnv('_APP_BUILDS_CLASS_NAME', Event::BUILDS_CLASS_NAME)); } /** diff --git a/src/Appwrite/Event/Certificate.php b/src/Appwrite/Event/Certificate.php index 00875c7a4a..259398717e 100644 --- a/src/Appwrite/Event/Certificate.php +++ b/src/Appwrite/Event/Certificate.php @@ -4,6 +4,7 @@ namespace Appwrite\Event; use Utopia\Database\Document; use Utopia\Queue\Publisher; +use Utopia\System\System; class Certificate extends Event { @@ -16,8 +17,8 @@ class Certificate extends Event parent::__construct($publisher); $this - ->setQueue(Event::CERTIFICATES_QUEUE_NAME) - ->setClass(Event::CERTIFICATES_CLASS_NAME); + ->setQueue(System::getEnv('_APP_CERTIFICATES_QUEUE_NAME', Event::CERTIFICATES_QUEUE_NAME)) + ->setClass(System::getEnv('_APP_CERTIFICATES_CLASS_NAME', Event::CERTIFICATES_CLASS_NAME)); } /** diff --git a/src/Appwrite/Event/Database.php b/src/Appwrite/Event/Database.php index eac30ac07e..4e8e1fdea9 100644 --- a/src/Appwrite/Event/Database.php +++ b/src/Appwrite/Event/Database.php @@ -5,6 +5,7 @@ namespace Appwrite\Event; use Utopia\Database\Document; use Utopia\DSN\DSN; use Utopia\Queue\Publisher; +use Utopia\System\System; class Database extends Event { @@ -24,7 +25,7 @@ class Database extends Event { parent::__construct($publisher); - $this->setClass(Event::DATABASE_CLASS_NAME); + $this->setClass(System::getEnv('_APP_DATABASE_CLASS_NAME', Event::DATABASE_CLASS_NAME)); } /** diff --git a/src/Appwrite/Event/Delete.php b/src/Appwrite/Event/Delete.php index 450be306d7..6747acb03f 100644 --- a/src/Appwrite/Event/Delete.php +++ b/src/Appwrite/Event/Delete.php @@ -4,6 +4,7 @@ namespace Appwrite\Event; use Utopia\Database\Document; use Utopia\Queue\Publisher; +use Utopia\System\System; class Delete extends Event { @@ -20,8 +21,8 @@ class Delete extends Event parent::__construct($publisher); $this - ->setQueue(Event::DELETE_QUEUE_NAME) - ->setClass(Event::DELETE_CLASS_NAME); + ->setQueue(System::getEnv('_APP_DELETE_QUEUE_NAME', Event::DELETE_QUEUE_NAME)) + ->setClass(System::getEnv('_APP_DELETE_CLASS_NAME', Event::DELETE_CLASS_NAME)); } /** diff --git a/src/Appwrite/Event/Func.php b/src/Appwrite/Event/Func.php index c9622a9ce0..8d8c51b540 100644 --- a/src/Appwrite/Event/Func.php +++ b/src/Appwrite/Event/Func.php @@ -5,6 +5,7 @@ namespace Appwrite\Event; use Utopia\Config\Config; use Utopia\Database\Document; use Utopia\Queue\Publisher; +use Utopia\System\System; class Func extends Event { @@ -25,8 +26,8 @@ class Func extends Event parent::__construct($publisher); $this - ->setQueue(Event::FUNCTIONS_QUEUE_NAME) - ->setClass(Event::FUNCTIONS_CLASS_NAME); + ->setQueue(System::getEnv('_APP_FUNCTIONS_QUEUE_NAME', Event::FUNCTIONS_QUEUE_NAME)) + ->setClass(System::getEnv('_APP_FUNCTIONS_CLASS_NAME', Event::FUNCTIONS_CLASS_NAME)); } /** diff --git a/src/Appwrite/Event/Mail.php b/src/Appwrite/Event/Mail.php index ed96f6f93a..3cfe8f8a87 100644 --- a/src/Appwrite/Event/Mail.php +++ b/src/Appwrite/Event/Mail.php @@ -4,6 +4,7 @@ namespace Appwrite\Event; use Utopia\Config\Config; use Utopia\Queue\Publisher; +use Utopia\System\System; class Mail extends Event { @@ -24,8 +25,8 @@ class Mail extends Event parent::__construct($publisher); $this - ->setQueue(Event::MAILS_QUEUE_NAME) - ->setClass(Event::MAILS_CLASS_NAME); + ->setQueue(System::getEnv('_APP_MAILS_QUEUE_NAME', Event::MAILS_QUEUE_NAME)) + ->setClass(System::getEnv('_APP_MAILS_CLASS_NAME', Event::MAILS_CLASS_NAME)); } /** diff --git a/src/Appwrite/Event/Messaging.php b/src/Appwrite/Event/Messaging.php index f4c72c7d72..8c13185e0b 100644 --- a/src/Appwrite/Event/Messaging.php +++ b/src/Appwrite/Event/Messaging.php @@ -4,6 +4,7 @@ namespace Appwrite\Event; use Utopia\Database\Document; use Utopia\Queue\Publisher; +use Utopia\System\System; class Messaging extends Event { @@ -19,8 +20,8 @@ class Messaging extends Event parent::__construct($publisher); $this - ->setQueue(Event::MESSAGING_QUEUE_NAME) - ->setClass(Event::MESSAGING_CLASS_NAME); + ->setQueue(System::getEnv('_APP_MESSAGING_QUEUE_NAME', Event::MESSAGING_QUEUE_NAME)) + ->setClass(System::getEnv('_APP_MESSAGING_CLASS_NAME', Event::MESSAGING_CLASS_NAME)); } /** diff --git a/src/Appwrite/Event/Migration.php b/src/Appwrite/Event/Migration.php index a224d4f4c3..89f49f0876 100644 --- a/src/Appwrite/Event/Migration.php +++ b/src/Appwrite/Event/Migration.php @@ -5,6 +5,7 @@ namespace Appwrite\Event; use Utopia\Config\Config; use Utopia\Database\Document; use Utopia\Queue\Publisher; +use Utopia\System\System; class Migration extends Event { @@ -16,8 +17,8 @@ class Migration extends Event parent::__construct($publisher); $this - ->setQueue(Event::MIGRATIONS_QUEUE_NAME) - ->setClass(Event::MIGRATIONS_CLASS_NAME); + ->setQueue(System::getEnv('_APP_MIGRATIONS_QUEUE_NAME', Event::MIGRATIONS_QUEUE_NAME)) + ->setClass(System::getEnv('_APP_MIGRATIONS_CLASS_NAME', Event::MIGRATIONS_CLASS_NAME)); } /** diff --git a/src/Appwrite/Event/StatsResources.php b/src/Appwrite/Event/StatsResources.php index c4f7ac1690..07f23feda8 100644 --- a/src/Appwrite/Event/StatsResources.php +++ b/src/Appwrite/Event/StatsResources.php @@ -3,6 +3,7 @@ namespace Appwrite\Event; use Utopia\Queue\Publisher; +use Utopia\System\System; class StatsResources extends Event { @@ -13,8 +14,8 @@ class StatsResources extends Event parent::__construct($publisher); $this - ->setQueue(Event::STATS_RESOURCES_QUEUE_NAME) - ->setClass(Event::STATS_RESOURCES_CLASS_NAME); + ->setQueue(System::getEnv('_APP_STATS_RESOURCES_QUEUE_NAME', Event::STATS_RESOURCES_QUEUE_NAME)) + ->setClass(System::getEnv('_APP_STATS_RESOURCES_CLASS_NAME', Event::STATS_RESOURCES_CLASS_NAME)); } /** diff --git a/src/Appwrite/Event/StatsUsage.php b/src/Appwrite/Event/StatsUsage.php index f6b1d695f4..47ba5a3ea0 100644 --- a/src/Appwrite/Event/StatsUsage.php +++ b/src/Appwrite/Event/StatsUsage.php @@ -4,6 +4,7 @@ namespace Appwrite\Event; use Utopia\Database\Document; use Utopia\Queue\Publisher; +use Utopia\System\System; class StatsUsage extends Event { @@ -18,8 +19,8 @@ class StatsUsage extends Event parent::__construct($publisher); $this - ->setQueue(Event::STATS_USAGE_QUEUE_NAME) - ->setClass(Event::STATS_USAGE_CLASS_NAME); + ->setQueue(System::getEnv('_APP_STATS_USAGE_QUEUE_NAME', Event::STATS_USAGE_QUEUE_NAME)) + ->setClass(System::getEnv('_APP_STATS_USAGE_CLASS_NAME', Event::STATS_USAGE_CLASS_NAME)); } /** diff --git a/src/Appwrite/Event/Webhook.php b/src/Appwrite/Event/Webhook.php index 5cc65758ee..f6d16c8b14 100644 --- a/src/Appwrite/Event/Webhook.php +++ b/src/Appwrite/Event/Webhook.php @@ -3,6 +3,7 @@ namespace Appwrite\Event; use Utopia\Queue\Publisher; +use Utopia\System\System; class Webhook extends Event { @@ -11,8 +12,8 @@ class Webhook extends Event parent::__construct($publisher); $this - ->setQueue(Event::WEBHOOK_QUEUE_NAME) - ->setClass(Event::WEBHOOK_CLASS_NAME); + ->setQueue(System::getEnv('_APP_WEBHOOK_QUEUE_NAME', Event::WEBHOOK_QUEUE_NAME)) + ->setClass(System::getEnv('_APP_WEBHOOK_CLASS_NAME', Event::WEBHOOK_CLASS_NAME)); } /** diff --git a/src/Appwrite/Platform/Tasks/ScheduleFunctions.php b/src/Appwrite/Platform/Tasks/ScheduleFunctions.php index 396eeb373b..7fda2f75df 100644 --- a/src/Appwrite/Platform/Tasks/ScheduleFunctions.php +++ b/src/Appwrite/Platform/Tasks/ScheduleFunctions.php @@ -19,7 +19,7 @@ class ScheduleFunctions extends ScheduleBase public const UPDATE_TIMER = 10; // seconds public const ENQUEUE_TIMER = 60; // seconds - protected ?float $lastEnqueueUpdate = null; + private ?float $lastEnqueueUpdate = null; public static function getName(): string { @@ -36,11 +36,6 @@ class ScheduleFunctions extends ScheduleBase return RESOURCE_TYPE_FUNCTIONS; } - protected function getQueueForFunctions(): Func - { - return new Func($this->publisherFunctions); - } - protected function enqueueResources(Database $dbForPlatform, callable $getProjectDB): void { $timerStart = \microtime(true); @@ -100,7 +95,8 @@ class ScheduleFunctions extends ScheduleBase $this->updateProjectAccess($schedule['project'], $dbForPlatform); - $queueForFunctions = $this->getQueueForFunctions(); + $queueForFunctions = new Func($this->publisherFunctions); + $queueForFunctions ->setType('schedule') ->setFunction($schedule['resource'])