From 6e7c160249c23e98ce2295a14a3e1fecafa8f187 Mon Sep 17 00:00:00 2001 From: shimon Date: Fri, 2 Jun 2023 06:54:34 +0300 Subject: [PATCH] changing event signature --- app/config/collections.php | 11 +++++++++++ app/console | 2 +- app/controllers/shared/api.php | 2 ++ app/init.php | 2 +- app/worker.php | 17 +++++++++++------ bin/worker-audits | 2 +- bin/worker-mails | 2 +- bin/worker-messaging | 2 +- bin/worker-webhooks | 2 +- docker-compose.yml | 12 ------------ src/Appwrite/Event/Audit.php | 7 ++++++- src/Appwrite/Event/Build.php | 6 +++++- src/Appwrite/Event/Certificate.php | 6 +++++- src/Appwrite/Event/Database.php | 6 +++++- src/Appwrite/Event/Delete.php | 6 +++++- src/Appwrite/Event/Event.php | 17 ++++++++--------- src/Appwrite/Event/Func.php | 6 +++++- src/Appwrite/Event/Mail.php | 6 +++++- src/Appwrite/Event/Phone.php | 6 +++++- src/Appwrite/Event/Usage.php | 6 +++++- src/Appwrite/Platform/Workers/Audits.php | 9 +++++++-- src/Appwrite/Platform/Workers/Mails.php | 9 ++++++++- src/Appwrite/Platform/Workers/Messaging.php | 7 ++++++- src/Appwrite/Platform/Workers/Webhooks.php | 5 ++++- 24 files changed, 109 insertions(+), 47 deletions(-) diff --git a/app/config/collections.php b/app/config/collections.php index 5648bfe6e..12904a1e2 100644 --- a/app/config/collections.php +++ b/app/config/collections.php @@ -3410,6 +3410,17 @@ $collections = [ 'default' => null, 'filters' => [], ], + [ + 'array' => false, + '$id' => ID::custom('bucketInternalId'), + 'type' => Database::VAR_STRING, + 'format' => '', + 'size' => Database::LENGTH_KEY, + 'signed' => true, + 'required' => true, + 'default' => null, + 'filters' => [], + ], [ '$id' => ID::custom('name'), 'type' => Database::VAR_STRING, diff --git a/app/console b/app/console index 2fac7c1f3..9174d8f8c 160000 --- a/app/console +++ b/app/console @@ -1 +1 @@ -Subproject commit 2fac7c1f390637f7dfc11882dcad6fb6508c83f1 +Subproject commit 9174d8f8cb584744dd7a53f69d324f490ee82ee3 diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index 305f527f8..76c985002 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -428,9 +428,11 @@ App::shutdown() $responsePayload = $response->getPayload(); if (!empty($queueForEvents->getEvent())) { + if (empty($queueForEvents->getPayload())) { $queueForEvents->setPayload($responsePayload); } + /** * Trigger functions. */ diff --git a/app/init.php b/app/init.php index 2378d7803..d473f5937 100644 --- a/app/init.php +++ b/app/init.php @@ -882,7 +882,7 @@ App::setResource('queueForDeletes', function (Connection $queue) { return new Delete($queue); }, ['queue']); App::setResource('queueForEvents', function (Connection $queue) { - return new Event('', '', $queue); + return new Event($queue); }, ['queue']); App::setResource('queueForAudits', function (Connection $queue) { return new Audit($queue); diff --git a/app/worker.php b/app/worker.php index 1108341b9..ac5a58517 100644 --- a/app/worker.php +++ b/app/worker.php @@ -12,6 +12,7 @@ use Appwrite\Event\Func; use Appwrite\Event\Mail; use Appwrite\Event\Phone; use Appwrite\Event\Usage; +use Appwrite\Extend\Exception; use Appwrite\Platform\Appwrite; use Swoole\Runtime; use Utopia\App; @@ -226,14 +227,18 @@ $args = $_SERVER['argv']; if (isset($args[0])) { $workerName = end($args); } else { - throw new Exception('Missing worker name'); + Console::error('Missing worker name'); } -$platform->init(Service::TYPE_WORKER, [ - 'workersNumber' => swoole_cpu_num() * intval(App::getEnv('_APP_WORKER_PER_CORE', 6)), - 'connection' => $pools->get('queue')->pop()->getResource(), - 'workerName' => $workerName, -]); +try { + $platform->init(Service::TYPE_WORKER, [ + 'workersNum' => swoole_cpu_num() * intval(App::getEnv('_APP_WORKER_PER_CORE', 6)), + 'connection' => $pools->get('queue')->pop()->getResource(), + 'workerName' => $workerName ?? null, + ]); +} catch (\Exception $e) { + Console::error($e->getMessage() . ', File: '.$e->getFile(). ', Line: '.$e->getLine()); +} $worker = $platform->getWorker(); diff --git a/bin/worker-audits b/bin/worker-audits index b737ebb29..3df65d65e 100644 --- a/bin/worker-audits +++ b/bin/worker-audits @@ -1,3 +1,3 @@ #!/bin/sh -QUEUE=v1-audits php /usr/src/code/app/worker.php audits $@ \ No newline at end of file +php /usr/src/code/app/worker.php audits $@ \ No newline at end of file diff --git a/bin/worker-mails b/bin/worker-mails index 0e0918ee3..fee8a96da 100644 --- a/bin/worker-mails +++ b/bin/worker-mails @@ -1,3 +1,3 @@ #!/bin/sh -QUEUE=v1-mails php /usr/src/code/app/worker.php mails $@ \ No newline at end of file +php /usr/src/code/app/worker.php mails $@ \ No newline at end of file diff --git a/bin/worker-messaging b/bin/worker-messaging index b61cf310c..e6edf80f0 100644 --- a/bin/worker-messaging +++ b/bin/worker-messaging @@ -1,3 +1,3 @@ #!/bin/sh -QUEUE=v1-messaging php /usr/src/code/app/worker.php messaging $@ \ No newline at end of file +php /usr/src/code/app/worker.php messaging $@ \ No newline at end of file diff --git a/bin/worker-webhooks b/bin/worker-webhooks index e2ab19874..93f8027a8 100644 --- a/bin/worker-webhooks +++ b/bin/worker-webhooks @@ -1,3 +1,3 @@ #!/bin/sh -QUEUE=v1-webhooks php /usr/src/code/app/worker.php webhooks $@ \ No newline at end of file +php /usr/src/code/app/worker.php webhooks $@ \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index c9d984452..658912aa6 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -828,18 +828,6 @@ services: # - REDIS_HOSTS=redis # ports: # - "8081:8081" - - # resque: - # image: appwrite/resque-web:1.1.0 - # networks: - # - appwrite - # ports: - # - "5678:5678" - # environment: - # - RESQUE_WEB_HOST=redis - # - RESQUE_WEB_PORT=6379 - # - RESQUE_WEB_HTTP_BASIC_AUTH_USER=user - # - RESQUE_WEB_HTTP_BASIC_AUTH_PASSWORD=password # webgrind: # image: 'jokkedk/webgrind:latest' # volumes: diff --git a/src/Appwrite/Event/Audit.php b/src/Appwrite/Event/Audit.php index 08fd3cad3..2321fe77d 100644 --- a/src/Appwrite/Event/Audit.php +++ b/src/Appwrite/Event/Audit.php @@ -14,7 +14,12 @@ class Audit extends Event public function __construct(protected Connection $connection) { - parent::__construct(Event::AUDITS_QUEUE_NAME, Event::AUDITS_CLASS_NAME, $connection); + parent::__construct($connection); + + $this + ->setQueue(Event::AUDITS_QUEUE_NAME) + ->setClass(Event::BUILDS_CLASS_NAME); + } /** diff --git a/src/Appwrite/Event/Build.php b/src/Appwrite/Event/Build.php index 1009dcedf..6eb553d27 100644 --- a/src/Appwrite/Event/Build.php +++ b/src/Appwrite/Event/Build.php @@ -14,7 +14,11 @@ class Build extends Event public function __construct(protected Connection $connection) { - parent::__construct(Event::BUILDS_QUEUE_NAME, Event::BUILDS_CLASS_NAME, $connection); + parent::__construct($connection); + + $this + ->setQueue(Event::BUILDS_QUEUE_NAME) + ->setClass(Event::BUILDS_CLASS_NAME); } /** diff --git a/src/Appwrite/Event/Certificate.php b/src/Appwrite/Event/Certificate.php index 1a755b733..85058c96f 100644 --- a/src/Appwrite/Event/Certificate.php +++ b/src/Appwrite/Event/Certificate.php @@ -13,7 +13,11 @@ class Certificate extends Event public function __construct(protected Connection $connection) { - parent::__construct(Event::CERTIFICATES_QUEUE_NAME, Event::CERTIFICATES_CLASS_NAME, $connection); + parent::__construct($connection); + + $this + ->setQueue(Event::CERTIFICATES_QUEUE_NAME) + ->setClass(Event::CERTIFICATES_CLASS_NAME); } /** diff --git a/src/Appwrite/Event/Database.php b/src/Appwrite/Event/Database.php index 7d95b347c..074125566 100644 --- a/src/Appwrite/Event/Database.php +++ b/src/Appwrite/Event/Database.php @@ -15,7 +15,11 @@ class Database extends Event public function __construct(protected Connection $connection) { - parent::__construct(Event::DATABASE_QUEUE_NAME, Event::DATABASE_CLASS_NAME, $connection); + parent::__construct($connection); + + $this + ->setQueue(Event::DATABASE_QUEUE_NAME) + ->setClass(Event::DATABASE_CLASS_NAME); } /** diff --git a/src/Appwrite/Event/Delete.php b/src/Appwrite/Event/Delete.php index 32c84ee90..0bad6cb44 100644 --- a/src/Appwrite/Event/Delete.php +++ b/src/Appwrite/Event/Delete.php @@ -17,7 +17,11 @@ class Delete extends Event public function __construct(protected Connection $connection) { - parent::__construct(Event::DELETE_QUEUE_NAME, Event::DELETE_CLASS_NAME, $connection); + parent::__construct($connection); + + $this + ->setQueue(Event::DELETE_QUEUE_NAME) + ->setClass(Event::DELETE_CLASS_NAME); } /** diff --git a/src/Appwrite/Event/Event.php b/src/Appwrite/Event/Event.php index 0fecbe030..fec30d363 100644 --- a/src/Appwrite/Event/Event.php +++ b/src/Appwrite/Event/Event.php @@ -3,8 +3,9 @@ namespace Appwrite\Event; use InvalidArgumentException; -use Resque; use Utopia\Database\Document; +use Utopia\Queue\Client; +use Utopia\Queue\Connection; class Event { @@ -48,15 +49,10 @@ class Event protected ?Document $user = null; /** - * @param string $queue - * @param string $class + * @param Connection $connection * @return void */ - public function __construct(string $queue, string $class) - { - $this->queue = $queue; - $this->class = $class; - } + public function __construct(protected Connection $connection){} /** * Set queue used for this event. @@ -263,7 +259,10 @@ class Event */ public function trigger(): string|bool { - return Resque::enqueue($this->queue, $this->class, [ + + $client = new Client($this->queue, $this->connection); + + return $client->enqueue([ 'project' => $this->project, 'user' => $this->user, 'payload' => $this->payload, diff --git a/src/Appwrite/Event/Func.php b/src/Appwrite/Event/Func.php index 5f8b4c80c..8cb640bf2 100644 --- a/src/Appwrite/Event/Func.php +++ b/src/Appwrite/Event/Func.php @@ -16,7 +16,11 @@ class Func extends Event public function __construct(protected Connection $connection) { - parent::__construct(Event::FUNCTIONS_QUEUE_NAME, Event::FUNCTIONS_CLASS_NAME); + parent::__construct($connection); + + $this + ->setQueue(Event::FUNCTIONS_QUEUE_NAME) + ->setClass(Event::FUNCTIONS_CLASS_NAME); } /** diff --git a/src/Appwrite/Event/Mail.php b/src/Appwrite/Event/Mail.php index 7c1a7b53b..6dea5f269 100644 --- a/src/Appwrite/Event/Mail.php +++ b/src/Appwrite/Event/Mail.php @@ -17,7 +17,11 @@ class Mail extends Event public function __construct(protected Connection $connection) { - parent::__construct(Event::MAILS_QUEUE_NAME, Event::MAILS_CLASS_NAME, $connection); + parent::__construct($connection); + + $this + ->setQueue(Event::MAILS_QUEUE_NAME) + ->setClass(Event::MAILS_CLASS_NAME); } /** diff --git a/src/Appwrite/Event/Phone.php b/src/Appwrite/Event/Phone.php index c18b861e2..9f27345a9 100644 --- a/src/Appwrite/Event/Phone.php +++ b/src/Appwrite/Event/Phone.php @@ -12,7 +12,11 @@ class Phone extends Event public function __construct(protected Connection $connection) { - parent::__construct(Event::MESSAGING_QUEUE_NAME, Event::MESSAGING_CLASS_NAME, $connection); + parent::__construct($connection); + + $this + ->setQueue(Event::MESSAGING_QUEUE_NAME) + ->setClass(Event::MESSAGING_CLASS_NAME); } /** diff --git a/src/Appwrite/Event/Usage.php b/src/Appwrite/Event/Usage.php index b302b8880..398c3319f 100644 --- a/src/Appwrite/Event/Usage.php +++ b/src/Appwrite/Event/Usage.php @@ -13,7 +13,11 @@ class Usage extends Event public function __construct(protected Connection $connection) { - parent::__construct(Event::USAGE_QUEUE_NAME, Event::USAGE_CLASS_NAME); + parent::__construct($connection); + + $this + ->setQueue(Event::USAGE_QUEUE_NAME) + ->setClass(Event::USAGE_CLASS_NAME); } /** diff --git a/src/Appwrite/Platform/Workers/Audits.php b/src/Appwrite/Platform/Workers/Audits.php index 84a15936c..8c44f70d8 100644 --- a/src/Appwrite/Platform/Workers/Audits.php +++ b/src/Appwrite/Platform/Workers/Audits.php @@ -17,6 +17,9 @@ class Audits extends Action return 'audits'; } + /** + * @throws Exception + */ public function __construct() { $this @@ -28,12 +31,14 @@ class Audits extends Action } - + /** + * @throws Exception + */ public function action(Message $message, $dbForProject): void { $payload = $message->getPayload() ?? []; - + var_dump('audits worker'); if (empty($payload)) { throw new Exception('Missing payload'); } diff --git a/src/Appwrite/Platform/Workers/Mails.php b/src/Appwrite/Platform/Workers/Mails.php index 8c9c20f51..b843d8490 100644 --- a/src/Appwrite/Platform/Workers/Mails.php +++ b/src/Appwrite/Platform/Workers/Mails.php @@ -19,6 +19,9 @@ class Mails extends Action return 'mails'; } + /** + * @throws Exception + */ public function __construct() { $this @@ -28,11 +31,15 @@ class Mails extends Action ->callback(fn($message, $register) => $this->action($message, $register)); } + /** + * @throws \PHPMailer\PHPMailer\Exception + * @throws Exception + */ public function action(Message $message, $register): void { $payload = $message->getPayload() ?? []; - + var_dump('mails worker'); if (empty($payload)) { throw new Exception('Missing payload'); } diff --git a/src/Appwrite/Platform/Workers/Messaging.php b/src/Appwrite/Platform/Workers/Messaging.php index a75befaff..ea4e130ff 100644 --- a/src/Appwrite/Platform/Workers/Messaging.php +++ b/src/Appwrite/Platform/Workers/Messaging.php @@ -28,6 +28,9 @@ class Messaging extends Action return 'messaging'; } + /** + * @throws Exception + */ public function __construct() { $this->provider = App::getEnv('_APP_SMS_PROVIDER', ''); @@ -43,9 +46,11 @@ class Messaging extends Action ->callback(fn($message) => $this->action($message)); } + /** + * @throws Exception + */ public function action(Message $message): void { - $payload = $message->getPayload() ?? []; if (empty($payload)) { diff --git a/src/Appwrite/Platform/Workers/Webhooks.php b/src/Appwrite/Platform/Workers/Webhooks.php index 144da99aa..4985c0bf0 100644 --- a/src/Appwrite/Platform/Workers/Webhooks.php +++ b/src/Appwrite/Platform/Workers/Webhooks.php @@ -25,10 +25,13 @@ class Webhooks extends Action ->callback(fn($message) => $this->action($message)); } + /** + * @throws Exception + */ public function action(Message $message): void { $payload = $message->getPayload() ?? []; - var_dump('webhooks'); + var_dump('webhooks action'); if (empty($payload)) { throw new Exception('Missing payload');