From 78974509f78c95cac1aea8b6ae9564b8040a13d1 Mon Sep 17 00:00:00 2001 From: rsgrinko Date: Wed, 6 Jul 2022 17:52:27 +0300 Subject: [PATCH] =?UTF-8?q?[*]=20MQ=20=D0=BF=D0=B5=D1=80=D0=B5=D1=85=D0=BE?= =?UTF-8?q?=D0=B4=20=D0=BD=D0=B0=20=D0=BE=D1=8A=D0=B5=D0=BA=D1=82=20=D1=80?= =?UTF-8?q?=D0=B5=D1=81=D0=BF=D0=BE=D0=BD=D1=81=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/lib/Core/Models/MQ.class.php | 45 ++++--- core/lib/Core/Models/MQResponse.class.php | 150 ++++++++++++++++++++++ 2 files changed, 176 insertions(+), 19 deletions(-) create mode 100644 core/lib/Core/Models/MQResponse.class.php diff --git a/core/lib/Core/Models/MQ.class.php b/core/lib/Core/Models/MQ.class.php index 12d62c4..e2bc95e 100644 --- a/core/lib/Core/Models/MQ.class.php +++ b/core/lib/Core/Models/MQ.class.php @@ -3,7 +3,7 @@ namespace Core\Models; use Core\CoreException; - use Core\Models\DB; + use Core\Models\{DB, MQResponse}; use Core\Helpers\{SystemFunctions, Log}; class MQ @@ -81,7 +81,7 @@ $this->DB = DB::getInstance(); } - public function setRunNow(bool $runNow): self + public function setRunNow(bool $runNow = true): self { $this->runNow = $runNow; return $this; @@ -260,7 +260,7 @@ * @return int|null Идентификатор созданного задания * @throws CoreException */ - public function createTask(?string $class = null, string $method, ?array $params = null): ?int + public function createTask(?string $class = null, string $method, ?array $params = null): MQResponse { if (empty($params)) { $params = []; @@ -300,11 +300,18 @@ ); if ($this->runNow) { - $this->execute($taskId); + return $this->execute($taskId); } + $response = new MQResponse(); + $response->setTaskId($taskId) + ->setStatus(self::STATUS_OK) + ->setParams(self::convertFromJson($params)) + ->setParamsJson($params) + ->setResponse('Task ' . $taskId . ' created'); - return $taskId; + + return $response; } /** @@ -349,16 +356,22 @@ * @return bool Флаг результата выполнения задания * @throws CoreException */ - public function execute(int $taskId): bool + public function execute(int $taskId): MQResponse { - $executeStatus = true; $arTask = $this->DB->getItem(self::TABLE, ['id' => $taskId]); if (empty($arTask) || $arTask['in_progress'] === self::VALUE_Y) { // Данное задание уже выполнено или выполняется другим воркером return false; } + $response = new MQResponse(); + $response->setTaskId($taskId) + ->setParamsJson($arTask['params']); + $arTask['params'] = $this->convertFromJson($arTask['params']); + + $response->setParams($arTask['params']); + $this->DB->update( self::TABLE, ['id' => $taskId], [ 'active' => self::VALUE_Y, @@ -401,6 +414,7 @@ 'response' => $this->convertToJson($result), ] ); + $response->setStatus(self::STATUS_OK)->setResponse($this->convertToJson($result)); $this->saveExecutedTask($taskId); @@ -425,7 +439,9 @@ 'response' => $t->getMessage(), ] ); - $executeStatus = false; + $response->setStatus(self::STATUS_ERROR)->setResponse($t->getMessage()); + + Log::logToFile( 'Ошибка выполнения задания с ID ' . $taskId, self::LOG_FILE, @@ -435,19 +451,10 @@ false ); - echo $t->getMessage() . ' on line ' . $t->getLine(); + echo $t->getMessage() . ' on line ' . $t->getLine() . PHP_EOL; } - /*SystemFunctions::sendTelegram( - 'Выполнено задание с ID ' . $arTask['id'] . PHP_EOL . 'Время выполнения: ' . $endTime . ' cek.' . PHP_EOL . $arTask['class'] . '::' - . $arTask['method'] . '()' . PHP_EOL . print_r( - $arTask['params'], - true - ) - );*/ - - - return $executeStatus; + return $response; } /** diff --git a/core/lib/Core/Models/MQResponse.class.php b/core/lib/Core/Models/MQResponse.class.php new file mode 100644 index 0000000..71d4156 --- /dev/null +++ b/core/lib/Core/Models/MQResponse.class.php @@ -0,0 +1,150 @@ +taskId; + } + + /** + * Установка идентификатора задачи + * + * @param int $taskId Идентификатор задачи + * + * @return $this + */ + public function setTaskId(int $taskId = 0): self + { + $this->taskId = $taskId; + return $this; + } + + /** + * Получение статуса задания + * + * @return string|null + */ + public function getStatus(): ?string + { + return $this->status; + } + + /** + * Установка текстового кода ответа внешней службы + * + * @param string $status Текстовый код ответа + * + * @return $this + * @throws CoreException + */ + public function setStatus(string $status): self + { + $status = strtoupper($status); + $this->status = $status; + return $this; + } + + + /** + * Получение текста запроса + * + * @return string|null + */ + public function getResponse(): ?string + { + return $this->response; + } + + /** + * Установка текста запроса + * + * @param string|null $response Результат работы задания + * + * @return Response + */ + public function setResponse(?string $response = null): self + { + $this->response = $response; + return $this; + } + + + /** + * Установка массива парамтров задания + * + * @param array|null $params + * + * @return $this + */ + public function setParams(?array $params): self + { + $this->params = $params; + return $this; + } + + /** + * Установка массива парамтров задания + * + * @param array $params + * + * @return $this + */ + public function setParamsJson(?string $paramsJson): self + { + $this->paramsJson = $paramsJson; + return $this; + } + + }