1
0
Fork 0
mirror of synced 2024-06-27 02:31:04 +12:00

Documented worker class

This commit is contained in:
Matej Baco 2021-11-24 11:09:10 +01:00
parent 5b08969133
commit fd1dcae247

View file

@ -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());