lib/eco/api/session/batch.rb in eco-helpers-2.0.16 vs lib/eco/api/session/batch.rb in eco-helpers-2.0.17

- old
+ new

@@ -112,21 +112,37 @@ msg += " with #{slice.length} entries of #{data.length} -- #{done} done" msg += " (last: #{str_stats(start_slice, slice.length)}; total: #{str_stats(start_time, done)})" logger.info(msg) unless silent start_slice = Time.now - people_api.batch do |batch| - slice.each do |person| - batch.public_send(method, person) do |response| - faltal("Request with no response") unless !!response - status[person] = response + offer_retry_on(Ecoportal::API::Errors::TimeOut) do + people_api.batch do |batch| + slice.each do |person| + batch.public_send(method, person) do |response| + faltal("Request with no response") unless !!response + status[person] = response + end end - end - end # next batch + end # end batch + end iteration += 1 done += slice.length end # next slice + end + end + + def offer_retry_on(error_type, retries_left = 3, &block) + begin + block.call + rescue error_type => e + raise unless retries_left > 0 + print "Batch TimeOut. You have #{retries_left} retries left. Do you want to retry (y/N)? " + if (res = STDIN.gets.chomp) && res[0].downcase == "y" + offer_retry_on(error_type, retries_left - 1, &block) + else + raise + end end end def str_stats(start, count) now = Time.now