Commit eb2b3bc8 authored by John E. Vincent's avatar John E. Vincent

an idea for custom watchers

parent 68364740
#!/usr/bin/env ruby
require './watcher-idea.rb'
f = Noah::Watcher.new do |f|
f.pattern = "noah.Configuration*"
f.destination = Proc.new {|x| puts x}
end
f.run!
#!/usr/bin/env ruby
$:.unshift(File.expand_path(File.join(File.dirname(__FILE__), "..", "lib")))
require 'rubygems'
require 'eventmachine'
require 'em-hiredis'
require 'logger'
log = Logger.new(STDOUT)
EventMachine.run do
# Passing messages...like a boss
@channel = EventMachine::Channel.new
r = EventMachine::Hiredis::Client.connect
r.psubscribe("noah.*")
r.on(:pmessage) do |pattern, event, message|
@channel.push "#{message}"
end
sub = @channel.subscribe { |msg|
log.debug("#{msg}")
}
end
require 'rubygems'
require 'eventmachine'
require 'em-hiredis'
module Noah
class Watcher
attr_accessor :pattern, :destination
def initialize(&blk)
@pattern ||= "noah.*"
@destination ||= nil
yield(self) if block_given?
end
def run!
@destination.nil? ? (puts "Can't run without a destination") : run_watcher(@destination)
end
private
def run_watcher(dest)
EventMachine.run do
channel = EventMachine::Channel.new
r = EventMachine::Hiredis::Client.connect
r.psubscribe(@pattern)
r.on(:pmessage) do |pattern, event, message|
channel.push "#{message}"
end
sub = channel.subscribe {|msg| dest.call(msg)}
end
end
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment