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

This commit is contained in:
rsgrinko committed 2022-07-06 17:18:25 +03:00
1 parent 5ff55bf7c7
commit 66c58559c7
4 files changed
+63 -11

No files matched your search

-2
View File
@@ -36,7 +36,6 @@
<th>Всего</th> <th>Всего</th>
<th>Активных</th> <th>Активных</th>
<th>Выполняются</th> <th>Выполняются</th>
<th>Выполнены</th>
<th>С ошибкой</th> <th>С ошибкой</th>
</tr> </tr>
</thead> </thead>
@@ -46,7 +45,6 @@
<td><?=$MQ->getCountTasks();?></td> <td><?=$MQ->getCountTasks();?></td>
<td><?=$MQ->getCountTasks(['active' => 'Y', 'in_progress' => 'N']);?></td> <td><?=$MQ->getCountTasks(['active' => 'Y', 'in_progress' => 'N']);?></td>
<td><?=$MQ->getCountTasks(['active' => 'Y', 'in_progress' => 'Y']);?></td> <td><?=$MQ->getCountTasks(['active' => 'Y', 'in_progress' => 'Y']);?></td>
<td><?=$MQ->getCountTasks(['active' => 'N', 'in_progress' => 'N', 'executed' => 'Y']);?></td>
<td><?=$MQ->getCountTasks(['active' => 'N', 'in_progress' => 'N', 'executed' => 'Y', 'status' => MQ::STATUS_ERROR]);?></td> <td><?=$MQ->getCountTasks(['active' => 'N', 'in_progress' => 'N', 'executed' => 'Y', 'status' => MQ::STATUS_ERROR]);?></td>
</tr> </tr>
</tbody> </tbody>
+2
View File
@@ -3,10 +3,12 @@
require_once __DIR__ . '/bootstrap.php'; require_once __DIR__ . '/bootstrap.php';
use Core\Helpers\SystemFunctions;
use Core\Models\MQ; use Core\Models\MQ;
try { try {
(new MQ())->run(); (new MQ())->run();
} catch (Throwable $t) { } 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(); echo 'Error: ' . $t->getMessage() . ' / line: ' . $t->getLine();
} }
+1 -1
View File
@@ -269,7 +269,7 @@
* *
* @return array|null * @return array|null
*/ */
public function getItems($table, $where, $sort = ''): ?array public function getItems($table, $where, $sort = []): ?array
{ {
self::$quantity++; self::$quantity++;
$result = $this->query('SELECT * FROM `' . $table . '` WHERE ' . $this->createWhere($where) . $this->createSort($sort)); $result = $this->query('SELECT * FROM `' . $table . '` WHERE ' . $this->createWhere($where) . $this->createSort($sort));
+60 -8
View File
@@ -58,6 +58,21 @@
*/ */
private $DB = null; 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(); $this->DB = DB::getInstance();
} }
public function setRunNow(bool $runNow): self
{
$this->runNow = $runNow;
return $this;
}
/** /**
* Тестовая функция 2 * Тестовая функция 2
* *
@@ -135,10 +156,12 @@
{ {
return $this->DB->getItems( return $this->DB->getItems(
self::TABLE, [ self::TABLE, [
'active' => self::VALUE_Y, 'active' => self::VALUE_Y,
'in_progress' => self::VALUE_N, 'in_progress' => self::VALUE_N,
'executed' => self::VALUE_N, 'executed' => self::VALUE_N,
] ], [
'priority' => 'ASC',
]
); );
} }
@@ -158,7 +181,7 @@
$num = self::EXECUTION_TASKS_LIMIT - $countTasks; $num = self::EXECUTION_TASKS_LIMIT - $countTasks;
$arTasks = $this->DB->query( $arTasks = $this->DB->query(
'SELECT id FROM ' . self::TABLE . ' WHERE active="' . self::VALUE_N . '" AND executed="' . self::VALUE_N . '" AND in_progress="' '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)) { if (!empty($arTasks)) {
$arTaskIds = []; $arTaskIds = [];
@@ -215,6 +238,18 @@
} }
} }
/**
* Установка приоритета задания
*
* @param int $priority Приоритет
*
* @return void
*/
public function setPriority(int $priority)
{
$this->priority = $priority;
}
/** /**
* Добавление задания в очередь * Добавление задания в очередь
* *
@@ -244,17 +279,32 @@
null, null,
false 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, [ self::TABLE, [
'active' => self::VALUE_N, 'active' => self::VALUE_N,
'in_progress' => self::VALUE_N, 'in_progress' => self::VALUE_N,
'attempts' => '0', 'attempts' => '0',
'class' => !empty($class) ? addslashes($class) : '', 'class' => !empty($class) ? addslashes($class) : '',
'method' => $method, 'method' => $method,
'priority' => $priority,
'params' => $params, 'params' => $params,
] ]
); );
if ($this->runNow) {
$this->execute($taskId);
}
return $taskId;
} }
/** /**
@@ -266,7 +316,8 @@
*/ */
public function removeTask(?int $taskId = null): bool 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('Не передан идентификатор задания'); throw new CoreException('Не передан идентификатор задания');
} }
return $this->DB->remove(self::TABLE, ['id' => $taskId]); return $this->DB->remove(self::TABLE, ['id' => $taskId]);
@@ -316,7 +367,6 @@
] ]
); );
try { try {
$startTime = microtime(true); $startTime = microtime(true);
@@ -401,6 +451,8 @@
} }
/** /**
* Сохранение выполненной задачи в историю
*
* @param int $taskId Идентификатор задачи * @param int $taskId Идентификатор задачи
* *
* @return int|null Идентификатор задачи в истории * @return int|null Идентификатор задачи в истории