diff --git a/core/lib/Core/Models/MQ.class.php b/core/lib/Core/Models/MQ.class.php index 292fabe..169be32 100644 --- a/core/lib/Core/Models/MQ.class.php +++ b/core/lib/Core/Models/MQ.class.php @@ -7,30 +7,77 @@ class MQ { + /** + * Таблица заданий + */ const TABLE = 'threads'; + /** + * Статус успешно выполненного задания + */ + const STATUS_OK = 'OK'; + + /** + * Статус неудачно выполненного задания + */ + const STATUS_ERROR = 'ERROR'; + + /** + * Значение ДА + */ + const VALUE_Y = 'Y'; + + /** + * Значение НЕТ + */ + const VALUE_N = 'N'; + /** * @var DB|null $DB Объект базы */ private $DB = null; + /** + * Конструктор + */ public function __construct() { $this->DB = DB::getInstance(); } + /** + * Тестовая функция + * + * @param $name + * @param $age + * + * @return int + * @throws \Core\CoreException + */ public static function test($name, $age): int { 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( self::TABLE, [ - 'active' => 'N', - 'in_progress' => 'N', + 'active' => self::VALUE_N, + 'in_progress' => self::VALUE_N, 'attempts' => '0', 'class' => str_replace('\\', '\\\\', $class), 'method' => $method, @@ -39,29 +86,37 @@ ); } - public function execute(int $taskId) + /** + * Выполнение конкретного задания + * + * @param int $taskId Идентификатор задания + * + * @return bool Флаг результата выполнения задания + */ + public function execute(int $taskId): bool { - $arTask = $this->DB->getItem(self::TABLE, ['id' => $taskId]); - $arTask['class'] = str_replace('\\\\', '\\', $arTask['class']); + $executeStatus = true; + $arTask = $this->DB->getItem(self::TABLE, ['id' => $taskId]); + $arTask['class'] = str_replace('\\\\', '\\', $arTask['class']); $arTask['params'] = json_decode($arTask['params'], true); $this->DB->update( self::TABLE, ['id' => $taskId], [ - 'active' => 'Y', - 'in_progress' => 'Y', + 'active' => self::VALUE_Y, + 'in_progress' => self::VALUE_Y, 'attempts' => ((int)$arTask['attempts'] + 1), ] ); try { - $result = call_user_func_array($arTask['class'] . '::' . $arTask['method'], $arTask['params']); //$this->DB->remove(self::TABLE, ['id' => $taskId]); $this->DB->update( self::TABLE, ['id' => $taskId], [ - 'active' => 'N', - 'in_progress' => 'N', - 'executed' => 'Y', - 'response' => json_encode($result, JSON_UNESCAPED_UNICODE), + 'active' => self::VALUE_N, + 'in_progress' => self::VALUE_N, + 'executed' => self::VALUE_Y, + 'status' => self::STATUS_OK, + 'response' => json_encode($result, JSON_UNESCAPED_UNICODE), ] ); } catch (\Throwable $t) { @@ -69,10 +124,14 @@ self::TABLE, ['id' => $taskId], [ 'active' => 'N', 'in_progress' => 'N', + 'status' => self::STATUS_ERROR, 'response' => $t->getMessage(), ] ); + $executeStatus = false; echo $t->getMessage(); } + + return $executeStatus; } } \ No newline at end of file