This commit is contained in:
@@ -0,0 +1,7 @@
|
|||||||
|
package ru.soune.nocopy.exception;
|
||||||
|
|
||||||
|
public class InsufficientTokensException extends RuntimeException {
|
||||||
|
public InsufficientTokensException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package ru.soune.nocopy.exception;
|
||||||
|
|
||||||
|
public class SearchFailedException extends RuntimeException {
|
||||||
|
public SearchFailedException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -14,8 +14,9 @@ import ru.soune.nocopy.entity.search.GlobalSearchTask;
|
|||||||
import ru.soune.nocopy.entity.search.SearchStatus;
|
import ru.soune.nocopy.entity.search.SearchStatus;
|
||||||
import ru.soune.nocopy.entity.tarif.TariffInfo;
|
import ru.soune.nocopy.entity.tarif.TariffInfo;
|
||||||
import ru.soune.nocopy.entity.user.User;
|
import ru.soune.nocopy.entity.user.User;
|
||||||
|
import ru.soune.nocopy.exception.InsufficientTokensException;
|
||||||
|
import ru.soune.nocopy.exception.SearchFailedException;
|
||||||
import ru.soune.nocopy.exception.TariffNotFoundException;
|
import ru.soune.nocopy.exception.TariffNotFoundException;
|
||||||
import ru.soune.nocopy.exception.TokenNotFoundException;
|
|
||||||
import ru.soune.nocopy.repository.GlobalSearchResultRepository;
|
import ru.soune.nocopy.repository.GlobalSearchResultRepository;
|
||||||
import ru.soune.nocopy.repository.GlobalSearchTaskRepository;
|
import ru.soune.nocopy.repository.GlobalSearchTaskRepository;
|
||||||
import ru.soune.nocopy.repository.UserRepository;
|
import ru.soune.nocopy.repository.UserRepository;
|
||||||
@@ -52,6 +53,42 @@ public class GlobalSearchAsyncProcessor {
|
|||||||
|
|
||||||
private final EmailService emailService;
|
private final EmailService emailService;
|
||||||
|
|
||||||
|
// @Async
|
||||||
|
// @Transactional
|
||||||
|
// public void processFilesAsync(String taskId, List<FileEntity> filesToProcess, Long userId) {
|
||||||
|
// try {
|
||||||
|
// for (int i = 0; i < filesToProcess.size(); i++) {
|
||||||
|
// FileEntity file = filesToProcess.get(i);
|
||||||
|
//
|
||||||
|
// GlobalSearchResult result = processFile(file, taskId, userId);
|
||||||
|
// globalSearchResultRepository.save(result);
|
||||||
|
//
|
||||||
|
// if (result.getFileStatus().equals(SearchStatus.FAILED.name())) {
|
||||||
|
// break;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// GlobalSearchTask task = globalSearchTaskRepository.findById(taskId).orElseThrow();
|
||||||
|
// task.setProcessedFiles(i + 1);
|
||||||
|
// task.setUpdatedAt(LocalDateTime.now());
|
||||||
|
// globalSearchTaskRepository.save(task);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// GlobalSearchTask task = globalSearchTaskRepository.findById(taskId).orElseThrow();
|
||||||
|
// task.setStatus(SearchStatus.COMPLETED.name());
|
||||||
|
// task.setUpdatedAt(LocalDateTime.now());
|
||||||
|
// globalSearchTaskRepository.save(task);
|
||||||
|
//
|
||||||
|
// } catch (Exception e) {
|
||||||
|
// log.error("Global search failed for task: {}", taskId, e);
|
||||||
|
//
|
||||||
|
// GlobalSearchTask task = globalSearchTaskRepository.findById(taskId).orElseThrow();
|
||||||
|
// task.setStatus(SearchStatus.FAILED.name());
|
||||||
|
// task.setUpdatedAt(LocalDateTime.now());
|
||||||
|
// globalSearchTaskRepository.save(task);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
|
||||||
@Async
|
@Async
|
||||||
@Transactional
|
@Transactional
|
||||||
public void processFilesAsync(String taskId, List<FileEntity> filesToProcess, Long userId) {
|
public void processFilesAsync(String taskId, List<FileEntity> filesToProcess, Long userId) {
|
||||||
@@ -59,13 +96,27 @@ public class GlobalSearchAsyncProcessor {
|
|||||||
for (int i = 0; i < filesToProcess.size(); i++) {
|
for (int i = 0; i < filesToProcess.size(); i++) {
|
||||||
FileEntity file = filesToProcess.get(i);
|
FileEntity file = filesToProcess.get(i);
|
||||||
|
|
||||||
GlobalSearchResult result = processFile(file, taskId, userId);
|
try {
|
||||||
globalSearchResultRepository.save(result);
|
GlobalSearchResult result = processFile(file, taskId, userId);
|
||||||
|
globalSearchResultRepository.save(result);
|
||||||
|
|
||||||
GlobalSearchTask task = globalSearchTaskRepository.findById(taskId).orElseThrow();
|
if (result.getFileStatus().equals(SearchStatus.FAILED.name()) ||
|
||||||
task.setProcessedFiles(i + 1);
|
result.getFileStatus().equals(SearchStatus.TIMEOUT.name())) {
|
||||||
task.setUpdatedAt(LocalDateTime.now());
|
throw new SearchFailedException("File processing failed for file: " + file.getId());
|
||||||
globalSearchTaskRepository.save(task);
|
}
|
||||||
|
|
||||||
|
GlobalSearchTask task = globalSearchTaskRepository.findById(taskId).orElseThrow();
|
||||||
|
task.setProcessedFiles(i + 1);
|
||||||
|
task.setUpdatedAt(LocalDateTime.now());
|
||||||
|
globalSearchTaskRepository.save(task);
|
||||||
|
|
||||||
|
} catch (InsufficientTokensException e) {
|
||||||
|
log.warn("Insufficient tokens for user {}, stopping task: {}", userId, taskId);
|
||||||
|
throw e;
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("Error processing file {} in task {}", file.getId(), taskId, e);
|
||||||
|
throw new SearchFailedException("Failed to process file: " + file.getId());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
GlobalSearchTask task = globalSearchTaskRepository.findById(taskId).orElseThrow();
|
GlobalSearchTask task = globalSearchTaskRepository.findById(taskId).orElseThrow();
|
||||||
@@ -99,9 +150,10 @@ public class GlobalSearchAsyncProcessor {
|
|||||||
emailService.sendTokensNotFoundEmail(user, currentTokens, TariffConstants.TOKEN_VALUE_FOR_SEARCH);
|
emailService.sendTokensNotFoundEmail(user, currentTokens, TariffConstants.TOKEN_VALUE_FOR_SEARCH);
|
||||||
|
|
||||||
result.setFileStatus(SearchStatus.FAILED.name());
|
result.setFileStatus(SearchStatus.FAILED.name());
|
||||||
|
|
||||||
globalSearchResultRepository.save(result);
|
globalSearchResultRepository.save(result);
|
||||||
|
|
||||||
throw new TariffNotFoundException("Tariff not found");
|
throw new InsufficientTokensException("Insufficient tokens for user: " + userId);
|
||||||
}
|
}
|
||||||
|
|
||||||
result = globalSearchResultRepository.save(result);
|
result = globalSearchResultRepository.save(result);
|
||||||
|
|||||||
Reference in New Issue
Block a user