diff --git a/admin/threads.php b/admin/threads.php
index cc54016..d51841e 100644
--- a/admin/threads.php
+++ b/admin/threads.php
@@ -36,7 +36,6 @@
Всего |
Активных |
Выполняются |
- Выполнены |
С ошибкой |
@@ -46,7 +45,6 @@
=$MQ->getCountTasks();?> |
=$MQ->getCountTasks(['active' => 'Y', 'in_progress' => 'N']);?> |
=$MQ->getCountTasks(['active' => 'Y', 'in_progress' => 'Y']);?> |
- =$MQ->getCountTasks(['active' => 'N', 'in_progress' => 'N', 'executed' => 'Y']);?> |
=$MQ->getCountTasks(['active' => 'N', 'in_progress' => 'N', 'executed' => 'Y', 'status' => MQ::STATUS_ERROR]);?> |
diff --git a/core/cron.php b/core/cron.php
index ceb8bea..ddee130 100644
--- a/core/cron.php
+++ b/core/cron.php
@@ -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();
}
\ No newline at end of file
diff --git a/core/lib/Core/Models/DB.class.php b/core/lib/Core/Models/DB.class.php
index 9ee60df..3fd8954 100644
--- a/core/lib/Core/Models/DB.class.php
+++ b/core/lib/Core/Models/DB.class.php
@@ -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));
diff --git a/core/lib/Core/Models/MQ.class.php b/core/lib/Core/Models/MQ.class.php
index 83f82f8..12d62c4 100644
--- a/core/lib/Core/Models/MQ.class.php
+++ b/core/lib/Core/Models/MQ.class.php
@@ -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 Идентификатор задачи в истории