From 66c58559c7fba5ceec7f4e5c01f2addf25d7d195 Mon Sep 17 00:00:00 2001 From: rsgrinko Date: Wed, 6 Jul 2022 17:18:25 +0300 Subject: [PATCH] =?UTF-8?q?[*]=20MQ=20=D0=BF=D1=80=D0=B0=D0=B2=D0=BA=D0=B8?= =?UTF-8?q?=20=D0=BD=D0=B5=D0=BC=D0=B5=D0=B4=D0=BB=D0=B5=D0=BD=D0=BD=D0=BE?= =?UTF-8?q?=D0=B5=20=D0=B2=D1=8B=D0=BF=D0=BE=D0=BB=D0=BD=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D0=B5=20=D0=B8=20=D0=BF=D1=80=D0=B8=D0=BE=D1=80=D0=B8=D1=82?= =?UTF-8?q?=D0=B5=D1=82=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- admin/threads.php | 2 - core/cron.php | 2 + core/lib/Core/Models/DB.class.php | 2 +- core/lib/Core/Models/MQ.class.php | 68 +++++++++++++++++++++++++++---- 4 files changed, 63 insertions(+), 11 deletions(-) diff --git a/admin/threads.php b/admin/threads.php index cc54016..d51841e 100644 --- a/admin/threads.php +++ b/admin/threads.php @@ -36,7 +36,6 @@ Всего Активных Выполняются - Выполнены С ошибкой @@ -46,7 +45,6 @@ getCountTasks();?> getCountTasks(['active' => 'Y', 'in_progress' => 'N']);?> getCountTasks(['active' => 'Y', 'in_progress' => 'Y']);?> - getCountTasks(['active' => 'N', 'in_progress' => 'N', 'executed' => 'Y']);?> getCountTasks(['active' => 'N', 'in_progress' => 'N', 'executed' => 'Y', 'status' => MQ::STATUS_ERROR]);?> diff --git a/core/cron.php b/core/cron.php index ceb8bea..ddee130 100644 --- a/core/cron.php +++ b/core/cron.php @@ -3,10 +3,12 @@ require_once __DIR__ . '/bootstrap.php'; + use Core\Helpers\SystemFunctions; use Core\Models\MQ; try { (new MQ())->run(); } catch (Throwable $t) { + SystemFunctions::sendTelegram('CRON: Произошла ошибка' . PHP_EOL . $t->getMessage() . PHP_EOL . 'File: ' . $t->getFile() . PHP_EOL . 'Line: ' . $t->getLine()); echo 'Error: ' . $t->getMessage() . ' / line: ' . $t->getLine(); } \ No newline at end of file diff --git a/core/lib/Core/Models/DB.class.php b/core/lib/Core/Models/DB.class.php index 9ee60df..3fd8954 100644 --- a/core/lib/Core/Models/DB.class.php +++ b/core/lib/Core/Models/DB.class.php @@ -269,7 +269,7 @@ * * @return array|null */ - public function getItems($table, $where, $sort = ''): ?array + public function getItems($table, $where, $sort = []): ?array { self::$quantity++; $result = $this->query('SELECT * FROM `' . $table . '` WHERE ' . $this->createWhere($where) . $this->createSort($sort)); diff --git a/core/lib/Core/Models/MQ.class.php b/core/lib/Core/Models/MQ.class.php index 83f82f8..12d62c4 100644 --- a/core/lib/Core/Models/MQ.class.php +++ b/core/lib/Core/Models/MQ.class.php @@ -58,6 +58,21 @@ */ private $DB = null; + /** + * Приоритет по умолчанию + */ + const DEFAULT_PRIORITY = 5; + + /** + * @var null $priority Приоритет задания + */ + private $priority = null; + + /** + * @var null $runNow Флаг немедленного запуска + */ + private $runNow = false; + /** * Конструктор */ @@ -66,6 +81,12 @@ $this->DB = DB::getInstance(); } + public function setRunNow(bool $runNow): self + { + $this->runNow = $runNow; + return $this; + } + /** * Тестовая функция 2 * @@ -135,10 +156,12 @@ { return $this->DB->getItems( self::TABLE, [ - 'active' => self::VALUE_Y, - 'in_progress' => self::VALUE_N, - 'executed' => self::VALUE_N, - ] + 'active' => self::VALUE_Y, + 'in_progress' => self::VALUE_N, + 'executed' => self::VALUE_N, + ], [ + 'priority' => 'ASC', + ] ); } @@ -158,7 +181,7 @@ $num = self::EXECUTION_TASKS_LIMIT - $countTasks; $arTasks = $this->DB->query( 'SELECT id FROM ' . self::TABLE . ' WHERE active="' . self::VALUE_N . '" AND executed="' . self::VALUE_N . '" AND in_progress="' - . self::VALUE_N . '" LIMIT ' . $num + . self::VALUE_N . '" ORDER BY priority ASC LIMIT ' . $num ); if (!empty($arTasks)) { $arTaskIds = []; @@ -215,6 +238,18 @@ } } + /** + * Установка приоритета задания + * + * @param int $priority Приоритет + * + * @return void + */ + public function setPriority(int $priority) + { + $this->priority = $priority; + } + /** * Добавление задания в очередь * @@ -244,17 +279,32 @@ null, false ); + if (!empty($this->priority)) { + $priority = $this->priority; + $this->priority = null; + } else { + $priority = self::DEFAULT_PRIORITY; + } - return $this->DB->addItem( + + $taskId = $this->DB->addItem( self::TABLE, [ 'active' => self::VALUE_N, 'in_progress' => self::VALUE_N, 'attempts' => '0', 'class' => !empty($class) ? addslashes($class) : '', 'method' => $method, + 'priority' => $priority, 'params' => $params, ] ); + + if ($this->runNow) { + $this->execute($taskId); + } + + + return $taskId; } /** @@ -266,7 +316,8 @@ */ public function removeTask(?int $taskId = null): bool { - if (empty($id)) { + if (empty($taskId)) { + SystemFunctions::sendTelegram('Не передан идентификатор задания (' . $taskId . ')' . PHP_EOL . print_r(func_get_args(), true)); throw new CoreException('Не передан идентификатор задания'); } return $this->DB->remove(self::TABLE, ['id' => $taskId]); @@ -316,7 +367,6 @@ ] ); - try { $startTime = microtime(true); @@ -401,6 +451,8 @@ } /** + * Сохранение выполненной задачи в историю + * * @param int $taskId Идентификатор задачи * * @return int|null Идентификатор задачи в истории