[+] mq правки диспетчера очереди

This commit is contained in:
rsgrinko committed 2022-07-01 15:47:30 +03:00
1 parent c2a3a925a2
commit f6c0ffbd24
1 file changed
+69 -10
+69 -10
View File
@@ -7,30 +7,77 @@
class MQ class MQ
{ {
/**
* Таблица заданий
*/
const TABLE = 'threads'; const TABLE = 'threads';
/**
* Статус успешно выполненного задания
*/
const STATUS_OK = 'OK';
/**
* Статус неудачно выполненного задания
*/
const STATUS_ERROR = 'ERROR';
/**
* Значение ДА
*/
const VALUE_Y = 'Y';
/**
* Значение НЕТ
*/
const VALUE_N = 'N';
/** /**
* @var DB|null $DB Объект базы * @var DB|null $DB Объект базы
*/ */
private $DB = null; private $DB = null;
/**
* Конструктор
*/
public function __construct() public function __construct()
{ {
$this->DB = DB::getInstance(); $this->DB = DB::getInstance();
} }
/**
* Тестовая функция
*
* @param $name
* @param $age
*
* @return int
* @throws \Core\CoreException
*/
public static function test($name, $age): int public static function test($name, $age): int
{ {
return Log::logToFile('Тестирование сработало видимо.', 'MQ_test.log', func_get_args()); return Log::logToFile('Тестирование сработало видимо.', 'MQ_test.log', func_get_args());
} }
public function add($class, $method, $params): ?int /**
* Добавление задания в очередь
*
* @param string $class Класс
* @param string $method Метод класса
* @param ?array $params Массив параметров
*
* @return int|null Идентификатор созданного задания
*/
public function add(string $class, string $method, ?array $params = null): ?int
{ {
if (empty($params)) {
$params = [];
}
return $this->DB->addItem( return $this->DB->addItem(
self::TABLE, [ self::TABLE, [
'active' => 'N', 'active' => self::VALUE_N,
'in_progress' => 'N', 'in_progress' => self::VALUE_N,
'attempts' => '0', 'attempts' => '0',
'class' => str_replace('\\', '\\\\', $class), 'class' => str_replace('\\', '\\\\', $class),
'method' => $method, 'method' => $method,
@@ -39,28 +86,36 @@
); );
} }
public function execute(int $taskId) /**
* Выполнение конкретного задания
*
* @param int $taskId Идентификатор задания
*
* @return bool Флаг результата выполнения задания
*/
public function execute(int $taskId): bool
{ {
$executeStatus = true;
$arTask = $this->DB->getItem(self::TABLE, ['id' => $taskId]); $arTask = $this->DB->getItem(self::TABLE, ['id' => $taskId]);
$arTask['class'] = str_replace('\\\\', '\\', $arTask['class']); $arTask['class'] = str_replace('\\\\', '\\', $arTask['class']);
$arTask['params'] = json_decode($arTask['params'], true); $arTask['params'] = json_decode($arTask['params'], true);
$this->DB->update( $this->DB->update(
self::TABLE, ['id' => $taskId], [ self::TABLE, ['id' => $taskId], [
'active' => 'Y', 'active' => self::VALUE_Y,
'in_progress' => 'Y', 'in_progress' => self::VALUE_Y,
'attempts' => ((int)$arTask['attempts'] + 1), 'attempts' => ((int)$arTask['attempts'] + 1),
] ]
); );
try { try {
$result = call_user_func_array($arTask['class'] . '::' . $arTask['method'], $arTask['params']); $result = call_user_func_array($arTask['class'] . '::' . $arTask['method'], $arTask['params']);
//$this->DB->remove(self::TABLE, ['id' => $taskId]); //$this->DB->remove(self::TABLE, ['id' => $taskId]);
$this->DB->update( $this->DB->update(
self::TABLE, ['id' => $taskId], [ self::TABLE, ['id' => $taskId], [
'active' => 'N', 'active' => self::VALUE_N,
'in_progress' => 'N', 'in_progress' => self::VALUE_N,
'executed' => 'Y', 'executed' => self::VALUE_Y,
'status' => self::STATUS_OK,
'response' => json_encode($result, JSON_UNESCAPED_UNICODE), 'response' => json_encode($result, JSON_UNESCAPED_UNICODE),
] ]
); );
@@ -69,10 +124,14 @@
self::TABLE, ['id' => $taskId], [ self::TABLE, ['id' => $taskId], [
'active' => 'N', 'active' => 'N',
'in_progress' => 'N', 'in_progress' => 'N',
'status' => self::STATUS_ERROR,
'response' => $t->getMessage(), 'response' => $t->getMessage(),
] ]
); );
$executeStatus = false;
echo $t->getMessage(); echo $t->getMessage();
} }
return $executeStatus;
} }
} }