From fd1dcae24709251695243d2a3bc31fb2fae288d6 Mon Sep 17 00:00:00 2001 From: Matej Baco Date: Wed, 24 Nov 2021 11:09:10 +0100 Subject: [PATCH] Documented worker class --- src/Appwrite/Resque/Worker.php | 56 +++++++++++++++++++++++++++++++--- 1 file changed, 52 insertions(+), 4 deletions(-) diff --git a/src/Appwrite/Resque/Worker.php b/src/Appwrite/Resque/Worker.php index 9c03b92386..8e99b37086 100644 --- a/src/Appwrite/Resque/Worker.php +++ b/src/Appwrite/Resque/Worker.php @@ -5,20 +5,56 @@ namespace Appwrite\Resque; use Utopia\App; use Utopia\CLI\Console; use Utopia\Logger\Log; -use function get_class; abstract class Worker { + /** + * Named array holding all information passed into worker alongside a new task. + * + * @return array + */ public array $args = []; + /** + * Function for identifying the worker needs to be set to unique name + * + * @return string + */ abstract public function getName(): string; + /** + * Function executed before running first task. + * Can include any preparations, such as connecting to external services or loading files + * + * @return void + * @throws \Exception|\Throwable + */ abstract public function init(): void; + /** + * Function executed when new task requests is received. + * You can access $args here, it will contain event information + * + * @return void + * @throws \Exception|\Throwable + */ abstract public function run(): void; + /** + * Function executed just before shutting down the worker. + * You can do cleanup here, such as disconnecting from services or removing temp files + * + * @return void + * @throws \Exception|\Throwable + */ abstract public function shutdown(): void; + /** + * A wrapper around 'init' function with non-worker-specific code + * + * @return void + * @throws \Exception|\Throwable + */ public function setUp(): void { try { @@ -41,7 +77,7 @@ abstract class Worker $log->setTags([ 'worker_type' => $workerType, 'code' => $error->getCode(), - 'verbose_type' => get_class($error), + 'verbose_type' => \get_class($error), ]); $log->addExtra('file', $error->getFile()); @@ -64,6 +100,12 @@ abstract class Worker } } + /** + * A wrapper around 'run' function with non-worker-specific code + * + * @return void + * @throws \Exception|\Throwable + */ public function perform(): void { try { @@ -85,7 +127,7 @@ abstract class Worker $log->setTags([ 'worker_type' => $workerType, 'code' => $error->getCode(), - 'verbose_type' => get_class($error), + 'verbose_type' => \get_class($error), ]); $log->addExtra('file', $error->getFile()); @@ -107,6 +149,12 @@ abstract class Worker } } + /** + * A wrapper around 'shutdown' function with non-worker-specific code + * + * @return void + * @throws \Exception|\Throwable + */ public function tearDown(): void { try { @@ -128,7 +176,7 @@ abstract class Worker $log->setTags([ 'worker_type' => $workerType, 'code' => $error->getCode(), - 'verbose_type' => get_class($error), + 'verbose_type' => \get_class($error), ]); $log->addExtra('file', $error->getFile());