-
Notifications
You must be signed in to change notification settings - Fork 145
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
More than one events generated for a file modification #313
Comments
I think the file is actually modified twice - once for the content and once for the update time. See: https://stackoverflow.com/questions/16777869/java-7-watchservice-ignoring-multiple-occurrences-of-the-same-event If that is not the case, please reopen this issue. |
I have come up with this work around based on your input. What do you think about it? And if deem fine, why not mention it in the README and place this or similar example (Will definitely save needless debugging knowing this needs to be handled always, anyway!)? trait ConfWatcher {
implicit def actorSystem: ActorSystem
private val confPath = "/home/codingkapoor/application.conf"
private val appConfFile = File(confPath)
private var appConfLastModified = appConfFile.lastModifiedTime
val watcher: ActorRef = appConfFile.newWatcher(recursive = false)
watcher ! on(EventType.ENTRY_MODIFY) { file =>
if (appConfLastModified.compareTo(file.lastModifiedTime) < 0) {
// TODO
appConfLastModified = file.lastModifiedTime
}
}
} |
It definitely is happening more than twice. Actually thrice.
Not sure, if that's a problem and the issue needs to be reopenend! |
@codingkapoor : Yes that's strange. I need some more information:
val watcher = new FileMonitor(CONF_DIR, recursive = true) {
override def onCreate(file: File, count: Int) = println(s"$file got created $count times")
override def onModify(file: File, count: Int) = println(s"$file got modified $count times")
override def onDelete(file: File, count: Int) = println(s"$file got deleted $count times")
override def onUnknownEvent(event: WatchEvent[_]) = println(s"Unknown event (${event.context()}: $event) got triggered ${event.count} times")
}
watcher.start()
Thread.sleep(60 * 1000) // The above line starts the monitoring asynchronously
val watcher = new FileMonitor(CONF_DIR/application.conf, recursive = true) {
|
I'm guessing this is a problem with the JDK In general, though, this is a hard problem to solve. I've tried to solve it in https://github.com/gmethvin/directory-watcher, but there are still some pretty significant trade-offs. Some OSes and filesystems have modification dates up to the millisecond or nanosecond, while others only provide second-level precision, so modification dates aren't universally reliable. It's often better to hash the actual file contents to see what changed, but that can be a bad idea if your directory contains very large files. Ultimately it depends a lot on your particular use case. |
Thanks for the answer @gmethvin . I still want to rule out a better-files bug because better-files does attach watchers recursively if watching directories: https://github.com/pathikrit/better-files/blob/master/core/src/main/scala/better/files/FileMonitor.scala#L54 |
Hi,
I am trying to watch a config file for modifications with the following piece of code:
I could watch a single file instead of a directory, right?
This seems to work but the problem is that everytime I am modififying the file, more than one events are getting generated. This is problematic since the sideeffects in the callback can't be called more than once.
Please suggest how can I achieve this or if I am doing something wrong?
The text was updated successfully, but these errors were encountered: