From 2eaab6dd4d7ba33bb1647e61857c607c77c45a65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Prante?= Date: Sun, 24 Mar 2024 16:43:10 +0100 Subject: [PATCH] add event type to file follow events --- .../event/path/FileFollowEventManagerService.java | 9 ++++++--- .../xbib/event/path/FileFollowEventService.java | 14 ++++++++++---- .../event/path/FileFollowEventManagerTest.java | 2 ++ .../org/xbib/event/path/PathEventManagerTest.java | 2 +- .../org/xbib/event/path/TestFileFollowEvent.java | 10 ++++++++++ .../event/path/TestFileFollowEventConsumer.java | 5 ++--- 6 files changed, 31 insertions(+), 11 deletions(-) create mode 100644 event-common/src/test/java/org/xbib/event/path/TestFileFollowEvent.java diff --git a/event-common/src/main/java/org/xbib/event/path/FileFollowEventManagerService.java b/event-common/src/main/java/org/xbib/event/path/FileFollowEventManagerService.java index ddb1904..4045ef2 100644 --- a/event-common/src/main/java/org/xbib/event/path/FileFollowEventManagerService.java +++ b/event-common/src/main/java/org/xbib/event/path/FileFollowEventManagerService.java @@ -1,7 +1,6 @@ package org.xbib.event.path; import org.xbib.event.Event; -import org.xbib.event.bus.EventBus; import org.xbib.event.common.AbstractEventManagerService; import org.xbib.event.common.EventManager; import org.xbib.event.common.EventManagerService; @@ -41,6 +40,7 @@ public class FileFollowEventManagerService extends AbstractEventManagerService i String name = entry.getKey(); Settings definition = entry.getValue(); if (definition.getAsBoolean("enabled", true)) { + String type = definition.get("type", "filefollow"); String baseStr = definition.get("base"); Objects.requireNonNull(baseStr); String patternStr = definition.get("pattern"); @@ -48,10 +48,13 @@ public class FileFollowEventManagerService extends AbstractEventManagerService i try { Path base = Paths.get(baseStr); Pattern pattern = Pattern.compile(patternStr); - FileFollowEventService fileFollowEventService = new FileFollowEventService(this, name, base, pattern); + FileFollowEventService fileFollowEventService = new FileFollowEventService(this, + type, name, base, pattern); Future future = executorService.submit(fileFollowEventService); eventServiceMap.put(future, fileFollowEventService); - logger.log(Level.INFO, "file follow service " + name + " with base " + base + " and pattern " + pattern + " added"); + logger.log(Level.INFO, "file follow service " + name + + " with type " + type + + " with base " + base + " and pattern " + pattern + " added"); } catch (Exception e) { logger.log(Level.SEVERE, "unable to create file follow service " +name + ", reason " + e.getMessage(), e); } diff --git a/event-common/src/main/java/org/xbib/event/path/FileFollowEventService.java b/event-common/src/main/java/org/xbib/event/path/FileFollowEventService.java index 2a34dc3..ac1aaca 100644 --- a/event-common/src/main/java/org/xbib/event/path/FileFollowEventService.java +++ b/event-common/src/main/java/org/xbib/event/path/FileFollowEventService.java @@ -33,6 +33,8 @@ public class FileFollowEventService implements Callable, Closeable { private final FileFollowEventManagerService fileFollowEventManagerService; + private final String eventType; + private final String name; private final Path base; @@ -46,10 +48,12 @@ public class FileFollowEventService implements Callable, Closeable { private volatile boolean keepWatching; public FileFollowEventService(FileFollowEventManagerService fileFollowEventManagerService, + String eventType, String name, Path base, Pattern pattern) throws IOException { this.fileFollowEventManagerService = fileFollowEventManagerService; + this.eventType = eventType; this.name = name; this.base = base; this.pattern = pattern; @@ -59,7 +63,8 @@ public class FileFollowEventService implements Callable, Closeable { kinds[0] = StandardWatchEventKinds.ENTRY_MODIFY; base.register(watchService, kinds); this.fileSizes = new LinkedHashMap<>(); - fillFileSizes(base, pattern); + // initialize the sizes of existing files, so we can safely create first event + setFileSizes(base, pattern); this.keepWatching = true; } @@ -88,13 +93,14 @@ public class FileFollowEventService implements Callable, Closeable { long currentSize = p.toFile().length(); fileSizes.put(p, currentSize); String content = readRange(channel, lastSize, currentSize); - // split content by line, this allows pattern matching without preprocessing in worker + // split file content by line + // this prevents swalloed events and allows pattern matching without preprocessing in worker for (String line : content.split("\n")) { if (fileFollowEventManagerService.getSuspended().contains(name)) { logger.log(Level.WARNING, name + " is suspended"); } else { Event event = EventManager.eventBuilder() - .setType("filefollow") + .setType(eventType) .setCode(base.toString()) .setPath(path) .setMessage(line) @@ -139,7 +145,7 @@ public class FileFollowEventService implements Callable, Closeable { return charBuffer.toString(); } - private void fillFileSizes(Path base, Pattern pattern) throws IOException { + private void setFileSizes(Path base, Pattern pattern) throws IOException { if (!Files.exists(base)) { return; } diff --git a/event-common/src/test/java/org/xbib/event/path/FileFollowEventManagerTest.java b/event-common/src/test/java/org/xbib/event/path/FileFollowEventManagerTest.java index 62a697a..91dabc1 100644 --- a/event-common/src/test/java/org/xbib/event/path/FileFollowEventManagerTest.java +++ b/event-common/src/test/java/org/xbib/event/path/FileFollowEventManagerTest.java @@ -21,11 +21,13 @@ public class FileFollowEventManagerTest { TestFileFollowEventConsumer consumer = new TestFileFollowEventConsumer(); Settings settings = Settings.settingsBuilder() .put("event.filefollow.testfilefollowevent.enabled", "true") + .put("event.filefollow.testfilefollowevent.type", "filefollow-test") .put("event.filefollow.testfilefollowevent.base", path.toString()) .put("event.filefollow.testfilefollowevent.pattern", ".*") .build(); EventManager eventManager = EventManager.builder() .setSettings(settings) + .register("filefollow-test", TestFileFollowEvent.class) .register(consumer) .build(); Thread.sleep(1000L); diff --git a/event-common/src/test/java/org/xbib/event/path/PathEventManagerTest.java b/event-common/src/test/java/org/xbib/event/path/PathEventManagerTest.java index cd1ab50..aa81b44 100644 --- a/event-common/src/test/java/org/xbib/event/path/PathEventManagerTest.java +++ b/event-common/src/test/java/org/xbib/event/path/PathEventManagerTest.java @@ -50,8 +50,8 @@ public class PathEventManagerTest { TestPathEventConsumer consumer = new TestPathEventConsumer(); Settings settings = Settings.settingsBuilder() .put("event.path.testpathevent.enabled", "true") - .put("event.path.testpathevent.path", path.toString()) .put("event.path.testpathevent.type", "path-ext") + .put("event.path.testpathevent.path", path.toString()) .build(); EventManager eventManager = EventManager.builder() .setSettings(settings) diff --git a/event-common/src/test/java/org/xbib/event/path/TestFileFollowEvent.java b/event-common/src/test/java/org/xbib/event/path/TestFileFollowEvent.java new file mode 100644 index 0000000..3dd2c11 --- /dev/null +++ b/event-common/src/test/java/org/xbib/event/path/TestFileFollowEvent.java @@ -0,0 +1,10 @@ +package org.xbib.event.path; + +import org.xbib.event.common.EventManager; +import org.xbib.event.common.FileFollowEventImpl; + +public class TestFileFollowEvent extends FileFollowEventImpl { + public TestFileFollowEvent(EventManager.EventBuilder builder) { + super(builder); + } +} diff --git a/event-common/src/test/java/org/xbib/event/path/TestFileFollowEventConsumer.java b/event-common/src/test/java/org/xbib/event/path/TestFileFollowEventConsumer.java index 6017293..0b6f68f 100644 --- a/event-common/src/test/java/org/xbib/event/path/TestFileFollowEventConsumer.java +++ b/event-common/src/test/java/org/xbib/event/path/TestFileFollowEventConsumer.java @@ -1,7 +1,6 @@ package org.xbib.event.path; import org.xbib.event.EventConsumer; -import org.xbib.event.FileFollowEvent; import org.xbib.event.bus.AllowConcurrentEvents; import org.xbib.event.bus.Subscribe; @@ -14,7 +13,7 @@ public class TestFileFollowEventConsumer implements EventConsumer { @Subscribe @AllowConcurrentEvents - void onEvent(FileFollowEvent event) { - logger.log(Level.INFO, "received filefollow event, path = " + event.getPath() + " content = " + event.getMessage()); + void onEvent(TestFileFollowEvent event) { + logger.log(Level.INFO, "received test filefollow event, path = " + event.getPath() + " content = " + event.getMessage()); } }