= rb-fsevent Very simple & usable Mac OSX FSEvents API - RubyCocoa not required! - Signals are working (really) - Tested on MRI 1.8.7 & 1.9.2, JRuby 1.6.3 - Tested on 10.6 & 10.7 (though 10.5 should work just as well) - Tested with XCode 3.2.6, 4.0.2, 4.1, 4.2b5 == Install gem install rb-fsevent == Usage === Singular path require 'rb-fsevent' fsevent = FSEvent.new fsevent.watch Dir.pwd do |directories| puts "Detected change inside: #{directories.inspect}" end fsevent.run === Multiple paths require 'rb-fsevent' paths = ['/tmp/path/one', '/tmp/path/two', Dir.pwd] fsevent = FSEvent.new fsevent.watch paths do |directories| puts "Detected change inside: #{directories.inspect}" end fsevent.run === Multiple paths and additional options as a Hash require 'rb-fsevent' paths = ['/tmp/path/one', '/tmp/path/two', Dir.pwd] options = {:latency => 1.5, :no_defer => true } fsevent = FSEvent.new fsevent.watch paths, options do |directories| puts "Detected change inside: #{directories.inspect}" end fsevent.run === Multiple paths and additional options as an Array require 'rb-fsevent' paths = ['/tmp/path/one', '/tmp/path/two', Dir.pwd] options = ['--latency', 1.5, '--no-defer'] fsevent = FSEvent.new fsevent.watch paths, options do |directories| puts "Detected change inside: #{directories.inspect}" end fsevent.run == Options When defining options using a hash or hash-like object, it gets checked for validity and converted to the appropriate fsevent_watch commandline arguments array when the FSEvent class is instantiated. This is obviously the safest and preferred method of passing in options. You may, however, choose to pass in an array of commandline arguments as your options value and it will be passed on, unmodified, to the fsevent_watch binary when called. So far, the following options are supported... - :latency => 0.5 # in seconds - :no_defer => true - :watch_root => true - :since_when => 18446744073709551615 # an FSEventStreamEventId === Latency The :latency parameter determines how long the service should wait after the first event before passing that information along to the client. If your latency is set to 4 seconds, and 300 changes occur in the first three, then the callback will be fired only once. If latency is set to 0.1 in the exact same scenario, you will see that callback fire somewhere closer to between 25 and 30 times. Setting a higher latency value allows for more effective temporal coalescing, resulting in fewer callbacks and greater overall efficiency... at the cost of apparent responsiveness. Setting this to a reasonably high value (and NOT setting :no_defer) is particularly well suited for background, daemon, or batch processing applications. Implementation note: It appears that FSEvents will only coalesce events from a maximum of 32 distinct subpaths, making the above completely accurate only when events are to fewer than 32 subpaths. Creating 300 files in one directory, for example, or 30 files in 10 subdirectories, but not 300 files within 300 subdirectories. In the latter case, you may receive 31 callbacks in one go after the latency period. As this appears to be an implementation detail, the number could potentially differ across OS revisions. It is entirely possible that this number is somehow configurable, but I have not yet discovered an accepted method of doing so. === NoDefer The :no_defer option changes the behavior of the latency parameter completely. Rather than waiting for $latency period of time before sending along events in an attempt to coalesce a potential deluge ahead of time, that first event is sent along to the client immediately and is followed by a $latency period of silence before sending along any additional events that occurred within that period. This behavior is particularly useful for interactive applications where that feeling of apparent responsiveness is most important, but you still don't want to get overwhelmed by a series of events that occur in rapid succession. === WatchRoot The :watch_root option allows for catching the scenario where you start watching "~/src/demo_project" and either it is later renamed to "~/src/awesome_sauce_3000" or the path changes in such a manner that the original directory is now at "~/clients/foo/iteration4/demo_project". Unfortunately, while this behavior is somewhat supported in the fsevent_watch binary built as part of this project, support for passing across detailed metadata is not (yet). As a result, you would not receive the appropriate RootChanged event and be able to react appropriately. Also, since the C code doesn't open watched directories and retain that file descriptor as part of path-specific callback metadata, we are unable to issue an F_GETPATH fcntl() to determine the directory's new path. Please do not use this option until proper support is added in an upcoming (planned) release. === SinceWhen The FSEventStreamEventId passed in to :since_when is used as a base for reacting to historic events. Unfortunately, not only is the metadata for transitioning from historic to live events not currently passed along, but it is incorrectly passed as a change event on the root path, and only per-host event streams are currently supported. When using per-host event streams, the event IDs are not guaranteed to be unique or contiguous when shared volumes (firewire/USB/net/etc) are used on multiple macs. Please do not use this option until proper support is added in an upcoming (planned) release, unless it's acceptable for you to receive that one fake event that's handled incorrectly when events transition from historical to live. Even in that scenario, there's no metadata available for determining the FSEventStreamEventId of the last received event. WARNING: passing in 0 as the parameter to :since_when will return events for every directory modified since "the beginning of time". == Debugging output If the gem is installed with the environment variable FWDEBUG set to the string "true", then fsevent_watch will be built with its various DEBUG sections defined, and the output to STDERR is truly verbose (and hopefully helpful in debugging your application and not just fsevent_watch itself). If enough people find this to be directly useful when developing code that makes use of rb-fsevent, then it wouldn't be hard to clean this up and make it a feature enabled by a commandline argument instead. Until somebody files an issue, however, I will assume otherwise. append_path called for: /tmp/moo/cow/ resolved path to: /private/tmp/moo/cow config.sinceWhen 18446744073709551615 config.latency 0.300000 config.flags 00000000 config.paths /private/tmp/moo/cow FSEventStreamRef @ 0x100108540: allocator = 0x7fff705a4ee0 callback = 0x10000151e context = {0, 0x0, 0x0, 0x0, 0x0} numPathsToWatch = 1 pathsToWatch = 0x7fff705a4ee0 pathsToWatch[0] = '/private/tmp/moo/cow' latestEventId = -1 latency = 300000 (microseconds) flags = 0x00000000 runLoop = 0x0 runLoopMode = 0x0 FSEventStreamCallback fired! numEvents: 32 event path: /private/tmp/moo/cow/1/a/ event flags: 00000000 event ID: 1023767 event path: /private/tmp/moo/cow/1/b/ event flags: 00000000 event ID: 1023782 event path: /private/tmp/moo/cow/1/c/ event flags: 00000000 event ID: 1023797 event path: /private/tmp/moo/cow/1/d/ event flags: 00000000 event ID: 1023812 event path: /private/tmp/moo/cow/1/e/ event flags: 00000000 event ID: 1023827 event path: /private/tmp/moo/cow/1/f/ event flags: 00000000 event ID: 1023842 event path: /private/tmp/moo/cow/1/g/ event flags: 00000000 event ID: 1023857 event path: /private/tmp/moo/cow/1/h/ event flags: 00000000 event ID: 1023872 event path: /private/tmp/moo/cow/1/i/ event flags: 00000000 event ID: 1023887 event path: /private/tmp/moo/cow/1/j/ event flags: 00000000 event ID: 1023902 event path: /private/tmp/moo/cow/1/k/ event flags: 00000000 event ID: 1023917 event path: /private/tmp/moo/cow/1/l/ event flags: 00000000 event ID: 1023932 event path: /private/tmp/moo/cow/1/m/ event flags: 00000000 event ID: 1023947 event path: /private/tmp/moo/cow/1/n/ event flags: 00000000 event ID: 1023962 event path: /private/tmp/moo/cow/1/o/ event flags: 00000000 event ID: 1023977 event path: /private/tmp/moo/cow/1/p/ event flags: 00000000 event ID: 1023992 event path: /private/tmp/moo/cow/1/q/ event flags: 00000000 event ID: 1024007 event path: /private/tmp/moo/cow/1/r/ event flags: 00000000 event ID: 1024022 event path: /private/tmp/moo/cow/1/s/ event flags: 00000000 event ID: 1024037 event path: /private/tmp/moo/cow/1/t/ event flags: 00000000 event ID: 1024052 event path: /private/tmp/moo/cow/1/u/ event flags: 00000000 event ID: 1024067 event path: /private/tmp/moo/cow/1/v/ event flags: 00000000 event ID: 1024082 event path: /private/tmp/moo/cow/1/w/ event flags: 00000000 event ID: 1024097 event path: /private/tmp/moo/cow/1/x/ event flags: 00000000 event ID: 1024112 event path: /private/tmp/moo/cow/1/y/ event flags: 00000000 event ID: 1024127 event path: /private/tmp/moo/cow/1/z/ event flags: 00000000 event ID: 1024142 event path: /private/tmp/moo/cow/1/ event flags: 00000000 event ID: 1024145 event path: /private/tmp/moo/cow/2/a/ event flags: 00000000 event ID: 1024160 event path: /private/tmp/moo/cow/2/b/ event flags: 00000000 event ID: 1024175 event path: /private/tmp/moo/cow/2/c/ event flags: 00000000 event ID: 1024190 event path: /private/tmp/moo/cow/2/d/ event flags: 00000000 event ID: 1024205 event path: /private/tmp/moo/cow/2/e/ event flags: 00000000 event ID: 1024220 == Note about FFI rb-fsevent doesn't use {ruby-ffi}[http://github.com/ffi/ffi] anymore because it sadly doesn't allow for catching Signals. You can still see the code in the {ffi branch}[http://github.com/thibaudgg/rb-fsevent/tree/ffi]. == Development - Source hosted at {GitHub}[http://github.com/thibaudgg/rb-fsevent] - Report issues/Questions/Feature requests on {GitHub Issues}[http://github.com/thibaudgg/rb-fsevent/issues] Pull requests are quite welcome! Please ensure that your commits are in a topic branch for each individual changeset than can be reasonably isolated. It is also important to ensure that your changes are well tested... whether that means new tests, modified tests, or fixing a scenario where the existing tests currently fail. If you have rvm and the required rubies currently installed, we have a helper task for running the testsuite in all of them: rake spec:portability The list of tested RVM targets is currently: %w[1.8.6 1.8.7 1.9.2 jruby-head] == Authors - {Thibaud Guillaume-Gentil}[http://github.com/thibaudgg] - {Travis Tilley}[http://github.com/ttilley]