diff --git a/src/main/java/ru/soune/nocopy/service/search/GlobalSearchAsyncProcessor.java b/src/main/java/ru/soune/nocopy/service/search/GlobalSearchAsyncProcessor.java index e7080a2..03f5442 100644 --- a/src/main/java/ru/soune/nocopy/service/search/GlobalSearchAsyncProcessor.java +++ b/src/main/java/ru/soune/nocopy/service/search/GlobalSearchAsyncProcessor.java @@ -5,6 +5,7 @@ import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Component; +import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import ru.soune.nocopy.configuration.search.SearchProperties; import ru.soune.nocopy.dto.file.YandexSearchResponse; @@ -113,11 +114,14 @@ public class GlobalSearchAsyncProcessor { task.setUpdatedAt(LocalDateTime.now()); globalSearchTaskRepository.save(task); } catch (InsufficientTokensException e) { - log.warn("Insufficient tokens for user {}, stopping task: {}", userId, taskId, e); - throw e; + log.error("Task {} failed due to insufficient tokens for user {}", taskId, userId, e); + + updateTaskStatus(taskId, SearchStatus.FAILED.name()); + } catch (Exception e) { - log.error("Error processing file {} in task {}", file.getId(), taskId, e); - throw new SearchFailedException("Failed to process file: " + file.getId()); + log.error("Global search failed for task: {}", taskId, e); + + updateTaskStatus(taskId, SearchStatus.FAILED.name()); } } @@ -137,6 +141,18 @@ public class GlobalSearchAsyncProcessor { } } + @Transactional(propagation = Propagation.REQUIRES_NEW) + public void updateTaskStatus(String taskId, String status) { + try { + GlobalSearchTask task = globalSearchTaskRepository.findById(taskId).orElseThrow(); + task.setStatus(status); + task.setUpdatedAt(LocalDateTime.now()); + globalSearchTaskRepository.save(task); + } catch (Exception e) { + log.error("Failed to update task status for task: {}", taskId, e); + } + } + private GlobalSearchResult processFile(String fileId, String taskId, Long userId) throws MessagingException, IOException, InsufficientTokensException { FileEntity file = fileEntityRepository.findById(fileId).orElseThrow();