Skip to content

Commit

Permalink
fix(file.util): avoid inotify problems in containers by using polling…
Browse files Browse the repository at this point in the history
… over FileWatcher (#38)

* fix(file.util): avoid inotify problems in containers by using polling over FileWatcher

* fixup! fix(file.util): avoid inotify problems in containers by using polling over FileWatcher

* fixup! fixup! fix(file.util): avoid inotify problems in containers by using polling over FileWatcher

* Update lib/src/main/java/com/dynatrace/file/util/FilePollerFactory.java

Co-authored-by: Armin Ruech <7052238+arminru@users.noreply.github.com>

---------

Co-authored-by: Armin Ruech <7052238+arminru@users.noreply.github.com>
  • Loading branch information
pichlermarc and arminru authored Apr 29, 2024
1 parent 62c0192 commit 15df84c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 13 deletions.
2 changes: 1 addition & 1 deletion lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ plugins {
}

group 'com.dynatrace.metric.util'
version = '2.2.0'
version = '2.2.1'

repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,15 @@ private void setUp(String fileName, Duration pollInterval) {
if (!Files.exists(Paths.get(fileName))) {
logger.info("File based configuration does not exist, serving default config.");
} else {
poller = FilePollerFactory.getDefault(fileName, pollInterval);
poller =
FilePollerFactory.getPollBased(
fileName, pollInterval != null ? pollInterval : Duration.ofSeconds(60));
}
} catch (InvalidPathException e) {
// This happens on Windows, when the *nix filepath is not valid.
logger.info(
() -> String.format("%s is not a valid file path (%s).", fileName, e.getMessage()));
} catch (IOException | IllegalArgumentException e) {
} catch (IllegalArgumentException e) {
logger.warning(
() -> String.format("File polling could not be initialized: %s", e.getMessage()));
}
Expand Down
19 changes: 9 additions & 10 deletions lib/src/main/java/com/dynatrace/file/util/FilePollerFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,18 @@ class FilePollerFactory {
private FilePollerFactory() {}

/**
* Creates the default {@link FilePoller} based on the current OS.
* Creates the default {@link FilePoller}.
*
* @implNote On Linux and Windows, * the poll mechanism is based on the {@link
* java.nio.file.WatchService WatchService}. For macOS, * where there is no native
* implementation for the {@link java.nio.file.WatchService WatchService} * bindings, this
* method provides a {@link PollBasedFilePoller}, which polls the file of interest *
* periodically (according to the {@code pollInterval}).
* @implNote This method may choose to use a {@link java.nio.file.WatchService}-based
* implementation which can be problematic when used in conjunction with container
* bind-mounts. If the resulting {@link FilePoller} will be used in such a manner, consider
* using {@link FilePollerFactory#getPollBased(String, Duration)} instead.
* @param fileName The name of the file to be watched.
* @param pollInterval The interval in which the {@link PollBasedFilePoller} polls for changes.
* Only applicable on macOS. Will default to 60s if {@code null} is passed.
* @param pollInterval The interval in which the {@link FilePoller} will update, if applicable.
* Interval MAY not apply to {@link java.nio.file.WatchService} based implementations. Will
* default to 60s if {@code null} is passed.
* @return An object that implements the abstract methods on {@link FilePoller}.
* @throws IOException if the initialization of the {@link WatchServiceBasedFilePoller} is not
* successful.
* @throws IOException if the initialization of the {@link FilePoller} is not successful
*/
static FilePoller getDefault(String fileName, Duration pollInterval) throws IOException {
if (IS_MAC_OS) {
Expand Down

0 comments on commit 15df84c

Please sign in to comment.