Skip to content

Instantly share code, notes, and snippets.

@tjsingleton
Last active December 16, 2015 04:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tjsingleton/5376617 to your computer and use it in GitHub Desktop.
Save tjsingleton/5376617 to your computer and use it in GitHub Desktop.
watch a dir for new files and do some work
require 'listen'
require 'rb-fsevent'
LISTEN_PATH = '/tmp/m'
consumer, producer = IO.pipe
Dir["#{LISTEN_PATH}/*"].each {|n| producer.puts n }
2.times do
fork do
puts "consumer started #{$$}"
loop do
if IO.select([consumer], nil, nil, 2)
path = consumer.gets
puts "out[#{$$}]: #{path}"
end
end
end
end
Listen.to(LISTEN_PATH) do |_, added, _|
added.each{|n| producer.puts n }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment