lib/updateagent/updateagent.rb in onebody-updateagent-0.6.5 vs lib/updateagent/updateagent.rb in onebody-updateagent-0.6.6
- old
+ new
@@ -2,13 +2,16 @@
class UpdateAgent
MAX_HASHES_AT_A_TIME = 500
MAX_TO_BATCH_AT_A_TIME = 10
SLEEP_PERIOD = 3
+ RETRIES = 5
+ RETRY_SLEEP = 15
def initialize(data=nil, options={})
@options = options
+ @options['debug'] = true
@attributes = []
@data = []
@create = []
@update = []
if data
@@ -125,25 +128,29 @@
# use ActiveResource to create/update records on remote end
def push
@errors = []
puts 'Updating remote end...'
index = 0
- retries = 10
print "#{resource.name} 0/0\r"; STDOUT.flush
(@create + @update).each_slice(MAX_TO_BATCH_AT_A_TIME) do |records|
- begin
- xml = {:options => @options['remote_options'], :records => records}.to_xml
- response = resource.post(:batch, {}, xml)
- rescue ActiveResource::TimeoutError
- retries -= 1
- if retries > 0
- puts "ActiveResource::TimeoutError with xml:\n#{xml}\n\nwill retry #{retries} more time(s) after 10 seconds..."
- sleep 10
- retry
+ post_succeeded = false
+ response = nil
+ RETRIES.times do |i|
+ begin
+ xml = {:options => @options['remote_options'], :records => records}.to_xml
+ response = resource.post(:batch, {}, xml)
+ rescue ActiveResource::TimeoutError, ActiveResource::ServerError
+ puts "ActiveResource::TimeoutError with xml:\n#{xml}\n\n"
+ if i < RETRIES-1
+ puts "waiting for #{RETRY_SLEEP} seconds to retry..."
+ sleep RETRY_SLEEP
+ end
else
- raise
+ post_succeeded = true
end
+ break if post_succeeded
end
+ exit(1) unless post_succeeded
statuses = Hash.from_xml(response.body)['objects']
statuses.each do |status|
record = data_by_id[status['legacy_id'].to_i]
record['id'] = status['id']
record['name'] = status['name']