fix(filemove): improve handling of library monitoring during file moves and add tests for operation order (#2104)

Signed-off-by: Balázs Szücs <bszucs1209@gmail.com>
This commit is contained in:
Balázs Szücs
2026-01-02 10:49:34 -07:00
committed by GitHub
parent 486781b9cd
commit 998b07d243
3 changed files with 259 additions and 9 deletions
@@ -55,6 +55,8 @@ public class FileMoveService {
for (FileMoveRequest.Move move : moves) {
processSingleMove(move);
}
// Ensure any file system events from the moves are drained/ignored while we are still unregistered
sleep(EVENT_DRAIN_TIMEOUT_MS);
} finally {
for (Long libraryId : allAffectedLibraryIds) {
libraryRepository.findById(libraryId)
@@ -148,7 +150,9 @@ public class FileMoveService {
boolean isLibraryMonitoredWhenCalled = false;
try {
isLibraryMonitoredWhenCalled = monitoringRegistrationService.isLibraryMonitored(libraryId);
Set<Path> existingPaths = monitoringRegistrationService.getPathsForLibraries(Set.of(libraryId));
isLibraryMonitoredWhenCalled = monitoringRegistrationService.isLibraryMonitored(libraryId) || !existingPaths.isEmpty();
String pattern = fileMoveHelper.getFileNamingPattern(bookEntity.getLibraryPath().getLibrary());
Path currentFilePath = bookEntity.getFullFilePath();
Path expectedFilePath = fileMoveHelper.generateNewFilePath(bookEntity, bookEntity.getLibraryPath(), pattern);
@@ -161,15 +165,19 @@ public class FileMoveService {
if (isLibraryMonitoredWhenCalled) {
log.debug("Unregistering library {} before moving a single file", libraryId);
Set<Path> libraryPaths = monitoringRegistrationService.getPathsForLibraries(Set.of(libraryId));
fileMoveHelper.unregisterLibrary(libraryId);
monitoringRegistrationService.waitForEventsDrainedByPaths(libraryPaths, EVENT_DRAIN_TIMEOUT_MS);
monitoringRegistrationService.waitForEventsDrainedByPaths(existingPaths, EVENT_DRAIN_TIMEOUT_MS);
}
fileMoveHelper.moveFile(currentFilePath, expectedFilePath);
fileMoveHelper.deleteEmptyParentDirsUpToLibraryFolders(currentFilePath.getParent(), Set.of(libraryRoot));
if (isLibraryMonitoredWhenCalled) {
// Ensure any file system events from the move and cleanup are drained/ignored while we are still unregistered
sleep(EVENT_DRAIN_TIMEOUT_MS);
}
String newFileName = expectedFilePath.getFileName().toString();
String newFileSubPath = fileMoveHelper.extractSubPath(expectedFilePath, bookEntity.getLibraryPath());
@@ -192,4 +200,13 @@ public class FileMoveService {
return FileMoveResult.builder().moved(false).build();
}
protected void sleep(long millis) {
try {
Thread.sleep(millis);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException("Sleep interrupted", e);
}
}
}