Browse files

add Stopper, make the client stop as well

  • Loading branch information...
1 parent 4429ea1 commit 559d09e56855410835b1391516b40891a24f6e22 @qrush qrush committed Apr 24, 2011
Showing with 59 additions and 73 deletions.
  1. +2 −1 lib/daikon.rb
  2. +10 −10 lib/daikon/client.rb
  3. +5 −41 lib/daikon/daemon.rb
  4. +5 −11 lib/daikon/reactor.rb
  5. +13 −0 lib/daikon/stopper.rb
  6. +22 −8 spec/client_spec.rb
  7. +2 −2 spec/reactor_spec.rb
View
3 lib/daikon.rb
@@ -20,6 +20,7 @@
$LOAD_PATH.include?(File.expand_path(__DIR__))
require 'daikon/configuration'
+require 'daikon/stopper'
require 'daikon/client'
require 'daikon/reactor'
require 'daikon/daemon'
@@ -28,5 +29,5 @@
require 'daikon/daemons_hacks'
module Daikon
- VERSION = "0.8.3"
+ VERSION = "0.9.0"
end
View
20 lib/daikon/client.rb
@@ -1,5 +1,7 @@
module Daikon
class Client
+ include Stopper
+
EXCEPTIONS = [Timeout::Error,
Errno::EINVAL,
Errno::ECONNRESET,
@@ -38,16 +40,14 @@ def request(method, path, options = {})
log "#{method.to_s.upcase} #{url}"
- EventMachine.run do
- http = EventMachine::HttpRequest.new(url).send(method, options)
- http.callback do
- log "SUCCESS: #{http.response}"
- EM.stop
- end
- http.errback do
- log "ERROR: #{http.response}"
- EM.stop
- end
+ http = EventMachine::HttpRequest.new(url).send(method, options)
+ http.callback do
+ log "SUCCESS: #{http.response}"
+ stopper
+ end
+ http.errback do
+ log "ERROR: #{http.response}"
+ stopper
end
end
View
46 lib/daikon/daemon.rb
@@ -1,27 +1,7 @@
module Daikon
class Daemon
- INFO_INTERVAL = ENV["INFO_INTERVAL"] || 10
- SUMMARY_INTERVAL = ENV["SUMMARY_INTERVAL"] || 60
-
- def self.sleep_time=(sleep_time)
- @@sleep_time = sleep_time
- end
-
- def self.sleep_time
- @@sleep_time ||= 1
- end
-
- def self.run=(run)
- @@run = run
- end
-
- def self.run
- @@run
- end
-
def self.start(argv, ontop = false)
- self.run = true
- config = Daikon::Configuration.new(argv)
+ config = Configuration.new(argv)
if argv.include?("-v") || argv.include?("--version")
puts "Daikon v#{VERSION}"
@@ -35,27 +15,11 @@ def self.start(argv, ontop = false)
logger = Logger.new("/tmp/radish.log")
end
- rotated_at = Time.now
- reported_at = Time.now
- client = Daikon::Client.new
-
- client.setup(config, logger)
- client.start_monitor
-
- while self.run do
- now = Time.now
-
- if now - reported_at >= sleep_time * INFO_INTERVAL.to_i
- client.report_info
- reported_at = now
- end
-
- if now - rotated_at >= sleep_time * SUMMARY_INTERVAL.to_i
- client.rotate_monitor(rotated_at, now)
- rotated_at = now
- end
+ client = Client.new(config, logger)
+ reactor = Reactor.new(client)
- sleep sleep_time
+ EventMachine.run do
+ reactor.start
end
end
end
View
16 lib/daikon/reactor.rb
@@ -1,7 +1,9 @@
module Daikon
class Reactor
+ include Stopper
+
attr_reader :current_time
- attr_writer :callback, :info_interval
+ attr_writer :info_interval
def initialize(client = nil)
@client = client
@@ -11,22 +13,14 @@ def start
EventMachine.add_periodic_timer(info_interval) do
@current_time = Time.now
collect_info
- callback
+ stopper
end
end
def collect_info
info_collector.info do |info|
@client.report_info(info)
- callback
- end
- end
-
- private
-
- def callback
- if @callback && @callback.call(self)
- EventMachine.stop
+ stopper
end
end
View
13 lib/daikon/stopper.rb
@@ -0,0 +1,13 @@
+module Daikon
+ module Stopper
+ attr_writer :stopper
+
+ private
+
+ def stopper
+ if @stopper && @stopper.call(self)
+ EventMachine.stop
+ end
+ end
+ end
+end
View
30 spec/client_spec.rb
@@ -30,27 +30,33 @@
subject { Daikon::Client.new }
before do
+ subject.stopper = lambda { |client| EventMachine.stop }
stub_request(:any, infos_url).to_timeout
end
it "does not kill the client" do
- lambda {
- subject.report_info({})
- }.should_not raise_error
+ em do
+ lambda {
+ subject.report_info({})
+ }.should_not raise_error
+ end
end
end
describe Daikon::Client, "when it returns bad json" do
subject { Daikon::Client.new }
before do
+ subject.stopper = lambda { |client| EventMachine.stop }
stub_request(:post, infos_url).to_return(:body => "{'bad':'json}")
end
it "does not commit suicide" do
- lambda {
- subject.report_info({})
- }.should_not raise_error
+ em do
+ lambda {
+ subject.report_info({})
+ }.should_not raise_error
+ end
end
end
@@ -62,6 +68,10 @@
"Content-Type" => "application/json"
}
+ em do
+ subject.rotate_monitor(DateTime.parse(past), DateTime.parse(now))
+ end
+
WebMock.should have_requested(:post, summaries_url(server)).
with(:body => payload.to_json, :headers => headers)
end
@@ -84,8 +94,8 @@
before do
Timecop.freeze DateTime.parse(now)
Daikon::Monitor.stubs(:pop).yields(data)
+ subject.stopper = lambda { |client| EventMachine.stop }
stub_request(:post, summaries_url(server)).to_return(:status => 200)
- subject.rotate_monitor(DateTime.parse(past), DateTime.parse(now))
end
after do
@@ -117,6 +127,10 @@
"Content-Type" => "application/json"
}
+ em do
+ subject.report_info(info)
+ end
+
WebMock.should have_requested(:post, infos_url(server)).
with(:body => info.to_json, :headers => headers)
end
@@ -127,8 +141,8 @@
let(:info) { {"connected_clients"=>"1", "used_cpu_sys_childrens"=>"0.00"} }
before do
+ subject.stopper = lambda { |client| EventMachine.stop }
stub_request(:post, infos_url(server)).to_return(:status => 200)
- subject.report_info(info)
end
context "with default configuration" do
View
4 spec/reactor_spec.rb
@@ -11,7 +11,7 @@
it "collects info once per interval" do
now = Time.now
- subject.callback = lambda { |reactor| reactor.current_time.to_f >= now.to_f + 1.1 }
+ subject.stopper = lambda { |reactor| reactor.current_time.to_f >= now.to_f + 1.1 }
em do
subject.start
@@ -37,7 +37,7 @@
redis_mock(replies) do
em do
subject.collect_info
- subject.callback = lambda do |reactor|
+ subject.stopper = lambda do |reactor|
client.should have_received(:report_info).with(:total_commands_processed => "1")
end
end

0 comments on commit 559d09e

Please sign in to comment.