[*] MQ правки выполнения таски (если выполнено и отсутствует - не падаем)

This commit is contained in:
rsgrinko committed 2022-07-06 14:03:00 +03:00
1 parent 0f206c2acc
commit 5ff55bf7c7
1 file changed
+21 -7
+21 -7
View File
@@ -108,7 +108,7 @@
*/ */
private function convertFromJson(?string $json): array private function convertFromJson(?string $json): array
{ {
if(empty($json)) { if (empty($json)) {
return []; return [];
} }
return json_decode($json, true); return json_decode($json, true);
@@ -257,6 +257,20 @@
); );
} }
/**
* Удаления задания из очереди
*
* @param int|null $taskId Идентификатор задания
*
* @throws CoreException
*/
public function removeTask(?int $taskId = null): bool
{
if (empty($id)) {
throw new CoreException('Не передан идентификатор задания');
}
return $this->DB->remove(self::TABLE, ['id' => $taskId]);
}
/** /**
* Проверка задания на дубликат * Проверка задания на дубликат
@@ -282,13 +296,14 @@
* @param int $taskId Идентификатор задания * @param int $taskId Идентификатор задания
* *
* @return bool Флаг результата выполнения задания * @return bool Флаг результата выполнения задания
* @throws CoreException
*/ */
public function execute(int $taskId): bool public function execute(int $taskId): bool
{ {
$executeStatus = true; $executeStatus = true;
$arTask = $this->DB->getItem(self::TABLE, ['id' => $taskId]); $arTask = $this->DB->getItem(self::TABLE, ['id' => $taskId]);
if ($arTask['in_progress'] === self::VALUE_Y) { if (empty($arTask) || $arTask['in_progress'] === self::VALUE_Y) {
// Данное задание уже выполняется другим воркером // Данное задание уже выполнено или выполняется другим воркером
return false; return false;
} }
@@ -389,11 +404,11 @@
* @param int $taskId Идентификатор задачи * @param int $taskId Идентификатор задачи
* *
* @return int|null Идентификатор задачи в истории * @return int|null Идентификатор задачи в истории
* @throws CoreException
*/ */
private function saveExecutedTask(int $taskId): ?int private function saveExecutedTask(int $taskId): ?int
{ {
$arTask = $this->DB->getItem(self::TABLE, ['id' => $taskId]); $arTask = $this->DB->getItem(self::TABLE, ['id' => $taskId]);
$taskHistoryId = $this->DB->addItem( $taskHistoryId = $this->DB->addItem(
self::TABLE_HISTORY, [ self::TABLE_HISTORY, [
'task_id' => $arTask['id'], 'task_id' => $arTask['id'],
@@ -408,8 +423,7 @@
'response' => $arTask['response'], 'response' => $arTask['response'],
] ]
); );
$this->DB->remove(self::TABLE, ['id' => $taskId]); $this->removeTask($taskId);
return $taskHistoryId; return $taskHistoryId;
} }