noah 0.3 → 0.4

Sign up to get free protection for your applications and to get access to all the features.
data/bin/noah-watcher.rb CHANGED
@@ -28,17 +28,16 @@ EventMachine.run do
28
28
  EM.error_handler do |e|
29
29
  LOGGER.warn(e)
30
30
  end
31
- logger = LOGGER
32
- trap("INT") { logger.debug("Shutting down. Watches will not be fired");EM.stop }
31
+ trap("INT") { LOGGER.debug("Shutting down. Watches will not be fired");EM.stop }
33
32
  noah = Noah::Agent.new
34
- noah.errback{|x| logger.error("Errback: #{x}")}
35
- noah.callback{|y| logger.info("Callback: #{y}")}
33
+ noah.errback{|x| LOGGER.error("Errback: #{x}")}
34
+ noah.callback{|y| LOGGER.info("Callback: #{y}")}
36
35
  # Passing messages...like a boss
37
36
  #master_channel = EventMachine::Channel.new
38
37
 
39
38
  r = EventMachine::Hiredis::Client.connect
40
- r.errback{|x| logger.error("Unable to connect to redis: #{x}")}
41
- logger.debug("Starting up")
39
+ r.errback{|x| LOGGER.error("Unable to connect to redis: #{x}")}
40
+ LOGGER.info("Attaching to Redis Pubsub")
42
41
  r.psubscribe("*")
43
42
  r.on(:pmessage) do |pattern, event, message|
44
43
  noah.reread_watchers if event =~ /^\/\/noah\/watchers\/.*/
data/examples/Kirkfile ADDED
@@ -0,0 +1,4 @@
1
+ log :level => :all
2
+ rack "/home/jvincent/development/noah/examples/lb.ru" do
3
+ listen 3000
4
+ end
data/examples/lb.ru ADDED
@@ -0,0 +1,4 @@
1
+ require 'sinatra'
2
+ require './sinatra-load-test-endpoint.rb'
3
+ app = NoahPostDemo
4
+ app.run
@@ -0,0 +1,16 @@
1
+ require 'sinatra/base'
2
+
3
+ class NoahPostDemo < Sinatra::Base
4
+ configure do
5
+ set :app_file, __FILE__
6
+ set :server, %w[thin Kirk]
7
+ set :logging, true
8
+ set :run, true
9
+ set :port, 3000
10
+ end
11
+
12
+ post '/*' do
13
+ x = request.body.read
14
+ end
15
+
16
+ end
data/lib/noah/agent.rb CHANGED
@@ -27,9 +27,10 @@ module Noah
27
27
  @logger.debug("Initializing with #{@@watchers.size} registered watches")
28
28
  @logger.debug("#{@@agents} agents registered")
29
29
  if EventMachine.reactor_running?
30
- self.succeed("Succeed callback")
30
+ #instantiate_agents!
31
+ @logger.info("Started up!")
31
32
  else
32
- logger.fatal("Must be inside a reactor!")
33
+ @logger.fatal("Must be inside a reactor!")
33
34
  end
34
35
  end
35
36
 
@@ -37,12 +38,8 @@ module Noah
37
38
  @@watchers.size
38
39
  end
39
40
 
40
- def http_worker
41
- @http_worker
42
- end
43
-
44
41
  def reread_watchers
45
- @logger.debug("Found new watches")
42
+ @logger.info("Found new watches")
46
43
  @logger.debug("Current watch count: #{@@watchers.size}")
47
44
  @@watchers = Noah::Watcher.watch_list
48
45
  @logger.debug("New watch count: #{@@watchers.size}")
@@ -50,15 +47,32 @@ module Noah
50
47
 
51
48
  def broker(msg)
52
49
  e,m = msg.split("|")
53
- # Below isn't being used right now
54
- #be = Base64.encode64(e).gsub("\n","")
55
50
  EM::Iterator.new(@@agents, @@agents.size).each do |agent, iter|
56
- agent.send(:notify, e, m, @@watchers.clone)
57
- iter.next
51
+ #a = agent.to_s.gsub(/::/,'_').downcase
52
+ x = agent.send(:new)
53
+ begin
54
+ #self.instance_variable_get("@#{a}").send(:notify, e, m, @@watchers)
55
+ x.notify(e, m, @@watchers.clone)
56
+ iter.next
57
+ rescue Exception => e
58
+ @logger.error("#{agent.to_s} invocation failed with #{e.message}")
59
+ end
58
60
  end
59
61
  end
60
62
 
61
- private
63
+ protected
64
+ def instantiate_agents!
65
+ @@agents.each do |agent|
66
+ # Convert Noah::Agents::HttpAgent to
67
+ # noah_agents_httpagent
68
+ a = agent.to_s.gsub(/::/,'_').downcase
69
+ @logger.debug("#{a}")
70
+ # Create instance variable of a
71
+ self.class.send :attr_accessor, a.to_sym
72
+ # Set the instance variable "a" to instance of agent
73
+ self.instance_variable_set(:"@#{a}", agent.send(:new))
74
+ end
75
+ end
62
76
  def find_and_register_agents
63
77
  candidates = []
64
78
  Gem.source_index.find_all {|g| candidates << g[1].name if g[1].name =~ /^noah-agent-.*/}
@@ -1,41 +1,38 @@
1
1
  module Noah::Agents
2
- module Base
3
- class <<self
4
- PREFIX = "base"
5
- NAME = "base-agent"
6
- end
2
+ class Base
3
+
4
+ PREFIX = "base"
5
+ NAME = "base-agent"
6
+ DEFAULT_CONCURRENCY = 1
7
7
 
8
- def self.included(base)
8
+ def self.inherited(base)
9
9
  Noah::Watchers.register_agent(base)
10
10
  base.send :include, EM::Deferrable
11
- base.send :extend, AgentClassMethods
12
11
  end
13
- end
14
-
15
- module AgentClassMethods
16
12
 
17
- def logger
18
- Noah::Log.logger.progname = self.name
19
- Noah::Log.logger
20
- end
21
-
22
- def find_watched_patterns!(watchlist)
23
- watched_patterns = []
24
- watchlist.find_all do |w|
25
- p, ep = Base64.decode64(w).split('|')
26
- watched_patterns << "#{p}|#{ep}" if ep =~ /^#{self.const_get("PREFIX")}/
13
+ def notify(event, message, watch_list)
14
+ logger.info("#{self.class} worker initiated")
15
+ worklist = []
16
+ watch_list.select{|w| worklist << w[:endpoint] if (w[:endpoint] =~ /^#{self.class::PREFIX}/ && event =~ /^#{w[:pattern]}/) }
17
+ if worklist.size >= 1
18
+ logger.info("Dispatching message to #{worklist.size} #{self.class.to_s} endpoints")
19
+ EM::Iterator.new(worklist, self.class::DEFAULT_CONCURRENCY).each do |ep, iter|
20
+ work!(ep, message)
21
+ iter.next
22
+ end
23
+ logger.info("Dispatched message to #{worklist.size} #{self.class.to_s} endpoints")
24
+ else
25
+ logger.info("No work to do")
27
26
  end
28
- watched_patterns
27
+ rescue Exception => e
28
+ logger.fatal("Exectution of notify failed with #{e.message}")
29
29
  end
30
30
 
31
- def notify(event, message, watch_list)
32
- logger.info("Worker Initiated")
33
- logger.debug("got event - #{event}")
34
- watched_patterns = find_watched_patterns!(watch_list)
35
- matches = watched_patterns.find_all {|w| event =~ /^#{w}/}
36
- logger.debug("Found #{matches.size} matches for #{event}")
37
- self.callback!(matches, message)
31
+ def logger
32
+ Noah::Log.logger.progname = self.class.to_s
33
+ Noah::Log.logger
38
34
  end
39
35
 
40
36
  end
37
+
41
38
  end
@@ -1,19 +1,15 @@
1
1
  require File.join(File.dirname(__FILE__), 'base_agent')
2
2
 
3
3
  module Noah::Agents
4
- class DummyAgent
5
- include Noah::Agents::Base
4
+ class DummyAgent < Base
6
5
 
7
6
  PREFIX = "dummy://"
8
- NAME = self.name
7
+ NAME = self.class.to_s
8
+ DEFAULT_CONCURRENCY = 10
9
9
 
10
- def self.callback!(matches, message)
11
- EM::Iterator.new(matches).each do |watch, iter|
12
- p, ep = watch.split("|")
13
- logger.info("Sending message to: #{ep} for pattern: #{p}")
14
- logger.debug("message received: #{message}")
15
- iter.next
16
- end
10
+ def work!(ep, message)
11
+ logger.info("Sending message to: #{ep}")
12
+ logger.info("Dummy message received: #{message}")
17
13
  end
18
14
 
19
15
  end
@@ -1,25 +1,21 @@
1
1
  require File.join(File.dirname(__FILE__), 'base_agent')
2
2
 
3
3
  module Noah::Agents
4
- class HttpAgent
5
- include Noah::Agents::Base
4
+ class HttpAgent < Base
6
5
 
7
6
  PREFIX = "http://"
8
- NAME = self.name
7
+ NAME = self.class.to_s
8
+ DEFAULT_CONCURRENCY = 500
9
9
 
10
- def self.callback!(matches, message)
11
- EM::Iterator.new(matches, 100).each do |watch, iter|
12
- p, ep = watch.split("|")
13
- logger.info("Sending message to (#{ep}) for pattern (#{p})")
14
- http = EM::HttpRequest.new(ep, :connection_timeout => 2, :inactivity_timeout => 4).post :body => message
10
+ def work!(ep, message)
11
+ logger.info("Sending message to (#{ep})")
12
+ http = EM::HttpRequest.new(ep, :connection_timeout => 2, :inactivity_timeout => 2).post :body => message
15
13
  http.callback {
16
14
  logger.info("Message posted to #{ep} successfully")
17
15
  }
18
16
  http.errback {
19
17
  logger.error("Something went wrong with #{ep}")
20
18
  }
21
- iter.next
22
- end
23
19
  end
24
20
 
25
21
  end
@@ -37,7 +37,7 @@ module Noah
37
37
  def watch_list
38
38
  arr = []
39
39
  watches = self.all.sort_by(:pattern)
40
- watches.each {|w| arr << w.name}
40
+ watches.each {|w| arr << w.to_hash}
41
41
  arr
42
42
  end
43
43
  end
data/lib/noah/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Noah
2
- VERSION = "0.3"
2
+ VERSION = "0.4"
3
3
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: noah
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: "0.3"
5
+ version: "0.4"
6
6
  platform: ruby
7
7
  authors:
8
8
  - John E. Vincent
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-03-25 00:00:00 -04:00
13
+ date: 2011-03-27 00:00:00 -04:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -246,6 +246,7 @@ files:
246
246
  - bin/noah-watcher.rb
247
247
  - config.ru
248
248
  - config/warble.rb
249
+ - examples/Kirkfile
249
250
  - examples/README.md
250
251
  - examples/cluster.ru
251
252
  - examples/custom-watcher.rb
@@ -256,10 +257,12 @@ files:
256
257
  - examples/js/WebSocketMain.swf
257
258
  - examples/js/swfobject.js
258
259
  - examples/js/web_socket.js
260
+ - examples/lb.ru
259
261
  - examples/logger.rb
260
262
  - examples/reconfiguring-sinatra-watcher.rb
261
263
  - examples/reconfiguring-sinatra.rb
262
264
  - examples/simple-post.rb
265
+ - examples/sinatra-load-test-endpoint.rb
263
266
  - examples/websocket.html
264
267
  - examples/websocket.rb
265
268
  - lib/noah.rb