lib/govdelivery/proctor/base.rb in govdelivery-proctor-1.3 vs lib/govdelivery/proctor/base.rb in govdelivery-proctor-1.3.1
- old
+ new
@@ -1,68 +1,71 @@
module GovDelivery::Proctor
class CheckTimeExceeded < StandardError; end
- def self.backoff_check(limit, desc = 'Backoff check', inc = 1)
+ def self.backoff_check(limit, desc = 'Backoff check', inc = 20)
raise 'Check requires a block' unless block_given?
check(limit, 'backoff', desc, inc) do
yield
end
end
# Sometimes you want to reverse backoff because as time
# passes it is more likely your condition is true
- def self.accelerating_check(limit, desc = 'Backon check', inc = 1)
+ def self.accelerating_check(limit, desc = 'Accelerating check', inc = 20)
raise 'Check requires a block' unless block_given?
check(limit, 'accelerate', desc, inc) do
yield
end
end
- def self.steady_check(limit, desc = 'Steady check', inc = 1)
+ def self.steady_check(limit, desc = 'Steady check', inc = 10)
raise 'Check requires a block' unless block_given?
check(limit, 'steady', desc, inc) do
yield
end
end
# if backoff, double the wait time each time
- # if accelerating, ramp up after long pause
+ # if accelerate, halve the wait time each time based on limit
+ # and use increment only for minimum wait time
# if steady, just keep it steady
+ # to_i used to make ActiveSupport::Durations safe
def self.sleep_time(check_type, limit, increment, iteration)
- case check_type
- when 'steady'
- increment
- when 'backoff'
- (2**iteration) * increment
- when 'accelerate'
- val = Math.log(limit, iteration + 1)
- val = val.to_f.infinite? ? limit / increment : val
- val = val.to_f.nan? ? 1 : val.round
- [increment, val * increment].max
- else
- raise "Check type #{check_type} invalid: choose backon, accelerate or steady."
- end
+ limit = limit.to_i
+ increment = increment.to_i
+ sleep_time = case check_type
+ when 'steady'
+ increment
+ when 'backoff'
+ (2**iteration) * increment
+ when 'accelerate'
+ [(limit / 2) / (iteration + 1), increment].max
+ else
+ raise "Check type #{check_type} invalid: choose accelerate, backoff, or steady."
+ end
+ [sleep_time, limit].min
end
def self.check(limit, check_type, desc, inc)
setup
slept_time = 0
x = 0
response = nil
Kernel.loop do
- sleep_time = sleep_time(check_type, limit, inc, ++x)
+ sleep_time = sleep_time(check_type, limit, inc, x)
+ getStandardLogger.info("sleeping for #{sleep_time} seconds")
sleep(sleep_time)
slept_time += sleep_time
response = yield
break if response
if slept_time >= limit
fail_message = "#{desc} has taken too long. Have waited #{slept_time} seconds\nlog: #{@backBuffer.string}"
raise CheckTimeExceeded, fail_message
end
+ x += 1
# We're doing this twice, once for standard out so you can see it's still
# working, and then again inside the logged output if it fails.
-
getStandardLogger.info("Still waiting while #{desc}, current time=#{slept_time}")
log.info("Still waiting while #{desc}, current time=#{slept_time}")
end
teardown
response