lib/dumper/agent.rb in dumper-0.0.7 vs lib/dumper/agent.rb in dumper-0.1.0
- old
+ new
@@ -29,34 +29,34 @@
@app_name = ObjectSpace.each_object(Rails::Application).first.class.name.split("::").first
logger.level = stdout_logger.level = options[:loglevel] if options[:loglevel]
end
def start
- return unless @app_key and @stack.supported?
+ return unless @stack.supported?
log "stack: dispatcher = #{@stack.dispatcher}, framework = #{@stack.framework}, rackup = #{@stack.rackup}"
@loop_thread = Thread.new { start_loop }
@loop_thread[:name] = 'Loop Thread'
end
def start_loop
sec = 1
begin
sec *= 2
- log "sleeping #{sec} seconds for agent/register.", :debug
+ log "sleeping #{sec} seconds for agent/register", :debug
sleep sec
- json = send_request(api: 'agent/register', json: MultiJson.encode(register_hash))
+ json = api_request('agent/register', :json => MultiJson.dump(register_hash))
end until json[:status]
return log("agent stopped: #{json.to_s}") if json[:status] == 'error'
@token = json[:token]
log "agent started as #{@token ? 'primary' : 'secondary'}"
sleep 1.hour unless @token
loop do
- json = send_request(api: 'agent/poll', params: { token: @token })
+ json = api_request('agent/poll', :params => { :token => @token })
if json[:status] == 'ok'
# Promoted or demoted?
if json[:token]
@token = json[:token] # promote
@@ -74,26 +74,25 @@
Dumper::Job.new(self, json[:job]).run_and_exit
end
end
end
- sleep @token ? 60.seconds : 1.hour
+ sleep [ json[:interval].to_i, 60 ].max
end
end
def register_hash
{
- # :pid => Process.pid,
- # :host => Socket.gethostname,
+ :hostname => Socket.gethostname,
:agent_version => Dumper::VERSION,
:app_name => @app_name,
:stack => @stack.to_hash,
}
end
- def send_request(options)
- uri = URI.parse("#{@api_base}/api/#{options[:api]}")
+ def api_request(method_name, options)
+ uri = URI.parse("#{@api_base}/api/#{method_name}")
http = Net::HTTP.new(uri.host, uri.port)
if uri.is_a? URI::HTTPS
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
end
@@ -111,12 +110,12 @@
end
response = http.request(request)
if response.code == '200'
log response.body, :debug
- MultiJson.decode(response.body).with_indifferent_access
+ MultiJson.load(response.body).with_indifferent_access
else
- log "******** ERROR on api: #{options[:api]}, resp code: #{response.code} ********", :error
+ log "******** ERROR on api: #{method_name}, resp code: #{response.code} ********", :error
{} # return empty hash
end
rescue
log_last_error
{} # return empty hash