Commit 59d284c9 authored by John E. Vincent's avatar John E. Vincent

watcher examples may be broken but tests pass

parent f7e41c63
......@@ -4,7 +4,7 @@ require File.join(File.dirname(__FILE__), '..','lib','noah','watcher')
class StdoutWatcher < Noah::Watcher
redis_host "redis://127.0.0.1:6379/0"
pattern "//noah/configuration/redis_server.*"
pattern "//noah/configuration/redis_server"
destination Proc.new {|x| puts x}
run!
end
......@@ -5,8 +5,8 @@ require 'excon'
class HttpPostWatch < Noah::Watcher
redis_host "redis://127.0.0.1:6379/1"
pattern "//noah/configuration/*"
redis_host "redis://127.0.0.1:6379/0"
pattern "//noah/configuration"
destination Proc.new {|x| ::Excon.post("http://localhost:4567/webhook", :headers => {"Content-Type" => "application/json"}, :body => x)}
run!
end
......@@ -12,16 +12,16 @@ a.valid?
a.save
a.watch! :endpoint => 'http://localhost:4567/webhook' # Boom headshot
pp Noah::Watcher.find(:pattern => "noah.application.my_kickass_application.*").first
# Default watch pattern is 'noah.model.name.*'
pp Noah::Watcher.find(:pattern => "//noah/application/my_kickass_application").first
# Default watch pattern is '//noah/model/name'
# You can also register a new watch with a custom pattern
b = Noah::Application.new
b.name = "my_other_awesome_app"
# You can also register a new watch with a custom pattern - kind of pointless obviously
b = Noah::Configuration.new
b.name = "my_awesome_configuration"
b.valid?
b.save
b.watch! :endpoint => 'http://localhost:4567/webhook', :pattern => 'delete'
pp Noah::Watcher.find(:pattern => "noah.application.my_other_awesome_app.delete").first
b.watch! :endpoint => 'http://localhost:4567/webhook', :pattern => '//noah/configuration/my_awesome_configuration'
pp Noah::Watcher.find(:pattern => "//noah/configuration/my_awesome_configuration").first
# There's now a watcher for pattern 'noah.application.my_other_awesome_app.delete'
# The idea now is that some background watcher agent will pick up on these registered watches
......
......@@ -5,7 +5,7 @@ require 'logger'
class LoggingWatcher < Noah::Watcher
redis_host "redis://127.0.0.1:6379/5"
pattern "//noah/application/*"
pattern "//noah/application"
destination Proc.new {|x| log = Logger.new(STDOUT); log.debug(x)}
run!
end
......@@ -5,7 +5,7 @@ require 'excon'
class HttpPostWatch < Noah::Watcher
redis_host "redis://127.0.0.1:6379/0"
pattern "//noah/configuration/redis_server.update"
pattern "//noah/configuration/redis_server"
destination Proc.new {|x| puts x; ::Excon.put("http://localhost:4567/webhook", :headers => {"Content-Type" => "application/json"}, :body => x)}
run!
end
require 'rubygems'
require 'logger'
require 'eventmachine'
require 'em-hiredis'
module Noah
class Watcher
attr_accessor :my_pattern, :my_destination
def self.watch(&blk)
watcher = Noah::Watcher.new
watcher.instance_eval(&blk)
watcher
end
def pattern(pattern)
@my_pattern = pattern
end
def destination(destination)
@my_destination = destination
end
def run!
@my_destination.nil? ? (puts "Can't run without a destination") : run_watcher(@my_destination)
end
private
def run_watcher(dest)
watcher_log = Logger.new(STDOUT)
EventMachine.run do
watcher_log.info("Starting Noah Watcher...")
watcher_log.info("Filtering on messages: #{@my_pattern}")
channel = EventMachine::Channel.new
r = EventMachine::Hiredis::Client.connect
r.db(5)
r.psubscribe(@my_pattern)
r.on(:pmessage) do |pattern, event, message|
channel.push "#{message}"
end
sub = channel.subscribe {|msg| dest.call(msg)}
end
end
end
end
......@@ -27,12 +27,9 @@ module Noah
def watch!(opts={:endpoint => nil, :pattern => nil})
# TODO: There's still a condition here for duplicate watches
# i.e. User already has a wildcard for that endpoint and adds a more narrow scope
# need to handle that either here or in the watcher class
base_pattern = "#{self.patternize_me}."
base_pattern = "#{self.patternize_me}"
opts[:endpoint].nil? ? (raise ArgumentError, "Need an endpoint") : endpoint=opts[:endpoint]
opts[:pattern].nil? ? pattern=base_pattern+"*" : pattern=base_pattern+opts[:pattern]
opts[:pattern].nil? ? pattern=base_pattern : pattern=opts[:pattern]
begin
w = Watcher.new :pattern => pattern, :endpoint => endpoint
......@@ -45,7 +42,7 @@ module Noah
protected
def patternize_me
"//noah/#{self.class_to_lower}/#{name.gsub(/^\//,'')}"
"//noah/#{self.class_to_lower}/#{name}"
end
def stash_name
......
......@@ -25,7 +25,7 @@ module Noah
def initialize
@my_redis ||= ENV['REDIS_URL']
@my_pattern ||= 'noah.*'
@my_pattern ||= '//noah'
end
def self.redis_host(host)
......@@ -60,7 +60,7 @@ module Noah
# Pulling out dbnum for now. Need to rethink it
#log.info "Binding to pattern #{db}:#{@my_pattern}"
log.info "Binding to pattern #{@my_pattern}"
r.psubscribe("#{@my_pattern}")
r.psubscribe("#{@my_pattern}*")
r.on(:pmessage) do |pattern, event, message|
log.debug "Got message"
channel.push "#{message}"
......
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