Fix race conditions between Bookdrop and Monitoring Service when importing multiple files (#1709)

* fix(bookdrop): gate library re-registration during single-file moves

- query MonitoringService to see if the library is actively watched before
  unregistering/re-registering it
- lower log noise for these operations and monitoring registration
- expose MonitoringService#isLibraryMonitored via MonitoringRegistrationService
- add FileMoveServiceTest covering monitored vs unmonitored scenarios

This fixes issues when importing multiple files via bookdrop.

Refs: #1608

* fix(bookdrop): avoid unnecessary stacktrace

* Use `deleteIfExists` to avoid trying to delete a missing file

This happens regularly as the file has already been removed by other methods
This commit is contained in:
Sergio Visinoni
2025-12-02 10:06:59 -07:00
committed by GitHub
parent a179749c93
commit d40a650df7
6 changed files with 130 additions and 3 deletions
@@ -0,0 +1,109 @@
package com.adityachandel.booklore.service.file;
import com.adityachandel.booklore.mapper.BookMapper;
import com.adityachandel.booklore.mapper.LibraryMapper;
import com.adityachandel.booklore.model.dto.FileMoveResult;
import com.adityachandel.booklore.model.entity.BookEntity;
import com.adityachandel.booklore.model.entity.LibraryEntity;
import com.adityachandel.booklore.model.entity.LibraryPathEntity;
import com.adityachandel.booklore.repository.BookRepository;
import com.adityachandel.booklore.repository.LibraryRepository;
import com.adityachandel.booklore.service.NotificationService;
import com.adityachandel.booklore.service.monitoring.MonitoringRegistrationService;
import jakarta.persistence.EntityManager;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import java.nio.file.Path;
import java.nio.file.Paths;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.anySet;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
class FileMoveServiceTest {
@Mock
private BookRepository bookRepository;
@Mock
private LibraryRepository libraryRepository;
@Mock
private FileMoveHelper fileMoveHelper;
@Mock
private MonitoringRegistrationService monitoringRegistrationService;
@Mock
private LibraryMapper libraryMapper;
@Mock
private BookMapper bookMapper;
@Mock
private NotificationService notificationService;
@Mock
private EntityManager entityManager;
@InjectMocks
private FileMoveService fileMoveService;
private BookEntity bookEntity;
private Path expectedFilePath;
@BeforeEach
void setUp() throws Exception {
LibraryEntity library = new LibraryEntity();
library.setId(42L);
LibraryPathEntity libraryPath = new LibraryPathEntity();
libraryPath.setId(77L);
libraryPath.setPath("/library/root");
libraryPath.setLibrary(library);
bookEntity = new BookEntity();
bookEntity.setId(999L);
bookEntity.setLibrary(library);
bookEntity.setLibraryPath(libraryPath);
bookEntity.setFileSubPath("SciFi");
bookEntity.setFileName("Original.epub");
expectedFilePath = Paths.get(libraryPath.getPath(), bookEntity.getFileSubPath(), "Renamed.epub");
when(fileMoveHelper.getFileNamingPattern(library)).thenReturn("{title}");
when(fileMoveHelper.generateNewFilePath(bookEntity, libraryPath, "{title}")).thenReturn(expectedFilePath);
when(fileMoveHelper.extractSubPath(expectedFilePath, libraryPath)).thenReturn(bookEntity.getFileSubPath());
doNothing().when(fileMoveHelper).moveFile(any(Path.class), any(Path.class));
doNothing().when(fileMoveHelper).deleteEmptyParentDirsUpToLibraryFolders(any(Path.class), anySet());
}
@Test
void moveSingleFile_whenLibraryMonitored_reRegistersLibraryPaths() throws Exception {
when(monitoringRegistrationService.isLibraryMonitored(42L)).thenReturn(true);
FileMoveResult result = fileMoveService.moveSingleFile(bookEntity);
assertTrue(result.isMoved());
verify(fileMoveHelper).unregisterLibrary(42L);
Path expectedRoot = Paths.get(bookEntity.getLibraryPath().getPath()).toAbsolutePath().normalize();
verify(fileMoveHelper).registerLibraryPaths(42L, expectedRoot);
}
@Test
void moveSingleFile_whenLibraryNotMonitored_skipsMonitoringCalls() throws Exception {
when(monitoringRegistrationService.isLibraryMonitored(42L)).thenReturn(false);
FileMoveResult result = fileMoveService.moveSingleFile(bookEntity);
assertTrue(result.isMoved());
verify(fileMoveHelper, never()).unregisterLibrary(anyLong());
verify(fileMoveHelper, never()).registerLibraryPaths(anyLong(), any(Path.class));
}
}