Skip to content

Commit 387e7c3

Browse files
committed
Add asynchronous bookkeeping of CREATED and OVERFLOW events
1 parent e676b5f commit 387e7c3

File tree

1 file changed

+35
-14
lines changed

1 file changed

+35
-14
lines changed

src/main/java/engineering/swat/watch/impl/jdk/JDKRecursiveDirectoryWatch.java

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,13 @@
2727
package engineering.swat.watch.impl.jdk;
2828

2929
import java.io.IOException;
30+
import java.io.UncheckedIOException;
3031
import java.nio.file.FileVisitResult;
3132
import java.nio.file.Files;
3233
import java.nio.file.Path;
3334
import java.nio.file.SimpleFileVisitor;
3435
import java.nio.file.attribute.BasicFileAttributes;
36+
import java.util.concurrent.CompletableFuture;
3537
import java.util.concurrent.ConcurrentHashMap;
3638
import java.util.concurrent.ConcurrentMap;
3739
import java.util.concurrent.Executor;
@@ -72,25 +74,44 @@ private void handleCreate(WatchEvent ev) {
7274
// but we don't want to delay the publication of this
7375
// create till after the processing is done, so we schedule it in the background
7476
var fullPath = ev.calculateFullPath();
75-
if (!activeWatches.containsKey(fullPath)) {
76-
try {
77-
if (Files.isDirectory(fullPath)) {
78-
addNewDirectory(fullPath);
79-
reportOverflow(fullPath);
80-
}
81-
} catch (IOException ex) {
82-
logger.error("Could not locate new sub directories for: {}", ev.calculateFullPath(), ex);
83-
}
77+
if (!activeWatches.containsKey(fullPath) && Files.isDirectory(fullPath)) {
78+
CompletableFuture
79+
.runAsync(() -> {
80+
try {
81+
addNewDirectory(fullPath);
82+
} catch (IOException e) {
83+
throw new UncheckedIOException(e);
84+
}
85+
}, exec)
86+
.thenRunAsync(() -> reportOverflow(fullPath), exec)
87+
.exceptionally(ex -> {
88+
logger.error("Could not locate new sub directories for: {}", fullPath, ex);
89+
return null;
90+
});
8491
}
8592
}
8693

8794
private void handleOverflow(WatchEvent ev) {
8895
var fullPath = ev.calculateFullPath();
89-
try (var children = Files.find(fullPath, 1, (p, attrs) -> p != fullPath && attrs.isDirectory())) {
90-
children.forEach(JDKRecursiveDirectoryWatch.this::reportOverflow);
91-
} catch (IOException e) {
92-
logger.error("Could not handle overflow for: {} ({})", fullPath, e);
93-
}
96+
CompletableFuture
97+
.supplyAsync(() -> {
98+
try {
99+
return Files.find(fullPath, 1, (p, attrs) -> p != fullPath && attrs.isDirectory());
100+
} catch (IOException e) {
101+
throw new UncheckedIOException(e);
102+
}
103+
}, exec)
104+
.whenCompleteAsync((children, ex) -> {
105+
try {
106+
if (ex == null) {
107+
children.forEach(JDKRecursiveDirectoryWatch.this::reportOverflow);
108+
} else {
109+
logger.error("Could not handle overflow for: {} ({})", fullPath, ex);
110+
}
111+
} finally {
112+
children.close();
113+
}
114+
}, exec);
94115
}
95116

96117
private void handleDeleteDirectory(WatchEvent ev) {

0 commit comments

Comments
 (0)