2021-03-10 08:08:17 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Appwrite\Resque;
|
|
|
|
|
|
2021-12-05 13:07:45 +00:00
|
|
|
use Exception;
|
|
|
|
|
|
|
|
|
|
class Worker
|
2021-03-10 08:08:17 +00:00
|
|
|
{
|
2021-11-30 12:12:17 +00:00
|
|
|
/**
|
|
|
|
|
* Callbacks that will be executed when an error occurs
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
2021-12-05 13:07:45 +00:00
|
|
|
static protected array $errorCallbacks = [];
|
2021-11-30 12:12:17 +00:00
|
|
|
|
2021-11-24 10:09:10 +00:00
|
|
|
/**
|
2021-12-05 11:08:08 +00:00
|
|
|
* Associative array holding all information passed into the worker
|
2021-11-24 10:09:10 +00:00
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
2021-09-01 09:09:04 +00:00
|
|
|
public array $args = [];
|
2021-03-10 08:08:17 +00:00
|
|
|
|
2021-11-24 10:09:10 +00:00
|
|
|
/**
|
|
|
|
|
* Function for identifying the worker needs to be set to unique name
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
2021-12-05 13:07:45 +00:00
|
|
|
* @throws Exception
|
2021-11-24 10:09:10 +00:00
|
|
|
*/
|
2021-12-05 13:07:45 +00:00
|
|
|
public function getName(): string
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("Please implement getName method in worker");
|
|
|
|
|
}
|
2021-11-23 14:24:25 +00:00
|
|
|
|
2021-11-24 10:09:10 +00: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-05 13:07:45 +00:00
|
|
|
public function init() {
|
|
|
|
|
throw new Exception("Please implement getName method in worker");
|
|
|
|
|
}
|
2021-06-11 14:20:18 +00:00
|
|
|
|
2021-11-24 10:09:10 +00: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-05 13:07:45 +00:00
|
|
|
public function run() {
|
|
|
|
|
throw new Exception("Please implement getName method in worker");
|
|
|
|
|
}
|
2021-06-11 14:20:18 +00:00
|
|
|
|
2021-11-24 10:09:10 +00: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-05 13:07:45 +00:00
|
|
|
public function shutdown() {
|
|
|
|
|
throw new Exception("Please implement getName method in worker");
|
|
|
|
|
}
|
2021-03-10 08:08:17 +00:00
|
|
|
|
2021-11-24 10:09:10 +00:00
|
|
|
/**
|
|
|
|
|
* A wrapper around 'init' function with non-worker-specific code
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
* @throws \Exception|\Throwable
|
|
|
|
|
*/
|
2021-03-10 08:08:17 +00:00
|
|
|
public function setUp(): void
|
|
|
|
|
{
|
2021-11-23 14:24:25 +00:00
|
|
|
try {
|
|
|
|
|
$this->init();
|
|
|
|
|
} catch(\Throwable $error) {
|
2021-12-06 12:55:59 +00:00
|
|
|
foreach (self::$errorCallbacks as $errorCallback) {
|
|
|
|
|
$errorCallback($error, "init", $this->getName());
|
2021-11-25 14:13:14 +00:00
|
|
|
}
|
2021-11-23 14:24:25 +00:00
|
|
|
|
|
|
|
|
throw $error;
|
|
|
|
|
}
|
2021-03-10 08:08:17 +00:00
|
|
|
}
|
|
|
|
|
|
2021-11-24 10:09:10 +00:00
|
|
|
/**
|
|
|
|
|
* A wrapper around 'run' function with non-worker-specific code
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
* @throws \Exception|\Throwable
|
|
|
|
|
*/
|
2021-09-01 09:09:04 +00:00
|
|
|
public function perform(): void
|
2021-03-10 08:08:17 +00:00
|
|
|
{
|
2021-11-23 14:24:25 +00:00
|
|
|
try {
|
|
|
|
|
$this->run();
|
|
|
|
|
} catch(\Throwable $error) {
|
2021-12-06 12:55:59 +00:00
|
|
|
foreach (self::$errorCallbacks as $errorCallback) {
|
|
|
|
|
$errorCallback($error, "run", $this->getName(), $this->args);
|
2021-11-25 14:13:14 +00:00
|
|
|
}
|
2021-11-23 14:24:25 +00:00
|
|
|
|
|
|
|
|
throw $error;
|
|
|
|
|
}
|
2021-03-10 08:08:17 +00:00
|
|
|
}
|
|
|
|
|
|
2021-11-24 10:09:10 +00:00
|
|
|
/**
|
|
|
|
|
* A wrapper around 'shutdown' function with non-worker-specific code
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
* @throws \Exception|\Throwable
|
|
|
|
|
*/
|
2021-03-10 08:08:17 +00:00
|
|
|
public function tearDown(): void
|
|
|
|
|
{
|
2021-11-23 14:24:25 +00:00
|
|
|
try {
|
|
|
|
|
$this->shutdown();
|
|
|
|
|
} catch(\Throwable $error) {
|
2021-12-06 12:55:59 +00:00
|
|
|
foreach (self::$errorCallbacks as $errorCallback) {
|
|
|
|
|
$errorCallback($error, "shutdown", $this->getName());
|
2021-11-25 14:13:14 +00:00
|
|
|
}
|
2021-11-23 14:24:25 +00:00
|
|
|
|
|
|
|
|
throw $error;
|
|
|
|
|
}
|
2021-03-10 08:08:17 +00:00
|
|
|
}
|
2021-11-30 12:12:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Register callback. Will be executed when error occurs.
|
|
|
|
|
* @param callable $callback
|
|
|
|
|
* @param Throwable $error
|
|
|
|
|
* @return self
|
|
|
|
|
*/
|
2021-12-05 13:07:45 +00:00
|
|
|
public static function error(callable $callback): void
|
2021-11-30 12:12:17 +00:00
|
|
|
{
|
2021-12-05 13:07:45 +00:00
|
|
|
\array_push(self::$errorCallbacks, $callback);
|
2021-11-30 12:12:17 +00:00
|
|
|
}
|
2021-03-10 08:08:17 +00:00
|
|
|
}
|