Commit 88e0d993 authored by John E. Vincent's avatar John E. Vincent

Small performance refactor in agent - https://gist.github.com/875564

parent d213ead3
...@@ -127,3 +127,8 @@ end ...@@ -127,3 +127,8 @@ end
task :start_demo do task :start_demo do
puts "Soon, young padawan" puts "Soon, young padawan"
end end
desc "Start an irb session with all libraries loaded"
task :shell do
sh "irb -r./lib/noah.rb"
end
...@@ -20,7 +20,8 @@ rescue LoadError => e ...@@ -20,7 +20,8 @@ rescue LoadError => e
exit exit
end end
LOGGER = Logger.new(STDOUT) LOGGER = Logger.new('rundeck-test.log')
#LOGGER = Logger.new(STDOUT)
EventMachine.run do EventMachine.run do
EM.error_handler do |e| EM.error_handler do |e|
...@@ -32,7 +33,7 @@ EventMachine.run do ...@@ -32,7 +33,7 @@ EventMachine.run do
noah.errback{|x| logger.error("Errback: #{x}")} noah.errback{|x| logger.error("Errback: #{x}")}
noah.callback{|y| logger.info("Callback: #{y}")} noah.callback{|y| logger.info("Callback: #{y}")}
# Passing messages...like a boss # Passing messages...like a boss
master_channel = EventMachine::Channel.new #master_channel = EventMachine::Channel.new
r = EventMachine::Hiredis::Client.connect r = EventMachine::Hiredis::Client.connect
r.errback{|x| logger.error("Unable to connect to redis: #{x}")} r.errback{|x| logger.error("Unable to connect to redis: #{x}")}
...@@ -40,11 +41,12 @@ EventMachine.run do ...@@ -40,11 +41,12 @@ EventMachine.run do
r.psubscribe("//noah/*") r.psubscribe("//noah/*")
r.on(:pmessage) do |pattern, event, message| r.on(:pmessage) do |pattern, event, message|
noah.reread_watchers if event =~ /^\/\/noah\/watcher\/.*/ noah.reread_watchers if event =~ /^\/\/noah\/watcher\/.*/
master_channel.push "#{event}|#{message}" noah.broker("#{event}|#{message}") unless noah.watchers == 0
#master_channel.push "#{event}|#{message}"
end end
sub = master_channel.subscribe {|msg| #sub = master_channel.subscribe {|msg|
# We short circuit if we have no watchers # We short circuit if we have no watchers
noah.broker(msg) unless noah.watchers == 0 # noah.broker(msg) unless noah.watchers == 0
} #}
end end
...@@ -40,7 +40,8 @@ module Noah ...@@ -40,7 +40,8 @@ module Noah
def broker(msg) def broker(msg)
e,m = msg.split("|") e,m = msg.split("|")
be = Base64.encode64(e).gsub("\n","") # 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).each do |agent, iter|
agent.send(:notify, e, m, @@watchers.clone) agent.send(:notify, e, m, @@watchers.clone)
iter.next iter.next
......
...@@ -11,9 +11,13 @@ module Noah::Agents ...@@ -11,9 +11,13 @@ module Noah::Agents
logger = LOGGER logger = LOGGER
logger.info("#{NAME}: Worker initiated") logger.info("#{NAME}: Worker initiated")
logger.debug("#{NAME}: got event - #{event}") 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)}/} matches = watch_list.find_all{|w| event =~ /^#{Base64.decode64(w)}/}
logger.debug("#{PREFIX}: Found #{matches.size} possible matches for #{event}") logger.debug("#{PREFIX}: Found #{matches.size} possible matches for #{event}")
EM::Iterator.new(matches).each do |watch, iter| EM::Iterator.new(matches, 100).each do |watch, iter|
p, ep = Base64.decode64(watch).split("|") p, ep = Base64.decode64(watch).split("|")
if ep =~ /^#{PREFIX}/ if ep =~ /^#{PREFIX}/
logger.info("#{NAME}: Sending message to (#{ep}) for pattern (#{p})") logger.info("#{NAME}: Sending message to (#{ep}) for pattern (#{p})")
......
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