Commit 4375abb6 authored by John E. Vincent's avatar John E. Vincent

adding a watch! method to all models

parent a375ff77
......@@ -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.Noah::Configuration.redis_server.update"
pattern "noah.configuration.redis_server.update"
destination Proc.new {|x| puts x}
run!
end
......@@ -3,9 +3,10 @@
require File.join(File.dirname(__FILE__), '..','lib','noah','watcher')
require 'excon'
class HttpPostWatch < Noah::Watcher
redis_host "redis://127.0.0.1:6379/1"
pattern "noah.Noah::Configuration*"
pattern "noah.configuration.*"
destination Proc.new {|x| ::Excon.post("http://localhost:4567/webhook", :headers => {"Content-Type" => "application/json"}, :body => x)}
run!
end
#!/usr/bin/env ruby
require 'pp'
require File.join(File.dirname(__FILE__), '..','lib','noah.rb')
# This doesn't actually send watch notifications
# but it shows how you could register a new watch
# for the agents to handle
a = Noah::Application.new
a.name = "my_kickass_application"
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.*'
# You can also register a new watch with a custom pattern
b = Noah::Application.new
b.name = "my_other_awesome_app"
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
# 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
# See https://github.com/lusis/Noah/wiki/Watcher-Braindump
......@@ -5,7 +5,7 @@ require 'logger'
class LoggingWatcher < Noah::Watcher
redis_host "redis://127.0.0.1:6379/5"
pattern "noah.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.Noah::Configuration.redis_server.update"
pattern "noah.configuration.redis_server.update"
destination Proc.new {|x| puts x; ::Excon.put("http://localhost:4567/webhook", :headers => {"Content-Type" => "application/json"}, :body => x)}
run!
end
......@@ -24,11 +24,35 @@ module Noah
self.created_at == self.updated_at
end
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}."
opts[:endpoint].nil? ? (raise ArgumentError, "Need an endpoint") : endpoint=opts[:endpoint]
opts[:pattern].nil? ? pattern=base_pattern+"*" : pattern=base_pattern+opts[:pattern]
begin
w = Watcher.new :pattern => pattern, :endpoint => endpoint
w.valid? ? w.save : (raise "#{w.errors}")
w.name
rescue Exception => e
e.message
end
end
protected
def patternize_me
"noah.#{self.class_to_lower}.#{name}"
end
def stash_name
@deleted_name = self.name
end
def class_to_lower
self.class.to_s.gsub(/(.*)::(\w)/,'\2').downcase
end
def dbnum
o = Ohm.options.first
return "0" if o.nil?
......@@ -43,7 +67,7 @@ module Noah
self.name.nil? ? name=@deleted_name : name=self.name
# Pulling out dbnum for now. Need to rethink it
#pub_category = "#{db}:noah.#{self.class.to_s}[#{name}].#{meth}"
pub_category = "noah.#{self.class.to_s}.#{name}.#{meth}"
pub_category = "#{self.patternize_me}.#{meth}"
Ohm.redis.publish(pub_category, self.to_hash.merge({"action" => meth, "pubcategory" => pub_category}).to_json)
# self.method_defined? "#{meth}_hook".to_sym
end
......
......@@ -4,28 +4,27 @@ module Noah
# Don't trust anything in here yet
# I'm still trying a few things
attribute :client
attribute :pattern
attribute :endpoint
index :client
index :pattern
index :endpoint
def validate
super
assert_present :client
assert_present :endpoint
assert_present :pattern
assert_unique [:client, :endpoint, :pattern]
assert_unique [:endpoint, :pattern]
end
def name
@name = Digest::SHA1.hexdigest "#{client}#{endpoint}#{pattern}"
@name = Digest::SHA1.hexdigest "#{endpoint}#{pattern}"
end
private
def path_to_pattern
# Not sure about these next two.
# Could get around patterns changing due to namespace changes
def path_to_pattern
end
def pattern_to_path
......
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