Skip to content

Commit

Permalink
NH-98975: don't check filename in event just reload config
Browse files Browse the repository at this point in the history
  • Loading branch information
cleverchuk committed Jan 21, 2025
1 parent 20bc207 commit f7a521c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,10 @@ private static void attachConfigurationFileWatcher() {

ConfigurationFileWatcher.restartWatch(
Paths.get(configurationFileDir),
runtimeConfigFilename,
watchPeriod,
watchService,
watchScheduler,
filePath -> {
() -> {
try {
logger.info("Configuration file change detected. Reloading configuration");
loadConfigurations();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,11 @@
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;

public final class ConfigurationFileWatcher {
private final Path directory;

private final String filename;

private final Consumer<Path> fileChangeListener;
private final Runnable fileChangeListener;

private final WatchService watchService;

Expand All @@ -48,13 +45,11 @@ public final class ConfigurationFileWatcher {

private ConfigurationFileWatcher(
Path directory,
String filename,
long watchPeriod,
WatchService watchService,
ScheduledExecutorService executorService,
Consumer<Path> fileChangeListener) {
Runnable fileChangeListener) {
this.directory = directory;
this.filename = filename;
this.watchPeriod = watchPeriod;
this.executorService = executorService;
this.fileChangeListener = fileChangeListener;
Expand All @@ -63,22 +58,16 @@ private ConfigurationFileWatcher(

public static void restartWatch(
Path directory,
String filename,
long watchPeriod,
WatchService watchService,
ScheduledExecutorService scheduledExecutorService,
Consumer<Path> fileChangeListener) {
Runnable fileChangeListener) {
if (INSTANCE != null) {
INSTANCE.cancelWatch();
}
INSTANCE =
new ConfigurationFileWatcher(
directory,
filename,
watchPeriod,
watchService,
scheduledExecutorService,
fileChangeListener);
directory, watchPeriod, watchService, scheduledExecutorService, fileChangeListener);
INSTANCE.startWatch();
}

Expand All @@ -91,18 +80,15 @@ private void watch() {
continue;
}

Object filePath = event.context();
if (filePath instanceof Path && ((Path) filePath).endsWith(this.filename)) {
fileChangeListener.accept(((Path) filePath));
}
fileChangeListener.run();
}
boolean valid = key.reset();
if (!valid) {
LoggerFactory.getLogger()
.info(
String.format(
"Watch ended for directory(%s) and file(%s) because the directory became inaccessible",
directory, filename));
"Watch ended for directory(%s) because the directory became inaccessible",
directory));
}
}
}
Expand All @@ -121,8 +107,8 @@ private void startWatch() {
LoggerFactory.getLogger()
.info(
String.format(
"Failed to start watch for directory(%s) and file(%s) due to error - %s",
directory, filename, exception));
"Failed to start watch for directory(%s) due to error - %s",
directory, exception));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.util.Collections;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.function.Consumer;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
Expand All @@ -42,16 +41,14 @@ class ConfigurationFileWatcherTest {

@Mock private ScheduledExecutorService scheduledExecutorServiceMock;

@Mock private Consumer<Path> fileChangeListerMock;
@Mock private Runnable fileChangeListerMock;

@Mock private ScheduledFuture<?> scheduledFutureMock;

@Mock private Path dirMock;

@Captor private ArgumentCaptor<Runnable> runnableArgumentCaptor;

private final String filename = "file.json";

private final long watchPeriod = 1;

@Test
Expand All @@ -66,12 +63,7 @@ void verifyThatOverflowEventsAreIgnored() throws IOException {
.thenAnswer(invocation -> scheduledFutureMock);

ConfigurationFileWatcher.restartWatch(
dirMock,
filename,
watchPeriod,
watchServiceMock,
scheduledExecutorServiceMock,
fileChangeListerMock);
dirMock, watchPeriod, watchServiceMock, scheduledExecutorServiceMock, fileChangeListerMock);

runnableArgumentCaptor.getValue().run();
verify(watchServiceMock).poll();
Expand All @@ -85,7 +77,6 @@ void verifyThatFileChangeListenerIsInvokedWhenFileIsModified() throws IOExceptio
when(watchKeyMock.pollEvents()).thenReturn(Collections.singletonList(watchEventMock));
when(watchEventMock.kind()).thenAnswer(invocation -> StandardWatchEventKinds.ENTRY_MODIFY);

when(watchEventMock.context()).thenReturn(Paths.get(filename));
when(watchKeyMock.reset()).thenReturn(false);
when(dirMock.register(any(), any())).thenReturn(watchKeyMock);

Expand All @@ -94,19 +85,13 @@ void verifyThatFileChangeListenerIsInvokedWhenFileIsModified() throws IOExceptio
.thenAnswer(invocation -> scheduledFutureMock);

ConfigurationFileWatcher.restartWatch(
dirMock,
filename,
watchPeriod,
watchServiceMock,
scheduledExecutorServiceMock,
fileChangeListerMock);
dirMock, watchPeriod, watchServiceMock, scheduledExecutorServiceMock, fileChangeListerMock);

runnableArgumentCaptor.getValue().run();

verify(watchServiceMock).poll();
verify(watchKeyMock).pollEvents();
verify(watchEventMock).context();

verify(fileChangeListerMock).accept(any());
verify(fileChangeListerMock).run();
}
}

0 comments on commit f7a521c

Please sign in to comment.