guard / rb-inotify

A thorough inotify wrapper for Ruby using FFI.
MIT License
312 stars 64 forks source link

Requires reregistering of watchers #18

Closed petur03 closed 4 years ago

petur03 commented 12 years ago

Hi

I've been using your gem, but it's not working quite as I would expect.

It seems that I have to reregister the watcher whenever an event has occurred. So running notifier.run will loop forever, but only handle each registered event once. To continuously handle events I have to use notifier.process in a loop and run notifier.watch after process, see example below. If I omit the call to watch() in the loop, the handler will only be called the first time the file is modified.

I don't know if this is intended behaviour or not, at least I don't think it is reflected in the readme.

Thanks, Petur

require 'rb-inotify'

handler = Proc.new { puts "handle" }

notifier = INotify::Notifier.new
notifier.watch("./file.txt", :modify, &handler)

loop {
  notifier.process
  notifier.watch("./file.txt", :modify, &handler)
}
nex3 commented 11 years ago

I can't reproduce this. The handler never gets unregistered for me.

lym commented 11 years ago

I was experiencing the same problem. Used @petur03 implementation and it works like I expected. I should probably mention that am running it against ruby-2.0.0-p0 on an Ubuntu 11.10 box.

eclubb commented 10 years ago

I had the same problem. For me, it was because I was editing my file with Vim. If I echoed text to the file, it worked fine.

I found the solution to my problem in this Super User answer: http://superuser.com/a/181543

I implemented his solution as such:

require 'rb-inotify'

notifier = INotify::Notifier.new

notifier.watch('.', :close_write, :moved_to, :create) do |event|
  puts 'dummy.txt was updated' if event.name == 'dummy.txt'
end

notifier.run

This detects the change whether the file is written to directly or replaced with another file.

ytti commented 10 years ago

Yeah I think this is expected, it's monitoring specific inode, and editors regularly delete file and write new one, so you'll just get delete event and that's it, inode you're watching is gone. Maybe high-level API should have option to follow file name, essentially implement solution like eclubb's when given argument like :follow?

eclubb commented 10 years ago

I like the idea of hiding behind a :follow arg.

nex3 commented 10 years ago

I'm fine with having a flag for this, but I'm not a fan of the name :follow; it sounds like it's referring to symlinks somehow. I'd prefer something like :resubscribe.

ytti commented 10 years ago

No contest on name, fine with any. Another method is also option, perhaps #watch_name, would avoid overloading configuration arguments with event types and have more obvious fail with older versions of API ran against code using it.

nex3 commented 10 years ago

We already overload configuration arguments for :recursive, but you make a good point about providing an obvious error. We should really be throwing an error for unrecognized flags anyway.

I think I slightly prefer a flag, since that allows flags to compose more seamlessly.

ytti commented 10 years ago

Completely okey with flag, just spitballing.

digitalextremist commented 10 years ago

I also see this failure to re-register events, under Ubuntu 14.04 with jRuby 1.7.12

paoloaga commented 8 years ago

The solution is to handle the flag ":ignored" and re-instance the notifier.watch whenever you receive one.

ioquatix commented 7 years ago

Now that we have test, it would be great to add a spec for this including some documentation for the correct way this should be handled.

ioquatix commented 4 years ago

Feel free to submit a failing PR either here or in the listen gem.