Class: Amazon::WebServices::MTurk::MechanicalTurkErrorHandler
- Inherits:
-
Object
- Object
- Amazon::WebServices::MTurk::MechanicalTurkErrorHandler
- Includes:
- Util::Logging
- Defined in:
- lib/amazon/webservices/mturk/mechanical_turk_error_handler.rb
Constant Summary collapse
- REQUIRED_PARAMETERS =
[:Relay]
- RETRY_PRE =
Commands with these prefixes can be retried if we are unsure of success
%w( search get register update disable assign set dispose )
- MAX_RETRY =
Max number of times to retry a call
6
- BACKOFF_BASE =
Base used in Exponential Backoff retry delay
2
- BACKOFF_INITIAL =
Scale factor for Exponential Backoff retry delay
0.1
- RESULT_PATTERN =
Matching pattern to find a ‘Results’ element in the Response
/Result/
- ACCEPTABLE_RESULTS =
Additional elements to be considered a ‘Result’ despite not matching RESULT_PATTERN
%w( HIT Qualification QualificationType QualificationRequest Information )
Instance Method Summary collapse
- #canRetry(try) ⇒ Object
- #dispatch(method, *args) ⇒ Object
- #doBackoff(try) ⇒ Object
- #handleError(error, method) ⇒ Object
-
#initialize(args) ⇒ MechanicalTurkErrorHandler
constructor
A new instance of MechanicalTurkErrorHandler.
- #isResultTag(tag) ⇒ Object
- #methodRetryable(method) ⇒ Object
- #validateResponse(response) ⇒ Object
Methods included from Util::Logging
Constructor Details
#initialize(args) ⇒ MechanicalTurkErrorHandler
Returns a new instance of MechanicalTurkErrorHandler.
33 34 35 36 37 |
# File 'lib/amazon/webservices/mturk/mechanical_turk_error_handler.rb', line 33 def initialize( args ) missing_parameters = REQUIRED_PARAMETERS - args.keys raise "Missing paramters: #{missing_parameters.join(',')}" unless missing_parameters.empty? @relay = args[:Relay] end |
Instance Method Details
#canRetry(try) ⇒ Object
107 108 109 |
# File 'lib/amazon/webservices/mturk/mechanical_turk_error_handler.rb', line 107 def canRetry( try ) try <= MAX_RETRY end |
#dispatch(method, *args) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/amazon/webservices/mturk/mechanical_turk_error_handler.rb', line 39 def dispatch(method, *args) try = 0 begin try += 1 log "Dispatching call to #{method} (try #{try})" response = @relay.send(method,*args) validateResponse( response ) return response rescue Exception => error case handleError( error,method ) when :RetryWithBackoff retry if doBackoff( try ) when :RetryImmediate retry if canRetry( try ) when :Ignore return :IgnoredError => error when :Unknown raise Util::UnknownResultException.new( error, method, args ) when :Fail raise error else raise "Unknown error handling method: #{handleError( error,method )}" end raise error end end |
#doBackoff(try) ⇒ Object
111 112 113 114 115 116 |
# File 'lib/amazon/webservices/mturk/mechanical_turk_error_handler.rb', line 111 def doBackoff( try ) return false unless canRetry(try) delay = BACKOFF_INITIAL * ( BACKOFF_BASE ** try ) sleep delay return true end |
#handleError(error, method) ⇒ Object
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 101 102 103 104 105 |
# File 'lib/amazon/webservices/mturk/mechanical_turk_error_handler.rb', line 73 def handleError( error, method ) log "Handling error: #{error.inspect}" case error.class.to_s when 'Timeout::Error','SOAP::HTTPStreamError' if methodRetryable( method ) return :RetryImmediate else return :Unknown end when 'SOAP::FaultError' case error.faultcode.data when "aws:Server.ServiceUnavailable" return :RetryWithBackoff else return :Unknown end when 'Amazon::WebServices::Util::ValidationException' case error. when 'AWS.ServiceUnavailable' return :RetryWithBackoff if methodRetryable( method ) end return :Fail when 'RuntimeError' case error. when 'Throttled' return :RetryWithBackoff else return :RetryImmediate end else return :Unknown end end |
#isResultTag(tag) ⇒ Object
118 119 120 |
# File 'lib/amazon/webservices/mturk/mechanical_turk_error_handler.rb', line 118 def isResultTag( tag ) tag.to_s =~ RESULT_PATTERN or ACCEPTABLE_RESULTS.include?( tag.to_s ) end |
#methodRetryable(method) ⇒ Object
66 67 68 69 70 71 |
# File 'lib/amazon/webservices/mturk/mechanical_turk_error_handler.rb', line 66 def methodRetryable( method ) RETRY_PRE.each do |pre| return true if method.to_s =~ /^#{pre}/i end return false end |
#validateResponse(response) ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/amazon/webservices/mturk/mechanical_turk_error_handler.rb', line 122 def validateResponse(response) log "Validating response: #{response.inspect}" if response[:Errors] and response[:Errors][:Error] if response[:Errors][:Error][:Code] == "ServiceUnavailable" raise 'Throttled' else raise Util::ValidationException.new(response) end end if response[:OperationRequest] and response[:OperationRequest][:Errors] raise Util::ValidationException.new(response) end resultTags = response.keys.find_all {|r| isResultTag( r ) } raise Util::ValidationException.new(response, "Didn't get back an acceptable result tag (got back #{response.keys.join(',')})") if resultTags.empty? resultTags.each do |resultTag| log "using result tag <#{resultTag}>" result = response[resultTag] if result[:Request] and result[:Request][:Errors] raise Util::ValidationException.new(result) end end response end |