appwrite/src/Appwrite/Resque/Worker.php

136 lines
3.2 KiB
PHP
Raw Normal View History

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