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:
+1
-1
@@ -507,7 +507,7 @@ public class BookDropService {
|
||||
private void cleanupTempFile(Path tempPath) {
|
||||
if (tempPath != null) {
|
||||
try {
|
||||
Files.delete(tempPath);
|
||||
Files.deleteIfExists(tempPath);
|
||||
} catch (Exception e) {
|
||||
log.warn("Failed to cleanup temp file: {}", tempPath, e);
|
||||
}
|
||||
|
||||
@@ -74,6 +74,7 @@ public class FileMoveHelper {
|
||||
}
|
||||
|
||||
public void registerLibraryPaths(Long libraryId, Path libraryRoot) {
|
||||
log.debug("Registering library paths for library {} with root {}", libraryId, libraryRoot);
|
||||
monitoringRegistrationService.registerLibraryPaths(libraryId, libraryRoot);
|
||||
}
|
||||
|
||||
|
||||
+10
-2
@@ -122,8 +122,10 @@ public class FileMoveService {
|
||||
|
||||
Long libraryId = bookEntity.getLibraryPath().getLibrary().getId();
|
||||
Path libraryRoot = Paths.get(bookEntity.getLibraryPath().getPath()).toAbsolutePath().normalize();
|
||||
boolean isLibraryMonitoredWhenCalled = false;
|
||||
|
||||
try {
|
||||
isLibraryMonitoredWhenCalled = monitoringRegistrationService.isLibraryMonitored(libraryId);
|
||||
String pattern = fileMoveHelper.getFileNamingPattern(bookEntity.getLibraryPath().getLibrary());
|
||||
Path currentFilePath = bookEntity.getFullFilePath();
|
||||
Path expectedFilePath = fileMoveHelper.generateNewFilePath(bookEntity, bookEntity.getLibraryPath(), pattern);
|
||||
@@ -134,7 +136,10 @@ public class FileMoveService {
|
||||
|
||||
log.info("File for book ID {} needs to be moved from {} to {} to match library pattern", bookEntity.getId(), currentFilePath, expectedFilePath);
|
||||
|
||||
fileMoveHelper.unregisterLibrary(libraryId);
|
||||
if (isLibraryMonitoredWhenCalled) {
|
||||
log.debug("Unregistering library {} before moving a single file", libraryId);
|
||||
fileMoveHelper.unregisterLibrary(libraryId);
|
||||
}
|
||||
|
||||
fileMoveHelper.moveFile(currentFilePath, expectedFilePath);
|
||||
|
||||
@@ -151,7 +156,10 @@ public class FileMoveService {
|
||||
} catch (Exception e) {
|
||||
log.error("Failed to move file for book ID {}: {}", bookEntity.getId(), e.getMessage(), e);
|
||||
} finally {
|
||||
fileMoveHelper.registerLibraryPaths(libraryId, libraryRoot);
|
||||
if (isLibraryMonitoredWhenCalled) {
|
||||
log.debug("Registering library paths for library {} with root {}", libraryId, libraryRoot);
|
||||
fileMoveHelper.registerLibraryPaths(libraryId, libraryRoot);
|
||||
}
|
||||
}
|
||||
|
||||
return FileMoveResult.builder().moved(false).build();
|
||||
|
||||
+5
@@ -21,6 +21,10 @@ public class MonitoringRegistrationService {
|
||||
return monitoringService.isPathMonitored(path);
|
||||
}
|
||||
|
||||
public boolean isLibraryMonitored(Long libraryId) {
|
||||
return monitoringService.isLibraryMonitored(libraryId);
|
||||
}
|
||||
|
||||
public void unregisterSpecificPath(Path path) {
|
||||
monitoringService.unregisterPath(path);
|
||||
}
|
||||
@@ -42,6 +46,7 @@ public class MonitoringRegistrationService {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
log.debug("Registering library paths for libraryId {} at {}", libraryId, libraryRoot);
|
||||
monitoringService.registerPath(libraryRoot, libraryId);
|
||||
try (var stream = Files.walk(libraryRoot)) {
|
||||
stream.filter(Files::isDirectory)
|
||||
|
||||
+4
@@ -241,4 +241,8 @@ public class MonitoringService {
|
||||
public boolean isPathMonitored(Path path) {
|
||||
return monitoredPaths.contains(path.toAbsolutePath().normalize());
|
||||
}
|
||||
|
||||
public boolean isLibraryMonitored(Long libraryId) {
|
||||
return libraryWatchStatusMap.getOrDefault(libraryId, false);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user