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