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

abstracting more agent logic

parent fd57abd9
......@@ -20,8 +20,8 @@ rescue LoadError => e
exit
end
LOGGER = Logger.new('rundeck-test.log')
#LOGGER = Logger.new(STDOUT)
#LOGGER = Logger.new('rundeck-test.log')
LOGGER = Logger.new(STDOUT)
EventMachine.run do
EM.error_handler do |e|
......
......@@ -42,7 +42,7 @@ module Noah
e,m = msg.split("|")
# Below isn't being used right now
#be = Base64.encode64(e).gsub("\n","")
EM::Iterator.new(@@agents).each do |agent, iter|
EM::Iterator.new(@@agents, @@agents.size).each do |agent, iter|
agent.send(:notify, e, m, @@watchers.clone)
iter.next
end
......
......@@ -3,14 +3,27 @@ require 'logger'
module Noah::Agents
module Base
class << self
include EM::Deferrable
PREFIX = "base"
NAME = "base-agent"
end
def self.included(base)
Noah::Watchers.register_agent(base)
base.send :include, EM::Deferrable
base.send :extend, AgentClassMethods
end
end
module AgentClassMethods
def find_watched_patterns!(watchlist)
watched_patterns = []
watchlist.find_all do |w|
p, ep = Base64.decode64(w).split('|')
watched_patterns << "#{p}|#{ep}" if ep =~ /^#{self.const_get("PREFIX")}/
end
watched_patterns
end
end
end
......@@ -4,21 +4,20 @@ module Noah::Agents
class DummyAgent
include Noah::Agents::Base
PREFIX = "dummy"
PREFIX = "dummy://"
NAME = "dummy"
def self.notify(event, message, watch_list)
logger = LOGGER
logger.info("#{NAME}: Worker initiated")
logger.debug("#{NAME}: got event - #{event}")
matches = watch_list.find_all{|w| event =~ /^#{Base64.decode64(w)}/}
logger.debug("#{NAME}: Found #{matches.size} possible matches for #{event}")
watched_patterns = find_watched_patterns!(watch_list)
matches = watched_patterns.find_all {|w| event =~ /^#{w}/}
logger.debug("#{NAME}: Found #{matches.size} matches for #{event}")
EM::Iterator.new(matches).each do |watch, iter|
p, ep = Base64.decode64(watch).split("|")
if ep =~ /^#{PREFIX}/
logger.info("#{NAME}: Sending message to: #{ep} for pattern: #{p}")
logger.debug("#{NAME}: message received: #{message}")
end
p, ep = watch.split("|")
logger.info("#{NAME}: Sending message to: #{ep} for pattern: #{p}")
logger.debug("#{NAME}: message received: #{message}")
iter.next
end
end
......
......@@ -4,31 +4,26 @@ module Noah::Agents
class HttpAgent
include Noah::Agents::Base
PREFIX = "http"
PREFIX = "http://"
NAME = "http"
def self.notify(event, message, watch_list)
logger = LOGGER
logger.info("#{NAME}: Worker initiated")
logger.debug("#{NAME}: got event - #{event}")
# TODO
# I can save work by only decoding once.
# This is retarded doing it twice.
# Populate matches with decoded data for chrissakes
matches = watch_list.find_all{|w| event =~ /^#{Base64.decode64(w)}/}
logger.debug("#{PREFIX}: Found #{matches.size} possible matches for #{event}")
logger.debug("#{NAME}: got event - #{event}")
watched_patterns = find_watched_patterns!(watch_list)
matches = watched_patterns.find_all {|w| event =~ /^#{w}/}
logger.debug("#{NAME}: Found #{matches.size} matches for #{event}")
EM::Iterator.new(matches, 100).each do |watch, iter|
p, ep = Base64.decode64(watch).split("|")
if ep =~ /^#{PREFIX}/
logger.info("#{NAME}: Sending message to (#{ep}) for pattern (#{p})")
http = EM::HttpRequest.new(ep, :connection_timeout => 2, :inactivity_timeout => 4).post :body => message
http.callback {
logger.info("#{NAME}: Message posted to #{ep} successfully")
}
http.errback {
logger.error("#{NAME}: Something went wrong with #{ep}")
}
end
p, ep = watch.split("|")
logger.info("#{NAME}: Sending message to (#{ep}) for pattern (#{p})")
http = EM::HttpRequest.new(ep, :connection_timeout => 2, :inactivity_timeout => 4).post :body => message
http.callback {
logger.info("#{NAME}: Message posted to #{ep} successfully")
}
http.errback {
logger.error("#{NAME}: Something went wrong with #{ep}")
}
iter.next
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