Commit 6b8788c5 authored by John E. Vincent's avatar John E. Vincent

converting logger and custom examples to new syntax

parent 8ec99b8a
#!/usr/bin/env ruby #!/usr/bin/env ruby
require './watcher-idea.rb' require './watcher-idea.rb'
f = Noah::Watcher.new do |f| Noah::Watcher.watch do
f.pattern = "noah.Configuration*" pattern "noah.Configuration*"
f.destination = Proc.new {|x| puts x} destination Proc.new {|x| puts x}
run!
end end
f.run!
#!/usr/bin/env ruby #!/usr/bin/env ruby
$:.unshift(File.expand_path(File.join(File.dirname(__FILE__), "..", "lib"))) require './watcher-idea.rb'
require 'rubygems'
require 'eventmachine'
require 'em-hiredis'
require 'logger' require 'logger'
log = Logger.new(STDOUT) log = Logger.new(STDOUT)
EventMachine.run do Noah::Watcher.watch do
pattern "noah.Application*"
# Passing messages...like a boss destination Proc.new {|x| log.debug(x)}
@channel = EventMachine::Channel.new run!
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 end
require 'rubygems' require 'rubygems'
require 'logger'
require 'eventmachine' require 'eventmachine'
require 'em-hiredis' require 'em-hiredis'
module Noah module Noah
class Watcher class Watcher
attr_accessor :pattern, :destination attr_accessor :my_pattern, :my_destination
def initialize(&blk) def self.watch(&blk)
@pattern ||= "noah.*" watcher = Noah::Watcher.new
@destination ||= nil watcher.instance_eval(&blk)
yield(self) if block_given? watcher
end
def pattern(pattern)
@my_pattern = pattern
end
def destination(destination)
@my_destination = destination
end end
def run! def run!
@destination.nil? ? (puts "Can't run without a destination") : run_watcher(@destination) @my_destination.nil? ? (puts "Can't run without a destination") : run_watcher(@my_destination)
end end
private private
def run_watcher(dest) def run_watcher(dest)
watcher_log = Logger.new(STDOUT)
EventMachine.run do EventMachine.run do
watcher_log.info("Starting Noah Watcher...")
watcher_log.info("Filtering on messages: #{@my_pattern}")
channel = EventMachine::Channel.new channel = EventMachine::Channel.new
r = EventMachine::Hiredis::Client.connect r = EventMachine::Hiredis::Client.connect
r.psubscribe(@pattern) r.psubscribe(@my_pattern)
r.on(:pmessage) do |pattern, event, message| r.on(:pmessage) do |pattern, event, message|
channel.push "#{message}" channel.push "#{message}"
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