Documented worker class
This commit is contained in:
parent
5b08969133
commit
fd1dcae247
1 changed files with 52 additions and 4 deletions
|
@ -5,20 +5,56 @@ namespace Appwrite\Resque;
|
||||||
use Utopia\App;
|
use Utopia\App;
|
||||||
use Utopia\CLI\Console;
|
use Utopia\CLI\Console;
|
||||||
use Utopia\Logger\Log;
|
use Utopia\Logger\Log;
|
||||||
use function get_class;
|
|
||||||
|
|
||||||
abstract class Worker
|
abstract class Worker
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* Named array holding all information passed into worker alongside a new task.
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
public array $args = [];
|
public array $args = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Function for identifying the worker needs to be set to unique name
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
abstract public function getName(): 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;
|
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;
|
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;
|
abstract public function shutdown(): void;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A wrapper around 'init' function with non-worker-specific code
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
* @throws \Exception|\Throwable
|
||||||
|
*/
|
||||||
public function setUp(): void
|
public function setUp(): void
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
|
@ -41,7 +77,7 @@ abstract class Worker
|
||||||
$log->setTags([
|
$log->setTags([
|
||||||
'worker_type' => $workerType,
|
'worker_type' => $workerType,
|
||||||
'code' => $error->getCode(),
|
'code' => $error->getCode(),
|
||||||
'verbose_type' => get_class($error),
|
'verbose_type' => \get_class($error),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$log->addExtra('file', $error->getFile());
|
$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
|
public function perform(): void
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
|
@ -85,7 +127,7 @@ abstract class Worker
|
||||||
$log->setTags([
|
$log->setTags([
|
||||||
'worker_type' => $workerType,
|
'worker_type' => $workerType,
|
||||||
'code' => $error->getCode(),
|
'code' => $error->getCode(),
|
||||||
'verbose_type' => get_class($error),
|
'verbose_type' => \get_class($error),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$log->addExtra('file', $error->getFile());
|
$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
|
public function tearDown(): void
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
|
@ -128,7 +176,7 @@ abstract class Worker
|
||||||
$log->setTags([
|
$log->setTags([
|
||||||
'worker_type' => $workerType,
|
'worker_type' => $workerType,
|
||||||
'code' => $error->getCode(),
|
'code' => $error->getCode(),
|
||||||
'verbose_type' => get_class($error),
|
'verbose_type' => \get_class($error),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$log->addExtra('file', $error->getFile());
|
$log->addExtra('file', $error->getFile());
|
||||||
|
|
Loading…
Reference in a new issue