From 4447b66a626363289462ad1a0b0a420e061039c9 Mon Sep 17 00:00:00 2001 From: rsgrinko Date: Sun, 3 Jul 2022 01:07:46 +0300 Subject: [PATCH] =?UTF-8?q?[+]=20mq=20=D1=80=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7?= =?UTF-8?q?=D0=BE=D0=B2=D0=B0=D0=BD=D0=BE=20=D0=BF=D0=B5=D1=80=D0=B5=D0=BC?= =?UTF-8?q?=D0=B5=D1=89=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=B8=D1=81=D0=BF=D0=BE?= =?UTF-8?q?=D0=BB=D0=BD=D0=B5=D0=BD=D0=BD=D1=8B=D1=85=20=D1=83=D1=81=D0=BF?= =?UTF-8?q?=D0=B5=D1=88=D0=BD=D1=8B=D1=85=20=D0=B7=D0=B0=D0=B4=D0=B0=D0=BD?= =?UTF-8?q?=D0=B8=D0=B9=20=D0=B2=20=D0=B8=D1=81=D1=82=D0=BE=D1=80=D0=B8?= =?UTF-8?q?=D1=8E=20=D0=B7=D0=B0=D0=B4=D0=B0=D0=BD=D0=B8=D0=B9=20=D0=B8=20?= =?UTF-8?q?=D0=B1=D0=BB=D0=BE=D0=BA=D0=B8=D1=80=D0=BE=D0=B2=D0=BA=D0=B0=20?= =?UTF-8?q?=D0=B2=D1=8B=D0=BF=D0=BE=D0=BB=D0=BD=D0=B5=D0=BD=D0=B8=D1=8F=20?= =?UTF-8?q?=D0=B7=D0=B0=D0=B2=D0=B5=D1=80=D1=88=D0=B5=D0=BD=D0=BD=D1=8B?= =?UTF-8?q?=D1=85=20=D0=BD=D0=B5=D1=83=D0=B4=D0=B0=D1=87=D0=B5=D0=B9=20?= =?UTF-8?q?=D0=B7=D0=B0=D0=B4=D0=B0=D1=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/lib/Core/Models/MQ.class.php | 82 ++++++++++++++++++++++++------- 1 file changed, 63 insertions(+), 19 deletions(-) diff --git a/core/lib/Core/Models/MQ.class.php b/core/lib/Core/Models/MQ.class.php index 09c3dfa..5590d65 100644 --- a/core/lib/Core/Models/MQ.class.php +++ b/core/lib/Core/Models/MQ.class.php @@ -13,6 +13,11 @@ */ const TABLE = 'threads'; + /** + * Таблица истории заданий + */ + const TABLE_HISTORY = 'threads_history'; + /** * Имя файла логов */ @@ -41,7 +46,7 @@ /** * Количество задач для обработки */ - const EXECUTION_TASKS_LIMIT = 10; + const EXECUTION_TASKS_LIMIT = 2; /** * @var DB|null $DB Объект базы @@ -78,7 +83,7 @@ */ public static function test2(): int { - sleep(5); + sleep(2); return Log::logToFile('Тестирование2', 'MQ_test.log', func_get_args()); } @@ -154,12 +159,6 @@ Log::logToFile('Запущено выполнение заданий из очереди', self::LOG_FILE, ['count' => count($arTasks)]); foreach ($arTasks as $task) { $this->execute($task['id']); - SystemFunctions::sendTelegram( - 'Выполнено задание с ID ' . $task['id'] . PHP_EOL . $task['class'] . '::' . $task['method'] . '()' . PHP_EOL . print_r( - $task['params'], - true - ) - ); } } } @@ -202,8 +201,13 @@ */ public function execute(int $taskId): bool { - $executeStatus = true; - $arTask = $this->DB->getItem(self::TABLE, ['id' => $taskId]); + $executeStatus = true; + $arTask = $this->DB->getItem(self::TABLE, ['id' => $taskId]); + if ($arTask['in_progress'] === self::VALUE_Y) { + // Данное задание уже выполняется другим воркером + SystemFunctions::sendTelegram('Задание ' . $taskId . ' уже выполняется другим воркером'); + return false; + } $arTask['class'] = str_replace('\\\\', '\\', $arTask['class']); $arTask['params'] = json_decode($arTask['params'], true); $this->DB->update( @@ -236,35 +240,75 @@ $result = call_user_func_array($arTask['class'] . '::' . $arTask['method'], $arTask['params']); } + $endTime = round(microtime(true) - $startTime, 4); - //$this->DB->remove(self::TABLE, ['id' => $taskId]); $this->DB->update( self::TABLE, ['id' => $taskId], [ 'active' => self::VALUE_N, 'in_progress' => self::VALUE_N, 'executed' => self::VALUE_Y, - 'execution_time' => round(microtime(true) - $startTime, 4), + 'execution_time' => $endTime, 'status' => self::STATUS_OK, 'date_updated' => date('Y-m-d H:i:s'), 'response' => json_encode($result, JSON_UNESCAPED_UNICODE), ] ); + + $this->saveExecutedTask($taskId); } catch (\Throwable|CoreException $t) { + $endTime = round(microtime(true) - $startTime, 4); $this->DB->update( self::TABLE, ['id' => $taskId], [ - 'active' => self::VALUE_N, - 'in_progress' => self::VALUE_N, - 'executed' => self::VALUE_Y, - 'execution_time' => round(microtime(true) - $startTime, 4), - 'date_updated' => date('Y-m-d H:i:s'), - 'status' => self::STATUS_ERROR, - 'response' => $t->getMessage(), + 'active' => self::VALUE_N, + 'in_progress' => self::VALUE_N, + 'executed' => self::VALUE_Y, + 'execution_time' => $endTime, + 'date_updated' => date('Y-m-d H:i:s'), + 'status' => self::STATUS_ERROR, + 'response' => $t->getMessage(), ] ); $executeStatus = false; echo $t->getMessage(); } + SystemFunctions::sendTelegram( + 'Выполнено задание с ID ' . $arTask['id'] . PHP_EOL . 'Время выполнения: ' . $endTime . ' cek.' . PHP_EOL . $arTask['class'] . '::' + . $arTask['method'] . '()' . PHP_EOL . print_r( + $arTask['params'], + true + ) + ); + + return $executeStatus; } + + /** + * @param int $taskId Идентификатор задачи + * + * @return int|null Идентификатор задачи в истории + */ + private function saveExecutedTask(int $taskId): ?int + { + $arTask = $this->DB->getItem(self::TABLE, ['id' => $taskId]); + + $taskHistoryId = $this->DB->addItem( + self::TABLE_HISTORY, [ + 'task_id' => $arTask['id'], + 'execution_time' => $arTask['execution_time'], + 'attempts' => $arTask['attempts'], + 'date_created' => $arTask['date_created'], + 'date_updated' => $arTask['date_updated'], + 'class' => $arTask['class'], + 'method' => $arTask['method'], + 'params' => $arTask['params'], + 'status' => $arTask['status'], + 'response' => $arTask['response'], + ] + ); + $this->DB->remove(self::TABLE, ['id' => $taskId]); + + return $taskHistoryId; + } } \ No newline at end of file