From de17d0d1d3ad195c84dc446f4e36205c1c2f78f8 Mon Sep 17 00:00:00 2001 From: Eldad Fux Date: Wed, 10 Mar 2021 10:08:17 +0200 Subject: [PATCH] Test worker wrapers --- app/workers/audits.php | 9 ++++--- app/workers/certificates.php | 10 ++++---- app/workers/deletes.php | 11 ++++----- app/workers/functions.php | 17 +++++++------ app/workers/mails.php | 10 ++++---- app/workers/tasks.php | 16 ++++++------ app/workers/usage.php | 10 ++++---- app/workers/webhooks.php | 10 ++++---- src/Appwrite/Resque/Worker.php | 45 ++++++++++++++++++++++++++++++++++ 9 files changed, 92 insertions(+), 46 deletions(-) create mode 100644 src/Appwrite/Resque/Worker.php diff --git a/app/workers/audits.php b/app/workers/audits.php index d6027dd2e3..13af7e828e 100644 --- a/app/workers/audits.php +++ b/app/workers/audits.php @@ -1,5 +1,6 @@ log($userId, $event, $resource, $userAgent, $ip, '', $data); } - public function tearDown(): void + public function shutdown(): void { // ... Remove environment for this job } diff --git a/app/workers/certificates.php b/app/workers/certificates.php index e8693e3566..6d08d2183d 100644 --- a/app/workers/certificates.php +++ b/app/workers/certificates.php @@ -9,6 +9,7 @@ use Appwrite\Database\Adapter\MySQL as MySQLAdapter; use Appwrite\Database\Adapter\Redis as RedisAdapter; use Appwrite\Database\Validator\Authorization; use Appwrite\Network\Validator\CNAME; +use Appwrite\Resque\Worker; require_once __DIR__.'/../init.php'; @@ -16,15 +17,15 @@ Console::title('Certificates V1 Worker'); Console::success(APP_NAME.' certificates worker v1 has started'); -class CertificatesV1 +class CertificatesV1 extends Worker { public $args = []; - public function setUp(): void + public function init(): void { } - public function perform() + public function execute(): void { global $register; @@ -204,8 +205,7 @@ class CertificatesV1 Authorization::reset(); } - public function tearDown(): void + public function shutdown(): void { - // ... Remove environment for this job } } diff --git a/app/workers/deletes.php b/app/workers/deletes.php index 426dcf1ed6..0478f7661a 100644 --- a/app/workers/deletes.php +++ b/app/workers/deletes.php @@ -5,6 +5,7 @@ use Appwrite\Database\Adapter\MySQL as MySQLAdapter; use Appwrite\Database\Adapter\Redis as RedisAdapter; use Appwrite\Database\Document; use Appwrite\Database\Validator\Authorization; +use Appwrite\Resque\Worker; use Utopia\Storage\Device\Local; use Utopia\Abuse\Abuse; use Utopia\Abuse\Adapters\TimeLimit; @@ -19,18 +20,17 @@ Console::title('Deletes V1 Worker'); Console::success(APP_NAME.' deletes worker v1 has started'."\n"); -class DeletesV1 +class DeletesV1 extends Worker { - public $args = []; protected $consoleDB = null; - public function setUp(): void + public function init(): void { } - public function perform() + public function execute(): void { $projectId = $this->args['projectId']; $type = $this->args['type']; @@ -82,9 +82,8 @@ class DeletesV1 } } - public function tearDown(): void + public function shutdown(): void { - // ... Remove environment for this job } protected function deleteDocuments(Document $document, $projectId) diff --git a/app/workers/functions.php b/app/workers/functions.php index 87e3a86245..707e6960fe 100644 --- a/app/workers/functions.php +++ b/app/workers/functions.php @@ -6,6 +6,7 @@ use Appwrite\Database\Adapter\MySQL as MySQLAdapter; use Appwrite\Database\Adapter\Redis as RedisAdapter; use Appwrite\Database\Validator\Authorization; use Appwrite\Event\Event; +use Appwrite\Resque\Worker; use Cron\CronExpression; use Swoole\Runtime; use Utopia\App; @@ -127,17 +128,17 @@ Console::info(count($list)." functions listed in " . ($executionEnd - $execution //TODO aviod scheduled execution if delay is bigger than X offest -class FunctionsV1 +class FunctionsV1 extends Worker { public $args = []; public $allowed = []; - public function setUp(): void + public function init(): void { } - public function perform() + public function execute(): void { global $register; @@ -195,7 +196,7 @@ class FunctionsV1 Console::success('Triggered function: '.$event); - $this->execute('event', $projectId, '', $database, $function, $event, $payload); + $this->run('event', $projectId, '', $database, $function, $event, $payload); } } break; @@ -251,7 +252,7 @@ class FunctionsV1 'scheduleOriginal' => $function->getAttribute('schedule', ''), ]); // Async task rescheduale - $this->execute($trigger, $projectId, $executionId, $database, $function); + $this->run($trigger, $projectId, $executionId, $database, $function); break; @@ -264,7 +265,7 @@ class FunctionsV1 throw new Exception('Function not found ('.$functionId.')'); } - $this->execute($trigger, $projectId, $executionId, $database, $function); + $this->run($trigger, $projectId, $executionId, $database, $function); break; default: @@ -286,7 +287,7 @@ class FunctionsV1 * * @return void */ - public function execute(string $trigger, string $projectId, string $executionId, Database $database, Document $function, string $event = '', string $payload = ''): void + public function run(string $trigger, string $projectId, string $executionId, Database $database, Document $function, string $event = '', string $payload = ''): void { global $list; @@ -548,7 +549,7 @@ class FunctionsV1 return $output; } - public function tearDown(): void + public function shutdown(): void { } } diff --git a/app/workers/mails.php b/app/workers/mails.php index 6f4422b57f..50e499f901 100644 --- a/app/workers/mails.php +++ b/app/workers/mails.php @@ -1,5 +1,6 @@ getAttribute('updated') !== $updated) { // Task have already been rescheduled by owner - return false; + return; } if ($task->getAttribute('status') !== 'play') { // Skip task and don't schedule again - return false; + return; } // Reschedule @@ -202,11 +203,10 @@ class TasksV1 // Send alert if needed (use SMTP as default for now) - return true; + return; } - public function tearDown(): void + public function shutdown(): void { - // ... Remove environment for this job } } diff --git a/app/workers/usage.php b/app/workers/usage.php index c83ae7ae30..1d1a5ba5a7 100644 --- a/app/workers/usage.php +++ b/app/workers/usage.php @@ -1,5 +1,6 @@ init(); + }); + } + + public function perform() + { + run(function() { + Runtime::enableCoroutine(SWOOLE_HOOK_ALL); + + $this->execute(); + }); + } + + public function tearDown(): void + { + run(function() { + Runtime::enableCoroutine(SWOOLE_HOOK_ALL); + + $this->shutdown(); + }); + } +}