appwrite/src/Appwrite/Resque/Worker.php

160 lines
4.1 KiB
PHP
Raw Normal View History

2021-03-10 08:08:17 +00:00
<?php
namespace Appwrite\Resque;
abstract class Worker
{
2021-11-30 12:12:17 +00:00
/**
* Callbacks that will be executed when an error occurs
*
* @var array
*/
protected $errorCallbacks = [];
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
*/
2021-11-24 09:38:32 +00:00
abstract public function getName(): string;
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-03-10 08:08:17 +00:00
abstract public function init(): void;
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
*/
abstract public function run(): void;
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-03-10 08:08:17 +00:00
abstract public function shutdown(): void;
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-11-30 12:12:17 +00:00
foreach ($this->errorCallbacks as $errorCallback) {
$errorCallback($error, "init");
}
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-11-30 12:12:17 +00:00
foreach ($this->errorCallbacks as $errorCallback) {
$errorCallback($error, "run");
}
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-11-30 12:12:17 +00:00
foreach ($this->errorCallbacks as $errorCallback) {
$errorCallback($error, "shutdown");
}
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 function error(callable $callback): self
{
\array_push($this->errorCallbacks, $callback);
return $this;
}
// TODO: Implement this on init file using Worker->error(function() { HERE })
//global $register;
//$logger = $register->get('logger');
//
//if($logger) {
//$version = App::getEnv('_APP_VERSION', 'UNKNOWN');
//$workerType = $this->getName();
//
//$log = new Log();
//
//$log->setNamespace("worker-" . $workerType);
//$log->setServer(\gethostname());
//$log->setVersion($version);
//$log->setType(Log::TYPE_ERROR);
//$log->setMessage($error->getMessage());
//
2021-12-01 10:59:08 +00:00
//$log->addTag('workerType', $workerType);
2021-11-30 12:12:17 +00:00
//$log->addTag('code', $error->getCode());
2021-12-01 10:59:08 +00:00
//$log->addTag('verboseType', \get_class($error));
2021-11-30 12:12:17 +00:00
//
//$log->addExtra('file', $error->getFile());
//$log->addExtra('line', $error->getLine());
//$log->addExtra('trace', $error->getTraceAsString());
//$log->addExtra('args', $this->args);
//
//$action = 'worker.' . $workerType . '.setUp';
//$log->setAction($action);
//
//$isProduction = App::getEnv('_APP_ENV', 'development') === 'production';
//$log->setEnvironment($isProduction ? Log::ENVIRONMENT_PRODUCTION : Log::ENVIRONMENT_STAGING);
//
//$responseCode = $logger->addLog($log);
//Console::info('Setup log pushed with status code: '.$responseCode);
//}
2021-03-10 08:08:17 +00:00
}