1
0
mirror of https://github.com/Stirling-Tools/Stirling-PDF.git synced 2024-11-11 02:10:11 +01:00

#1214 Fix unable to create FileMonitor if the root directory does not exist

This commit is contained in:
Danny Lau 2024-05-29 23:01:53 +08:00
parent 17ef2e9b5d
commit 65b9544942

View File

@ -24,13 +24,12 @@ public class FileMonitor {
private final ConcurrentHashMap.KeySetView<Path, Boolean> readyForProcessingFiles; private final ConcurrentHashMap.KeySetView<Path, Boolean> readyForProcessingFiles;
private final WatchService watchService; private final WatchService watchService;
private final Predicate<Path> pathFilter; private final Predicate<Path> pathFilter;
private final Path rootDir;
private Set<Path> stagingFiles; private Set<Path> stagingFiles;
/** /**
* @param rootDirectory the root directory to monitor * @param rootDirectory the root directory to monitor
* @param pathFilter the filter to apply to the paths, return true if the path should be * @param pathFilter the filter to apply to the paths, return true if the path should be monitored, false otherwise
* monitored, false otherwise
* @throws IOException
*/ */
@Autowired @Autowired
public FileMonitor( public FileMonitor(
@ -43,11 +42,7 @@ public class FileMonitor {
this.pathFilter = pathFilter; this.pathFilter = pathFilter;
this.readyForProcessingFiles = ConcurrentHashMap.newKeySet(); this.readyForProcessingFiles = ConcurrentHashMap.newKeySet();
this.watchService = FileSystems.getDefault().newWatchService(); this.watchService = FileSystems.getDefault().newWatchService();
this.rootDir = Path.of(rootDirectory);
Path path = Path.of(rootDirectory);
recursivelyRegisterEntry(path);
logger.info("Created a new file tracker for directory: {}", rootDirectory);
} }
private boolean shouldNotProcess(Path path) { private boolean shouldNotProcess(Path path) {
@ -82,6 +77,20 @@ public class FileMonitor {
*/ */
stagingFiles = new HashSet<>(newlyDiscoveredFiles); stagingFiles = new HashSet<>(newlyDiscoveredFiles);
readyForProcessingFiles.clear(); readyForProcessingFiles.clear();
if (path2KeyMapping.isEmpty()) {
logger.warn(
"not monitoring any directory, even the root directory itself: {}", rootDir);
if (Files.exists(
rootDir)) { // if the root directory exists, re-register the root directory
try {
recursivelyRegisterEntry(rootDir);
} catch (IOException e) {
logger.error("unable to register monitoring", e);
}
}
}
WatchKey key; WatchKey key;
while ((key = watchService.poll()) != null) { while ((key = watchService.poll()) != null) {
final Path watchingDir = (Path) key.watchable(); final Path watchingDir = (Path) key.watchable();
@ -119,10 +128,6 @@ public class FileMonitor {
boolean isKeyValid = key.reset(); boolean isKeyValid = key.reset();
if (!isKeyValid) { // key is invalid when the directory itself is no longer exists if (!isKeyValid) { // key is invalid when the directory itself is no longer exists
path2KeyMapping.remove((Path) key.watchable()); path2KeyMapping.remove((Path) key.watchable());
if (path2KeyMapping.isEmpty()) {
logger.warn(
"FileMonitor is not monitoring any directory, no even the root directory.");
}
} }
} }
readyForProcessingFiles.addAll(stagingFiles); readyForProcessingFiles.addAll(stagingFiles);