add timeout to pet load

This commit is contained in:
Emi Matchu 2012-08-11 18:47:25 -04:00
parent 412c401c5f
commit 7dfc6d81a2
3 changed files with 30 additions and 28 deletions

View file

@ -19,7 +19,8 @@ class Pet < ActiveRecord::Base
def load! def load!
require 'ostruct' require 'ostruct'
begin begin
envelope = Pet.amf_service.fetch(PET_VIEWER_METHOD, name, nil) envelope = Pet.amf_service.request(PET_VIEWER_METHOD, name, nil).
fetch(:timeout => 2)
rescue RocketAMF::RemoteGateway::AMFError => e rescue RocketAMF::RemoteGateway::AMFError => e
if e.message == PET_NOT_FOUND_REMOTE_ERROR if e.message == PET_NOT_FOUND_REMOTE_ERROR
raise PetNotFound, "Pet #{name.inspect} does not exist" raise PetNotFound, "Pet #{name.inspect} does not exist"

View file

@ -9,43 +9,44 @@ module RocketAMF
@params = params @params = params
end end
def fetch def fetch(options={})
uri = @service.gateway.uri uri = @service.gateway.uri
data = envelope.serialize data = envelope.serialize
# TODO: re-enable if we go back to using threads
if false && defined?(EventMachine) && EventMachine.respond_to?(:reactor_running?) && EventMachine.reactor_running? req = Net::HTTP::Post.new(uri.path)
req = EM::HttpRequest.new(uri).post :body => data req.body = data
response_body = req.response
else begin
req = Net::HTTP::Post.new(uri.path) http = Net::HTTP.new(uri.host, uri.port)
req.body = data http.read_timeout = options[:timeout] if options[:timeout]
begin res = http.request(req)
res = Net::HTTP.new(uri.host, uri.port).start { |http| http.request(req) } rescue Exception => e
rescue Exception => e raise ConnectionError, e.message
raise ConnectionError, e.message
end
case res
when Net::HTTPSuccess
response_body = res.body
else
error = nil
begin
res.error!
rescue Exception => scoped_error
error = scoped_error
end
raise ConnectionError, error.message
end
end end
if res.is_a?(Net::HTTPSuccess)
response_body = res.body
else
error = nil
begin
res.error!
rescue Exception => scoped_error
error = scoped_error
end
raise ConnectionError, error.message
end
begin begin
result = RocketAMF::Envelope.new.populate_from_stream(response_body) result = RocketAMF::Envelope.new.populate_from_stream(response_body)
rescue Exception => e rescue Exception => e
raise ConnectionError, e.message raise ConnectionError, e.message
end end
first_message_data = result.messages[0].data first_message_data = result.messages[0].data
if first_message_data.respond_to?(:[]) && first_message_data[:code] == ERROR_CODE if first_message_data.respond_to?(:[]) && first_message_data[:code] == ERROR_CODE
raise AMFError.new(first_message_data) raise AMFError.new(first_message_data)
end end
result result
end end

View file

@ -8,8 +8,8 @@ module RocketAMF
@name = name @name = name
end end
def fetch(method, *params) def request(method, *params)
Request.new(self, method, *params).fetch Request.new(self, method, *params)
end end
end end
end end