[*] MQ переделка на воркеры

This commit is contained in:
rsgrinko committed 2022-07-10 13:47:00 +03:00
1 parent 2a59c4c1d8
commit 7217850315
6 files changed
+46 -23

No files matched your search

+1 -1
View File
@@ -43,7 +43,7 @@
<tbody>
<tr>
<td><?=$MQ->getCountTasks();?></td>
<td><?=$MQ->getCountTasks(['active' => 'Y', 'in_progress' => 'N']);?></td>
<td><?=$MQ->getCountTasks(['active' => 'Y']);?></td>
<td><?=$MQ->getCountTasks(['active' => 'Y', 'in_progress' => 'Y']);?></td>
<td><?=$MQ->getCountTasks(['in_progress' => 'N', 'status' => MQ::STATUS_ERROR]);?></td>
</tr>
+2 -9
View File
@@ -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();
}
// Запускаем воркер диспетчера очереди
exec('(php -f ' . __DIR__ . '/runtime/threadsWorker.php & ) >> /dev/null 2>&1');
+3 -2
View File
@@ -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('<br>', 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);
}
}
+7 -11
View File
@@ -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;
}
/**
+13
View File
@@ -0,0 +1,13 @@
<?php
/**
* Инициализация runtime
*/
@ignore_user_abort(true);
set_time_limit(0);
if (empty($_SERVER['DOCUMENT_ROOT'])) {
$_SERVER['DOCUMENT_ROOT'] = __DIR__ . '/../..';
}
require_once __DIR__ . '/../bootstrap.php';
+20
View File
@@ -0,0 +1,20 @@
<?php
/**
* Воркер диспетчера очереди
*/
@ignore_user_abort(true);
set_time_limit(0);
require_once __DIR__ . '/init.php';
use Core\Helpers\SystemFunctions;
use Core\Models\MQ;
try {
(new MQ())->run();
} catch (Throwable $t) {
SystemFunctions::sendTelegram(
'ThreadsWorker: Произошла ошибка' . PHP_EOL . $t->getMessage() . PHP_EOL . 'File: ' . $t->getFile() . PHP_EOL . 'Line: ' . $t->getLine()
);
}