lib/updateagent/updateagent.rb in onebody-updateagent-0.4.2 vs lib/updateagent/updateagent.rb in onebody-updateagent-0.4.3

- old
+ new

@@ -1,14 +1,12 @@ # general class to handle comparing and pushing data to the remote end class UpdateAgent - MAX_HASHES_AT_A_TIME = 100 + MAX_HASHES_AT_A_TIME = 500 MAX_TO_BATCH_AT_A_TIME = 10 SLEEP_PERIOD = 3 - DEBUG = false - def initialize(data=nil, options={}) @options = options @attributes = [] @data = [] @create = [] @@ -64,10 +62,12 @@ else value = true end elsif INTEGER_ATTRIBUTES.include?(key) value = value.to_s != '' ? value.scan(/\d/).join.to_i : nil + else + value = value.to_s.gsub(/\r\n|\n|\r/, ' ') end hash[key] = value end record_count += 1 print "reading record #{record_count}\r" @@ -109,11 +109,11 @@ puts end def present_record(row, new=false) puts "#{row['legacy_id'].to_s.ljust(10)} #{name_for(row).to_s.ljust(40)} #{new ? '(new)' : ' '}" - if DEBUG + if @options['debug'] puts "Local values: #{row.values_for_hash(@attributes).join}" puts "Remote values: #{row['remote_hash']}" end end @@ -176,18 +176,16 @@ @sync = Sync.create(:complete => false, :started_at => Time.now) end def finish_sync(create_items=true, mark_complete=true) if create_items - @sync.post( - :create_items, - {}, - ( - @create.map { |r| to_sync_item(r, 'create') } + - @update.map { |r| to_sync_item(r, 'update') } - ).to_xml - ) + items = @create.map { |r| to_sync_item(r, 'create') } + + @update.map { |r| to_sync_item(r, 'update') } + items.each_slice(100) do |items| + @sync.post(:create_items, {}, items.to_xml) + sleep SLEEP_PERIOD + end end if mark_complete @sync.complete = true @sync.finished_at = Time.now @sync.error_count = errors.length @@ -228,25 +226,28 @@ protected # ask remote end for value hashe for each record (50 at a time) # mark records to create or update based on response def compare_hashes(ids, force=false) + index = 0 + print "#{resource.name} 0/#{ids.length}\r"; STDOUT.flush ids.each_slice(MAX_HASHES_AT_A_TIME) do |some_ids| - print '.'; STDOUT.flush options = {:attrs => @attributes.join(','), :legacy_id => some_ids.join(',')} - options.merge!(:debug => true) if DEBUG + options.merge!(:debug => true) if @options['debug'] response = resource.post(:hashify, {}, options.to_xml) hashes = Hash.from_xml(response.body)['records'].to_a hashes.each do |record| row = data_by_id[record['legacy_id'].to_i] - if DEBUG + if @options['debug'] row['remote_hash'] = record['hash'] @update << row if force or row.values_for_hash(@attributes).join != record['hash'] or (resource.name == 'Person' and record['family_id'].nil?) else @update << row if force or row.values_hash(@attributes) != record['hash'] or (resource.name == 'Person' and record['family_id'].nil?) end end @create += some_ids.reject { |id| hashes.map { |h| h['legacy_id'].to_i }.include?(id.to_i) }.map { |id| data_by_id[id.to_i] } + index += some_ids.length + print "#{resource.name} #{index}/#{ids.length}\r"; STDOUT.flush sleep SLEEP_PERIOD end puts end end