Commit 9448cdee authored by John E. Vincent's avatar John E. Vincent

updating examples using DSL

parent 82605a8c
#!/usr/bin/env ruby #!/usr/bin/env ruby
require './watcher-idea.rb'
Noah::Watcher.watch do require File.join(File.dirname(__FILE__), '..','lib','noah','watcher')
class StdoutWatcher < Noah::Watcher
redis_host "redis://127.0.0.1:6379/6"
pattern "noah.Noah::Configuration*" pattern "noah.Noah::Configuration*"
destination Proc.new {|x| puts x} destination Proc.new {|x| puts x}
run! run!
......
require 'sinatra' require 'sinatra'
require 'json'
post '/webhook' do post '/webhook' do
p request.body.read x = request.body.read
puts JSON.load(x)
end end
#!/usr/bin/env ruby #!/usr/bin/env ruby
$:.unshift(File.expand_path(File.join(File.dirname(__FILE__), "..", "lib")))
require 'rubygems'
require 'em-hiredis'
require 'em-http-request'
EventMachine.run do require File.join(File.dirname(__FILE__), '..','lib','noah','watcher')
require 'excon'
# Passing messages...like a boss class HttpPostWatch < Noah::Watcher
@channel = EventMachine::Channel.new redis_host "redis://127.0.0.1:6379/1"
pattern "noah.Noah::Configuration*"
r = EventMachine::Hiredis::Client.connect destination Proc.new {|x| ::Excon.post("http://localhost:4567/webhook", :headers => {"Content-Type" => "application/json"}, :body => x)}
r.psubscribe("noah.*") run!
r.on(:pmessage) do |pattern, event, message|
puts "Got message for #{event}"
@channel.push "#{message}"
end
request = EventMachine::HttpRequest.new('http://localhost:4567/webhook')
sub = @channel.subscribe { |msg|
http = request.post(:body => msg, :head => {"Content-Type" => "application/json"})
}
end end
#!/usr/bin/env ruby #!/usr/bin/env ruby
require './watcher-idea.rb'
require 'logger'
log = Logger.new(STDOUT) require File.join(File.dirname(__FILE__), '..','lib','noah','watcher')
require 'logger'
Noah::Watcher.watch do class LoggingWatcher < Noah::Watcher
redis_host "redis://127.0.0.1:6379/5"
pattern "noah.Noah::Application*" pattern "noah.Noah::Application*"
destination Proc.new {|x| log.debug(x)} destination Proc.new {|x| log = Logger.new(STDOUT); log.debug(x)}
run! run!
end end
require 'eventmachine' require 'eventmachine'
require 'uri' require 'uri'
require 'logger'
@log = Logger.new(STDOUT)
@log.level = Logger::DEBUG
require File.join(File.dirname(__FILE__), 'passthrough') require File.join(File.dirname(__FILE__), 'passthrough')
require File.join(File.dirname(__FILE__), '..','vendor','em-hiredis','lib','em-hiredis') require File.join(File.dirname(__FILE__), '..','vendor','em-hiredis','lib','em-hiredis')
...@@ -42,17 +46,26 @@ module Noah ...@@ -42,17 +46,26 @@ module Noah
private private
def self.run_watcher(dest) def self.run_watcher(dest)
log = Logger.new(STDOUT)
log.level = Logger::INFO
log.debug "#{dest.inspect}"
redis_url = URI.parse(@my_redis) redis_url = URI.parse(@my_redis)
db = redis_url.path.gsub(/\//,'') db = redis_url.path.gsub(/\//,'')
EventMachine.run do EventMachine.run do
trap("TERM") { log.info "Killed"; EventMachine.stop }
trap("INT") { log.info "Interrupted"; EventMachine.stop }
channel = EventMachine::Channel.new channel = EventMachine::Channel.new
r = EventMachine::Hiredis::Client.connect(redis_url.host, redis_url.port) r = EventMachine::Hiredis::Client.connect(redis_url.host, redis_url.port)
log.info "Binding to pattern #{db}:#{@my_pattern}"
r.psubscribe("#{db}:#{@my_pattern}") r.psubscribe("#{db}:#{@my_pattern}")
r.on(:pmessage) do |pattern, event, message| r.on(:pmessage) do |pattern, event, message|
log.debug "Got message"
channel.push "#{message}" channel.push "#{message}"
end end
r.errback { log.info "Something went tango-uniform"; EventMachine.stop }
sub = channel.subscribe {|msg| dest.call(msg)} sub = channel.subscribe {|msg| log.info "Calling message handler"; dest.call(msg)}
end end
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