From 7217850315cde53aa598754053774a13c42455c8 Mon Sep 17 00:00:00 2001 From: rsgrinko Date: Sun, 10 Jul 2022 13:47:00 +0300 Subject: [PATCH] =?UTF-8?q?[*]=20MQ=20=D0=BF=D0=B5=D1=80=D0=B5=D0=B4=D0=B5?= =?UTF-8?q?=D0=BB=D0=BA=D0=B0=20=D0=BD=D0=B0=20=D0=B2=D0=BE=D1=80=D0=BA?= =?UTF-8?q?=D0=B5=D1=80=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- admin/threads.php | 2 +- core/cron.php | 11 ++--------- core/lib/Core/MQTasks.class.php | 5 +++-- core/lib/Core/Models/MQ.class.php | 18 +++++++----------- core/runtime/init.php | 13 +++++++++++++ core/runtime/threadsWorker.php | 20 ++++++++++++++++++++ 6 files changed, 46 insertions(+), 23 deletions(-) create mode 100644 core/runtime/init.php create mode 100644 core/runtime/threadsWorker.php diff --git a/admin/threads.php b/admin/threads.php index 65bc6c0..b4e1b18 100644 --- a/admin/threads.php +++ b/admin/threads.php @@ -43,7 +43,7 @@ getCountTasks();?> - getCountTasks(['active' => 'Y', 'in_progress' => 'N']);?> + getCountTasks(['active' => 'Y']);?> getCountTasks(['active' => 'Y', 'in_progress' => 'Y']);?> getCountTasks(['in_progress' => 'N', 'status' => MQ::STATUS_ERROR]);?> diff --git a/core/cron.php b/core/cron.php index 79bc61c..382e0d1 100644 --- a/core/cron.php +++ b/core/cron.php @@ -7,12 +7,5 @@ } 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 + // Запускаем воркер диспетчера очереди + exec('(php -f ' . __DIR__ . '/runtime/threadsWorker.php & ) >> /dev/null 2>&1'); \ No newline at end of file diff --git a/core/lib/Core/MQTasks.class.php b/core/lib/Core/MQTasks.class.php index 9ec320a..90fbc82 100644 --- a/core/lib/Core/MQTasks.class.php +++ b/core/lib/Core/MQTasks.class.php @@ -142,7 +142,7 @@ $DB->update('bashorg', ['hash' => $hash], ['ext_id' => (int)$element['id'], 'date' => $element['date'], 'rating' => $element['rating']]); } } - sleep(5); + //sleep(5); return 'bashMeta: Добавлено ' . $i . ', обновлено ' . $j . ' записей из ' . count($result); } @@ -164,6 +164,7 @@ foreach($arJokes as $joke) { $hash = md5($joke); $joke = str_replace('
', PHP_EOL, $joke); + $joke = addslashes($joke); $res = $DB->query('SELECT * FROM jokes WHERE hash="' . $hash . '"'); if (!$res) { $i++; @@ -174,7 +175,7 @@ - sleep(2); + //sleep(2); return 'Добавлено ' . $i . ' шуток из ' . count($arJokes); } } \ No newline at end of file diff --git a/core/lib/Core/Models/MQ.class.php b/core/lib/Core/Models/MQ.class.php index 9586084..893ba17 100644 --- a/core/lib/Core/Models/MQ.class.php +++ b/core/lib/Core/Models/MQ.class.php @@ -61,7 +61,7 @@ /** * Лимит количества одновременно выполняемых задач */ - const WORKERS_LIMIT = 2; + const WORKERS_LIMIT = 5; /** * @var DB|null $DB Объект базы @@ -464,6 +464,7 @@ * @param string $params Json параметры * * @return bool + * @throws CoreException */ private function checkDuplicates(?string $class, string $method, string $params): bool { @@ -561,7 +562,6 @@ if (empty($arTask['class']) && !empty($arTask['method']) && !function_exists($arTask['method'])) { throw new CoreException('Функция не существует, невозможно выполнить', CoreException::ERROR_CLASS_OR_METHOD_NOT_FOUND); } - if (empty($arTask['class'])) { $result = call_user_func_array($arTask['method'], $arTask['params']); } else { @@ -708,28 +708,24 @@ } /** - * Получение текущего количества выполняемых задач + * Получение количества запущенных воркеров * * @return int - * @throws CoreException */ - private function getCountWorkers(): int + public function getCountWorkers(): int { - return (int)$this->DB->query( - 'SELECT count(id) as count FROM ' . self::TABLE . ' WHERE active="' . self::VALUE_Y . '" AND in_progress="' . self::VALUE_Y . '"' - )[0]['count']; + return (int)exec('ps -awx | grep \'threadsWorker.php\' | grep -v \'grep\' | wc -l'); } /** - * Проверка на максимальное количество выполняемых задач + * Проверка на максимальное количество работающих воркеров * * @return bool - * @throws CoreException */ private function hasMaxWorkers(): bool { - return $this->getCountWorkers() >= self::WORKERS_LIMIT; + return $this->getCountWorkers() > self::WORKERS_LIMIT; } /** diff --git a/core/runtime/init.php b/core/runtime/init.php new file mode 100644 index 0000000..bebbaba --- /dev/null +++ b/core/runtime/init.php @@ -0,0 +1,13 @@ +run(); + } catch (Throwable $t) { + SystemFunctions::sendTelegram( + 'ThreadsWorker: Произошла ошибка' . PHP_EOL . $t->getMessage() . PHP_EOL . 'File: ' . $t->getFile() . PHP_EOL . 'Line: ' . $t->getLine() + ); + } \ No newline at end of file