diff --git a/composer.lock b/composer.lock index 3bc02c3..87d6acc 100644 --- a/composer.lock +++ b/composer.lock @@ -466,16 +466,16 @@ }, { "name": "phpunit/php-code-coverage", - "version": "9.2.27", + "version": "9.2.29", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/php-code-coverage.git", - "reference": "b0a88255cb70d52653d80c890bd7f38740ea50d1" + "reference": "6a3a87ac2bbe33b25042753df8195ba4aa534c76" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/b0a88255cb70d52653d80c890bd7f38740ea50d1", - "reference": "b0a88255cb70d52653d80c890bd7f38740ea50d1", + "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/6a3a87ac2bbe33b25042753df8195ba4aa534c76", + "reference": "6a3a87ac2bbe33b25042753df8195ba4aa534c76", "shasum": "" }, "require": { @@ -532,7 +532,7 @@ "support": { "issues": "https://github.com/sebastianbergmann/php-code-coverage/issues", "security": "https://github.com/sebastianbergmann/php-code-coverage/security/policy", - "source": "https://github.com/sebastianbergmann/php-code-coverage/tree/9.2.27" + "source": "https://github.com/sebastianbergmann/php-code-coverage/tree/9.2.29" }, "funding": [ { @@ -540,7 +540,7 @@ "type": "github" } ], - "time": "2023-07-26T13:44:30+00:00" + "time": "2023-09-19T04:57:46+00:00" }, { "name": "phpunit/php-file-iterator", @@ -785,16 +785,16 @@ }, { "name": "phpunit/phpunit", - "version": "9.6.11", + "version": "9.6.13", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/phpunit.git", - "reference": "810500e92855eba8a7a5319ae913be2da6f957b0" + "reference": "f3d767f7f9e191eab4189abe41ab37797e30b1be" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/810500e92855eba8a7a5319ae913be2da6f957b0", - "reference": "810500e92855eba8a7a5319ae913be2da6f957b0", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/f3d767f7f9e191eab4189abe41ab37797e30b1be", + "reference": "f3d767f7f9e191eab4189abe41ab37797e30b1be", "shasum": "" }, "require": { @@ -809,7 +809,7 @@ "phar-io/manifest": "^2.0.3", "phar-io/version": "^3.0.2", "php": ">=7.3", - "phpunit/php-code-coverage": "^9.2.13", + "phpunit/php-code-coverage": "^9.2.28", "phpunit/php-file-iterator": "^3.0.5", "phpunit/php-invoker": "^3.1.1", "phpunit/php-text-template": "^2.0.3", @@ -868,7 +868,7 @@ "support": { "issues": "https://github.com/sebastianbergmann/phpunit/issues", "security": "https://github.com/sebastianbergmann/phpunit/security/policy", - "source": "https://github.com/sebastianbergmann/phpunit/tree/9.6.11" + "source": "https://github.com/sebastianbergmann/phpunit/tree/9.6.13" }, "funding": [ { @@ -884,7 +884,7 @@ "type": "tidelift" } ], - "time": "2023-08-19T07:10:56+00:00" + "time": "2023-09-19T05:39:22+00:00" }, { "name": "sebastian/cli-parser", @@ -1912,5 +1912,5 @@ "ext-redis": "*" }, "platform-dev": [], - "plugin-api-version": "2.3.0" + "plugin-api-version": "2.2.0" } diff --git a/src/Platform/Action.php b/src/Platform/Action.php index 764b2ed..4c628d2 100644 --- a/src/Platform/Action.php +++ b/src/Platform/Action.php @@ -37,6 +37,8 @@ abstract class Action public const TYPE_OPTIONS = 'Options'; + public const TYPE_WORKER_START = 'WorkerStart'; + protected ?string $desc = null; protected array $groups = []; diff --git a/src/Platform/Platform.php b/src/Platform/Platform.php index 58b98b8..7332222 100644 --- a/src/Platform/Platform.php +++ b/src/Platform/Platform.php @@ -5,6 +5,8 @@ use Exception; use Utopia\App; use Utopia\CLI\CLI; +use Utopia\Queue\Adapter\Swoole; +use Utopia\Queue\Server; use Utopia\Route; abstract class Platform @@ -14,16 +16,19 @@ abstract class Platform Service::TYPE_CLI => [], Service::TYPE_HTTP => [], Service::TYPE_GRAPHQL => [], + Service::TYPE_WORKER => [], ]; protected CLI $cli; + protected Server $worker; + /** * Initialize Application * * @return void */ - public function init(string $type): void + public function init(string $type, array $params = []): void { switch ($type) { case Service::TYPE_HTTP: @@ -35,6 +40,9 @@ public function init(string $type): void case Service::TYPE_GRAPHQL: $this->initGraphQL(); break; + case Service::TYPE_WORKER: + $this->initWorker($params); + break; default: throw new Exception('Please provide which type of initialization you want to carry out.'); } @@ -151,6 +159,69 @@ protected function initCLI(): void } } + /** + * Init worker Services + * + * @param array $params + * @return void + */ + protected function initWorker(array $params): void + { + $connection = $params['connection'] ?? null; + $workersNum = $params['workersNum'] ?? 0; + $workerName = $params['workerName'] ?? null; + $queueName = $params['queueName'] ?? 'v1-'.$workerName; + $adapter = new Swoole($connection, $workersNum, $queueName); + $this->worker ??= new Server($adapter); + foreach ($this->services[Service::TYPE_WORKER] as $service) { + foreach ($service->getActions() as $key => $action) { + if (! str_contains(strtolower($key), $workerName)) { + continue; + } + + switch ($action->getType()) { + case Action::TYPE_INIT: + $hook = $this->worker->init(); + break; + case Action::TYPE_ERROR: + $hook = $this->worker->error(); + break; + case Action::TYPE_SHUTDOWN: + $hook = $this->worker->shutdown(); + break; + case Action::TYPE_WORKER_START: + $hook = $this->worker->workerStart(); + break; + case Action::TYPE_DEFAULT: + default: + $hook = $this->worker->job(); + break; + } + $hook + ->groups($action->getGroups()) + ->desc($action->getDesc() ?? ''); + + foreach ($action->getOptions() as $key => $option) { + switch ($option['type']) { + case 'param': + $key = substr($key, stripos($key, ':') + 1); + $hook->param($key, $option['default'], $option['validator'], $option['description'], $option['optional'], $option['injections']); + break; + case 'injection': + $hook->inject($option['name']); + break; + } + } + + foreach ($action->getLabels() as $key => $label) { + $hook->label($key, $label); + } + + $hook->action($action->getCallback()); + } + } + } + /** * Initialize GraphQL Services * @@ -192,9 +263,9 @@ public function removeService(string $key): Platform * Get Service * * @param string $key - * @return Service + * @return Service|null */ - public function getService(string $key): Service + public function getService(string $key): ?Service { if (empty($this->services['all'][$key])) { throw new Exception('Service '.$key.' not found'); @@ -230,4 +301,22 @@ public function setCli(CLI $cli): self return $this; } + + /** + * Get the value of worker + */ + public function getWorker(): Server + { + return $this->worker; + } + + /** + * Set the value of worker + */ + public function setWorker(Server $worker): self + { + $this->worker = $worker; + + return $this; + } } diff --git a/src/Platform/Service.php b/src/Platform/Service.php index efbfa07..153b7b5 100644 --- a/src/Platform/Service.php +++ b/src/Platform/Service.php @@ -10,6 +10,8 @@ abstract class Service public const TYPE_CLI = 'CLI'; + public const TYPE_WORKER = 'Worker'; + protected array $actions; protected string $type;