From 3f199e1232e5243890d3a7204e872ea8845dd89a Mon Sep 17 00:00:00 2001 From: Torsten Dittmann Date: Fri, 11 Jun 2021 16:20:18 +0200 Subject: [PATCH 1/2] refactor(workers): create abstract worker class --- app/controllers/api/health.php | 1 - app/workers/audits.php | 12 ++++++------ app/workers/certificates.php | 21 ++++++++++----------- app/workers/deletes.php | 14 ++++++-------- app/workers/functions.php | 11 ++++++----- app/workers/mails.php | 13 ++++++------- app/workers/tasks.php | 25 ++++++++++++------------- app/workers/usage.php | 19 +++++++++---------- app/workers/webhooks.php | 13 ++++++------- src/Appwrite/Resque/Worker.php | 33 +++++++++++++++++++++++++++++++++ 10 files changed, 94 insertions(+), 68 deletions(-) create mode 100644 src/Appwrite/Resque/Worker.php diff --git a/app/controllers/api/health.php b/app/controllers/api/health.php index 0885c0c40b..dd1d4a0d69 100644 --- a/app/controllers/api/health.php +++ b/app/controllers/api/health.php @@ -6,7 +6,6 @@ use Utopia\Storage\Device\Local; use Utopia\Storage\Storage; use Appwrite\ClamAV\Network; use Appwrite\Event\Event; -use RuntimeException; App::get('/v1/health') ->desc('Get HTTP') diff --git a/app/workers/audits.php b/app/workers/audits.php index d6027dd2e3..17327a3bb8 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 } -} +} \ No newline at end of file diff --git a/app/workers/certificates.php b/app/workers/certificates.php index 90907204a3..0cb0e63c6c 100644 --- a/app/workers/certificates.php +++ b/app/workers/certificates.php @@ -1,30 +1,30 @@ args['projectId']) ? $this->args['projectId'] : ''; $type = $this->args['type']; @@ -82,9 +81,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 8bf719e607..cbe8ff4f50 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; @@ -125,17 +126,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 run(): void { global $register; @@ -579,7 +580,7 @@ class FunctionsV1 return $output; } - public function tearDown(): void + public function shutdown(): void { } -} +} \ No newline at end of file diff --git a/app/workers/mails.php b/app/workers/mails.php index 11f41e4446..e28d619811 100644 --- a/app/workers/mails.php +++ b/app/workers/mails.php @@ -1,26 +1,26 @@ 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 +202,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 } -} +} \ No newline at end of file diff --git a/app/workers/usage.php b/app/workers/usage.php index 605b8d3f4c..bf948c0388 100644 --- a/app/workers/usage.php +++ b/app/workers/usage.php @@ -1,29 +1,30 @@ get('statsd', true); $projectId = $this->args['projectId'] ?? ''; @@ -36,12 +37,12 @@ class UsageV1 $httpMethod = $this->args['httpMethod'] ?? ''; $httpRequest = $this->args['httpRequest'] ?? 0; - $functionId = $this->args['functionId']; + $functionId = $this->args['functionId'] ?? ''; $functionExecution = $this->args['functionExecution'] ?? 0; $functionExecutionTime = $this->args['functionExecutionTime'] ?? 0; $functionStatus = $this->args['functionStatus'] ?? ''; - $tags = ",project={$projectId},version=".App::getEnv('_APP_VERSION', 'UNKNOWN').''; + $tags = ",project={$projectId},version=".App::getEnv('_APP_VERSION', 'UNKNOWN'); // the global namespace is prepended to every key (optional) $statsd->setNamespace('appwrite.usage'); @@ -52,7 +53,6 @@ class UsageV1 if($functionExecution >= 1) { $statsd->increment('executions.all'.$tags.',functionId='.$functionId.',functionStatus='.$functionStatus); - var_dump($tags.',functionId='.$functionId.',functionStatus='.$functionStatus); $statsd->count('executions.time'.$tags.',functionId='.$functionId, $functionExecutionTime); } @@ -65,8 +65,7 @@ class UsageV1 } } - public function tearDown(): void + public function shutdown(): void { - // ... Remove environment for this job } -} +} \ No newline at end of file diff --git a/app/workers/webhooks.php b/app/workers/webhooks.php index 6acc38f646..a73dbb6c79 100644 --- a/app/workers/webhooks.php +++ b/app/workers/webhooks.php @@ -1,23 +1,23 @@ init(); + } + + public function perform() + { + $this->run(); + } + + public function tearDown(): void + { + $this->shutdown(); + } +} \ No newline at end of file From d96635ec23d69f2a13fe16fbe8178f22c4b2d4c7 Mon Sep 17 00:00:00 2001 From: Torsten Dittmann Date: Fri, 11 Jun 2021 16:39:44 +0200 Subject: [PATCH 2/2] fix(worker): remove unnecessary imports --- src/Appwrite/Resque/Worker.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/Appwrite/Resque/Worker.php b/src/Appwrite/Resque/Worker.php index 0707d2f87f..db8dc91ce7 100644 --- a/src/Appwrite/Resque/Worker.php +++ b/src/Appwrite/Resque/Worker.php @@ -2,10 +2,6 @@ namespace Appwrite\Resque; -use Swoole\Runtime; - -use function Swoole\Coroutine\run; - abstract class Worker { public $args = [];