[*] 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>
</tr>
</thead>
@@ -46,7 +45,6 @@
<td><?=$MQ->getCountTasks();?></td>
<td><?=$MQ->getCountTasks(['active' => 'Y', 'in_progress' => 'N']);?></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>
</tr>
</tbody>
+2
View File
@@ -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();
}
+1 -1
View File
@@ -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));
+60 -8
View File
@@ -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 Идентификатор задачи в истории