Class: HerokuDroid

Inherits:
Droid
  • Object
show all
Defined in:
lib/droid/heroku.rb

Constant Summary

Constants inherited from Droid

Droid::DEFAULT_TTL

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Droid

bunny, call, closing?, default_config, gen_queue, handle_error, log, #log, log=, name, name=, pop, publish, #publish, publish_to_ex, publish_to_q, reconnect_on_error, reply_to_q, reset_bunny, start, stop_safe, version, wait_for_tcp_port

Methods included from Droid::EMTimerUtils

included, #periodic_timer, #timer

Methods included from Droid::BackwardsCompatibleMethods

#listen4

Methods included from Droid::QueueMethods

#listener, #worker

Constructor Details

#initialize(name, opts = {}, &blk) ⇒ HerokuDroid

Returns a new instance of HerokuDroid.



29
30
31
32
33
34
35
36
37
# File 'lib/droid/heroku.rb', line 29

def initialize(name, opts={}, &blk)
	@extended_stats = !(opts[:extended_stats] == false)

	super(name, opts) do |droid|
		setup_standard_topics(self) unless opts[:standard_topics] == false
		LocalStats.attach
		blk.call(self)
	end
end

Instance Attribute Details

#extended_statsObject (readonly)

Returns the value of attribute extended_stats.



27
28
29
# File 'lib/droid/heroku.rb', line 27

def extended_stats
  @extended_stats
end

Instance Method Details

#call_statsObject



46
47
48
# File 'lib/droid/heroku.rb', line 46

def call_stats
	@stats ? @stats.call : nil
end

#nameObject



50
51
52
# File 'lib/droid/heroku.rb', line 50

def name
	Droid.name
end

#ruby_pathObject



54
55
56
57
58
59
60
# File 'lib/droid/heroku.rb', line 54

def ruby_path
	if File.exists?("/usr/ruby1.8.6/bin/ruby")
		"/usr/ruby1.8.6/bin"
	else
		"/usr/local/bin"
	end
end

#setup_ping_topic(droid) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/droid/heroku.rb', line 66

def setup_ping_topic(droid)
	require 'time'

	droid.listener('ping').subscribe do |req|
		Droid::Utilization.latency = (Time.now.to_f - req['departed_at']).abs
	end

	blk = Proc.new do |d|
		begin
			t1 = Time.now
			response = {}.merge(LocalStats.stats)

			estats = nil
			if self.extended_stats
				estats = droid.call_stats
				estats = { :notes => estats } unless estats.kind_of?(Hash)
				estats[:notes] ||= estats.map { |k, v| "#{v} #{k}" }.join(", ")
				estats.merge!(LocalStats.extended_stats)
			end

			response.merge!({
				:extended_stats => estats,
				:droid_name => Droid.name,
				:latency => Droid::Utilization.latency,
			})

			d.publish('pong', response.merge(:stat_collection => (Time.now - t1)))
		rescue Object => e
			log.error "Ping Block Error: #{e.class} -> #{e.message}", :exception => e
		end
	end

	EM.add_timer(2) { blk.call(droid) }
	EM.add_periodic_timer(50 + (rand*15).to_i) { blk.call(droid) }
end

#setup_standard_topics(droid) ⇒ Object



62
63
64
# File 'lib/droid/heroku.rb', line 62

def setup_standard_topics(droid)
	setup_ping_topic(droid)
end

#stats(&blk) ⇒ Object



39
40
41
42
43
44
# File 'lib/droid/heroku.rb', line 39

def stats(&blk)
	@stats = blk
	out = call_stats
	out = out.inspect unless out.kind_of?(String)
	Log.notice out
end