2021-03-10 21:08:17 +13:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Appwrite\Resque;
|
|
|
|
|
2021-12-06 02:07:45 +13:00
|
|
|
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
|
|
|
|
*/
|
2021-12-06 02:07:45 +13:00
|
|
|
static protected array $errorCallbacks = [];
|
2021-12-01 01:12:17 +13:00
|
|
|
|
2021-11-24 23:09:10 +13:00
|
|
|
/**
|
2021-12-06 00:08:08 +13:00
|
|
|
* Associative array holding all information passed into the worker
|
2021-11-24 23:09:10 +13:00
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2021-09-01 21:09:04 +12:00
|
|
|
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
|
2021-12-06 02:07:45 +13:00
|
|
|
* @throws Exception
|
2021-11-24 23:09:10 +13:00
|
|
|
*/
|
2021-12-06 02:07:45 +13:00
|
|
|
public function getName(): string
|
|
|
|
{
|
|
|
|
throw new Exception("Please implement getName method in worker");
|
|
|
|
}
|
2021-11-24 03:24:25 +13:00
|
|
|
|
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
|
|
|
|
*/
|
2021-12-06 02:07:45 +13:00
|
|
|
public function init() {
|
|
|
|
throw new Exception("Please implement getName method in worker");
|
|
|
|
}
|
2021-06-12 02:20:18 +12:00
|
|
|
|
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
|
|
|
|
*/
|
2021-12-06 02:07:45 +13:00
|
|
|
public function run() {
|
|
|
|
throw new Exception("Please implement getName method in worker");
|
|
|
|
}
|
2021-06-12 02:20:18 +12:00
|
|
|
|
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
|
|
|
|
*/
|
2021-12-06 02:07:45 +13:00
|
|
|
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
|
|
|
|
{
|
2021-11-24 03:24:25 +13:00
|
|
|
try {
|
|
|
|
$this->init();
|
|
|
|
} catch(\Throwable $error) {
|
2021-12-01 01:12:17 +13:00
|
|
|
foreach ($this->errorCallbacks as $errorCallback) {
|
|
|
|
$errorCallback($error, "init");
|
2021-11-26 03:13:14 +13:00
|
|
|
}
|
2021-11-24 03:24:25 +13:00
|
|
|
|
|
|
|
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
|
|
|
|
*/
|
2021-09-01 21:09:04 +12:00
|
|
|
public function perform(): void
|
2021-03-10 21:08:17 +13:00
|
|
|
{
|
2021-11-24 03:24:25 +13:00
|
|
|
try {
|
|
|
|
$this->run();
|
|
|
|
} catch(\Throwable $error) {
|
2021-12-01 01:12:17 +13:00
|
|
|
foreach ($this->errorCallbacks as $errorCallback) {
|
|
|
|
$errorCallback($error, "run");
|
2021-11-26 03:13:14 +13:00
|
|
|
}
|
2021-11-24 03:24:25 +13:00
|
|
|
|
|
|
|
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
|
|
|
|
{
|
2021-11-24 03:24:25 +13:00
|
|
|
try {
|
|
|
|
$this->shutdown();
|
|
|
|
} catch(\Throwable $error) {
|
2021-12-01 01:12:17 +13:00
|
|
|
foreach ($this->errorCallbacks as $errorCallback) {
|
|
|
|
$errorCallback($error, "shutdown");
|
2021-11-26 03:13:14 +13:00
|
|
|
}
|
2021-11-24 03:24:25 +13:00
|
|
|
|
|
|
|
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
|
|
|
|
*/
|
2021-12-06 02:07:45 +13:00
|
|
|
public static function error(callable $callback): void
|
2021-12-01 01:12:17 +13:00
|
|
|
{
|
2021-12-06 02:07:45 +13:00
|
|
|
\array_push(self::$errorCallbacks, $callback);
|
2021-12-01 01:12:17 +13:00
|
|
|
}
|
2021-03-10 21:08:17 +13:00
|
|
|
}
|