Skip to content

Instantly share code, notes, and snippets.

@shadowbq
Last active August 29, 2015 14:07
Show Gist options
  • Save shadowbq/10119fbb3d612266c896 to your computer and use it in GitHub Desktop.
Save shadowbq/10119fbb3d612266c896 to your computer and use it in GitHub Desktop.
Celluloid rb-kqueue on freebsd test
require 'rubygems'
require 'rb-kqueue'
require 'find'
#test
class BSD
include Celluloid
EVENTS = [:delete, :write, :extend, :attrib, :rename]
attr_accessor :listener
def initialize(listener)
@listener = listener
end
def start
worker = _init_worker
Thread.new { worker.poll }
end
def _init_worker
KQueue::Queue.new.tap do |queue|
_directories_path.each do |path|
Find.find(path) { |file_path| _watch_file(filepath, queue) }
end
end
end
def _worker_callback
lambda do |event|
_notify_change(_event_path(event), type: 'File', change: _change(event.flags))
# If it is a directory, and it has a write flag, it means a
# file has been added so find out which and deal with it.
# No need to check for removed files, kqueue will forget them
# when the vfs does.
_watch_for_new_file(event) if _new_file_added?(event)
end
end
def _change(event_flags)
change_events = {:modified=>[:attrib, :extend], :added=>[:write], :removed=>[:rename, :delete]}
change_events.each do |change, flags|
return change unless (flags & event_flags).empty?
end
nil
end
def _event_path(event)
Pathname.new(event.watcher.path)
end
def _watch_file(path, queue)
queue.watch_file(path, *EVENTS, &_worker_callback)
end
def _latency
listener.options[:latency] || DEFAULT_LATENCY
end
def _directories_path
listener.directories.map(&:to_s)
end
def _notify_change(path, options)
sleep 0.01 until listener.registry[:change_pool]
listener.registry[:change_pool].async.change(path, options) if listener.listen?
end
end
*** LOCAL GEMS ***
benchmark-ips (1.2.0)
benchmark_suite (1.0.0)
bundler (1.3.5, 1.3.1)
celluloid (0.15.2, 0.15.0)
coderay (1.1.0)
coveralls (0.7.0)
diff-lcs (1.2.5)
docile (1.1.1)
ffi (1.9.3)
formatador (0.2.4)
god (0.13.3)
guard (2.2.4)
guard-rspec (4.2.0)
guard-shell (0.5.2)
hiera (1.1.2)
json_pure (1.7.7)
listen (2.3.1)
lumberjack (1.0.4)
method_source (0.8.2)
mime-types (2.0)
multi_json (1.8.2)
open4 (1.3.0)
Platform (0.4.0)
popen4 (0.1.2)
pry (0.9.12.4)
rake (10.1.0)
rb-fsevent (0.9.3)
rb-inotify (0.9.2)
rb-kqueue (0.2.0)
rest-client (1.6.7)
rgen (0.6.6)
rspec (2.14.1)
rspec-core (2.14.7)
rspec-expectations (2.14.4)
rspec-mocks (2.14.4)
rspec-retry (0.2.1)
shellex (1.0.2)
simplecov (0.8.2)
simplecov-html (0.8.0)
slop (3.4.7)
term-ansicolor (1.2.2)
thor (0.18.1)
timers (1.1.0)
tins (0.13.1)
yard (0.8.7.3)
guard :shell do
watch(%r{^bar|foo.man$}) { system("touch /tmp/point")}
end
#!/usr/bin/env ruby
require 'rubygems'
require 'rb-kqueue'
queue = KQueue::Queue.new
queue.watch_file("/tmp/bar", :write) {puts "bar was modified!"}
queue.run
#!/usr/bin/env ruby
require 'rubygems'
require 'rb-kqueue'
require 'listen'
listener = Listen.to('/tmp/flag') do |modified, added, removed|
puts "modified absolute path: #{modified}"
puts "added absolute path: #{added}"
puts "removed absolute path: #{removed}"
end
listener.start # not blocking
sleep
#!/usr/bin/env ruby
require 'rubygems'
require 'listen'
require 'shellex'
#BSD requires
require 'rb-kqueue'
listener = Listen.to('/usr/local/etc/service-config') do |modified, added, removed|
puts shellex('/usr/local/etc/rc.d/service restart').to_s
end
listener.start
sleep
@shadowbq
Copy link
Author

shadowbq commented Oct 1, 2014

Test files .. kqueue was having issues in FreeBSD.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment