chore(api): drop unused LibraryScanListener (#2434)
This commit is contained in:
@@ -1,62 +0,0 @@
|
||||
package org.booklore.service.library;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.dao.InvalidDataAccessApiUsageException;
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.transaction.event.TransactionPhase;
|
||||
import org.springframework.transaction.event.TransactionalEventListener;
|
||||
|
||||
import org.booklore.task.options.RescanLibraryContext;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* Handles library scan requests after the triggering transaction commits.
|
||||
* Scans are executed asynchronously with deduplication to prevent concurrent scans.
|
||||
* Security context is automatically propagated via DelegatingSecurityContextExecutor.
|
||||
*/
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
public class LibraryScanListener {
|
||||
|
||||
private final LibraryProcessingService libraryProcessingService;
|
||||
private final Set<Long> scanningLibraries = ConcurrentHashMap.newKeySet();
|
||||
|
||||
public boolean isScanning(long libraryId) {
|
||||
return scanningLibraries.contains(libraryId);
|
||||
}
|
||||
|
||||
@Async("taskExecutor")
|
||||
@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT, fallbackExecution = true)
|
||||
public void handle(LibraryScanRequestedEvent event) {
|
||||
long libraryId = event.libraryId();
|
||||
|
||||
if (!scanningLibraries.add(libraryId)) {
|
||||
log.warn("Library {} is already being scanned, skipping duplicate request", libraryId);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
log.info("Starting background scan for library ID {} (full rescan: {})", libraryId, event.fullRescan());
|
||||
if (event.fullRescan()) {
|
||||
libraryProcessingService.rescanLibrary(RescanLibraryContext.builder().libraryId(libraryId).build());
|
||||
} else {
|
||||
libraryProcessingService.processLibrary(libraryId);
|
||||
}
|
||||
log.info("Completed background scan for library ID {}", libraryId);
|
||||
} catch (InvalidDataAccessApiUsageException e) {
|
||||
log.debug("InvalidDataAccessApiUsageException during library scan - Library id: {}", libraryId, e);
|
||||
} catch (IOException e) {
|
||||
log.error("IO error during library scan for library ID {}: {}", libraryId, e.getMessage(), e);
|
||||
} catch (Exception e) {
|
||||
log.error("Unexpected error during library scan for library ID {}: {}", libraryId, e.getMessage(), e);
|
||||
} finally {
|
||||
scanningLibraries.remove(libraryId);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
package org.booklore.service.library;
|
||||
|
||||
/**
|
||||
* Event published when a library scan is requested.
|
||||
* The actual scan will be performed after the current transaction commits.
|
||||
*
|
||||
* @param libraryId the ID of the library to scan
|
||||
*/
|
||||
public record LibraryScanRequestedEvent(long libraryId, boolean fullRescan) {
|
||||
}
|
||||
@@ -12,7 +12,6 @@ import org.booklore.repository.BookRepository;
|
||||
import org.booklore.repository.LibraryRepository;
|
||||
import org.booklore.service.file.FileFingerprint;
|
||||
import org.booklore.service.library.LibraryProcessingService;
|
||||
import org.booklore.service.library.LibraryScanListener;
|
||||
import org.booklore.util.FileUtils;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -46,7 +45,6 @@ public class LibraryFileEventProcessor implements SmartLifecycle {
|
||||
private final BookFileTransactionalHandler bookFileTransactionalHandler;
|
||||
private final BookFilePersistenceService bookFilePersistenceService;
|
||||
private final LibraryProcessingService libraryProcessingService;
|
||||
private final LibraryScanListener libraryScanListener;
|
||||
private final PendingDeletionPool pendingDeletionPool;
|
||||
|
||||
private ScheduledExecutorService scheduler;
|
||||
@@ -106,11 +104,6 @@ public class LibraryFileEventProcessor implements SmartLifecycle {
|
||||
}
|
||||
|
||||
public void processEvent(WatchEvent.Kind<?> eventKind, long libraryId, Path fullPath, boolean isDirectory) {
|
||||
if (libraryScanListener.isScanning(libraryId)) {
|
||||
log.debug("[SKIP] Library {} is currently being scanned, ignoring file event for '{}'", libraryId, fullPath);
|
||||
return;
|
||||
}
|
||||
|
||||
Path path = fullPath.toAbsolutePath().normalize();
|
||||
|
||||
if (eventKind == StandardWatchEventKinds.ENTRY_DELETE) {
|
||||
|
||||
+2
-5
@@ -9,7 +9,6 @@ import org.booklore.model.enums.LibraryOrganizationMode;
|
||||
import org.booklore.repository.BookRepository;
|
||||
import org.booklore.repository.LibraryRepository;
|
||||
import org.booklore.service.library.LibraryProcessingService;
|
||||
import org.booklore.service.library.LibraryScanListener;
|
||||
import org.junit.jupiter.api.*;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import org.mockito.Mock;
|
||||
@@ -19,7 +18,6 @@ import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.StandardWatchEventKinds;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
@@ -36,7 +34,6 @@ class LibraryFileEventProcessorTest {
|
||||
@Mock private BookFileTransactionalHandler bookFileTransactionalHandler;
|
||||
@Mock private BookFilePersistenceService bookFilePersistenceService;
|
||||
@Mock private LibraryProcessingService libraryProcessingService;
|
||||
@Mock private LibraryScanListener libraryScanListener;
|
||||
@Mock private PendingDeletionPool pendingDeletionPool;
|
||||
|
||||
private LibraryFileEventProcessor processor;
|
||||
@@ -54,7 +51,7 @@ class LibraryFileEventProcessorTest {
|
||||
mocks = MockitoAnnotations.openMocks(this);
|
||||
processor = new LibraryFileEventProcessor(
|
||||
libraryRepository, bookRepository, bookFileTransactionalHandler,
|
||||
bookFilePersistenceService, libraryProcessingService, libraryScanListener, pendingDeletionPool);
|
||||
bookFilePersistenceService, libraryProcessingService, pendingDeletionPool);
|
||||
|
||||
libraryPath = new LibraryPathEntity();
|
||||
libraryPath.setId(1L);
|
||||
@@ -434,7 +431,7 @@ class LibraryFileEventProcessorTest {
|
||||
// Reinitialize so tearDown's shutdown doesn't fail
|
||||
processor = new LibraryFileEventProcessor(
|
||||
libraryRepository, bookRepository, bookFileTransactionalHandler,
|
||||
bookFilePersistenceService, libraryProcessingService, libraryScanListener, pendingDeletionPool);
|
||||
bookFilePersistenceService, libraryProcessingService, pendingDeletionPool);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user