1
0
Fork 0
mirror of synced 2024-09-30 09:18:14 +13:00
appwrite/src/Appwrite/Resque/Worker.php

136 lines
3.2 KiB
PHP
Raw Normal View History

2021-03-10 21:08:17 +13:00
<?php
namespace Appwrite\Resque;
use Exception;
class Worker
2021-03-10 21:08:17 +13:00
{
2021-12-01 01:12:17 +13:00
/**
* Callbacks that will be executed when an error occurs
*
* @var array
*/
static protected array $errorCallbacks = [];
2021-12-01 01:12:17 +13:00
2021-11-24 23:09:10 +13:00
/**
* Associative array holding all information passed into the worker
2021-11-24 23:09:10 +13:00
*
* @return array
*/
public array $args = [];
2021-03-10 21:08:17 +13:00
2021-11-24 23:09:10 +13:00
/**
* Function for identifying the worker needs to be set to unique name
*
* @return string
* @throws Exception
2021-11-24 23:09:10 +13:00
*/
public function getName(): string
{
throw new Exception("Please implement getName method in worker");
}
2021-11-24 23:09:10 +13:00
/**
* Function executed before running first task.
* Can include any preparations, such as connecting to external services or loading files
*
* @return void
* @throws \Exception|\Throwable
*/
public function init() {
throw new Exception("Please implement getName method in worker");
}
2021-11-24 23:09:10 +13:00
/**
* Function executed when new task requests is received.
* You can access $args here, it will contain event information
*
* @return void
* @throws \Exception|\Throwable
*/
public function run() {
throw new Exception("Please implement getName method in worker");
}
2021-11-24 23:09:10 +13:00
/**
* 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
*/
public function shutdown() {
throw new Exception("Please implement getName method in worker");
}
2021-03-10 21:08:17 +13:00
2021-11-24 23:09:10 +13:00
/**
* A wrapper around 'init' function with non-worker-specific code
*
* @return void
* @throws \Exception|\Throwable
*/
2021-03-10 21:08:17 +13:00
public function setUp(): void
{
try {
$this->init();
} catch(\Throwable $error) {
2021-12-01 01:12:17 +13:00
foreach ($this->errorCallbacks as $errorCallback) {
$errorCallback($error, "init");
}
throw $error;
}
2021-03-10 21:08:17 +13:00
}
2021-11-24 23:09:10 +13:00
/**
* A wrapper around 'run' function with non-worker-specific code
*
* @return void
* @throws \Exception|\Throwable
*/
public function perform(): void
2021-03-10 21:08:17 +13:00
{
try {
$this->run();
} catch(\Throwable $error) {
2021-12-01 01:12:17 +13:00
foreach ($this->errorCallbacks as $errorCallback) {
$errorCallback($error, "run");
}
throw $error;
}
2021-03-10 21:08:17 +13:00
}
2021-11-24 23:09:10 +13:00
/**
* A wrapper around 'shutdown' function with non-worker-specific code
*
* @return void
* @throws \Exception|\Throwable
*/
2021-03-10 21:08:17 +13:00
public function tearDown(): void
{
try {
$this->shutdown();
} catch(\Throwable $error) {
2021-12-01 01:12:17 +13:00
foreach ($this->errorCallbacks as $errorCallback) {
$errorCallback($error, "shutdown");
}
throw $error;
}
2021-03-10 21:08:17 +13:00
}
2021-12-01 01:12:17 +13:00
/**
* Register callback. Will be executed when error occurs.
* @param callable $callback
* @param Throwable $error
* @return self
*/
public static function error(callable $callback): void
2021-12-01 01:12:17 +13:00
{
\array_push(self::$errorCallbacks, $callback);
2021-12-01 01:12:17 +13:00
}
2021-03-10 21:08:17 +13:00
}