Class: UpdateAgent

Inherits:
Object
  • Object
show all
Defined in:
lib/updateagent/updateagent.rb

Overview

general class to handle comparing and pushing data to the remote end

Direct Known Subclasses

FamilyUpdater, PeopleUpdater

Constant Summary collapse

MAX_HASHES_AT_A_TIME =
500
MAX_TO_BATCH_AT_A_TIME =
10
SLEEP_PERIOD =
3
RETRIES =
5
RETRY_SLEEP =
15

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = nil, options = {}) ⇒ UpdateAgent

Returns a new instance of UpdateAgent.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/updateagent/updateagent.rb', line 10

def initialize(data=nil, options={})
  @options = options
  @options['debug'] = true
  @attributes = []
  @data = []
  @create = []
  @update = []
  if data
    if data.is_a?(Array)
      @data = data
      @attributes = data.first.keys.sort
    else
      read_from_file(data)
    end
  end
  check_for_invalid_columns
end

Class Attribute Details

.resourceObject

Returns the value of attribute resource.



244
245
246
# File 'lib/updateagent/updateagent.rb', line 244

def resource
  @resource
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



241
242
243
# File 'lib/updateagent/updateagent.rb', line 241

def attributes
  @attributes
end

#createObject (readonly)

Returns the value of attribute create.



242
243
244
# File 'lib/updateagent/updateagent.rb', line 242

def create
  @create
end

#dataObject

Returns the value of attribute data.



241
242
243
# File 'lib/updateagent/updateagent.rb', line 241

def data
  @data
end

#syncObject

Returns the value of attribute sync.



241
242
243
# File 'lib/updateagent/updateagent.rb', line 241

def sync
  @sync
end

#updateObject (readonly)

Returns the value of attribute update.



242
243
244
# File 'lib/updateagent/updateagent.rb', line 242

def update
  @update
end

Instance Method Details

#check_for_invalid_columnsObject



28
29
30
31
32
33
34
35
36
# File 'lib/updateagent/updateagent.rb', line 28

def check_for_invalid_columns
  if invalid = @data.detect { |row| row['id'] }
    puts "Error: one or more records contain an 'id' column."
    puts "You must utilize 'legacy_id' rather than 'id' so that"
    puts "identity and foreign keys are maintained from your"
    puts "existing membership management database."
    exit(1)
  end
end

#compare(force = false) ⇒ Object



99
100
101
# File 'lib/updateagent/updateagent.rb', line 99

def compare(force=false)
  compare_hashes(legacy_ids, force)
end

#confirmObject



124
125
126
# File 'lib/updateagent/updateagent.rb', line 124

def confirm
  agree('Do you want to continue, pushing these records to OneBody? ')
end

#data_by_idObject



233
234
235
236
237
238
239
# File 'lib/updateagent/updateagent.rb', line 233

def data_by_id
  @data_by_id ||= begin
    by_id = {}
    @data.each { |r| by_id[r['legacy_id'].to_i] = r }
    by_id
  end
end

#errorsObject



172
173
174
# File 'lib/updateagent/updateagent.rb', line 172

def errors
  (@create + @update).select { |r| r['error_messages'] }
end

#finish_sync(create_items = true, mark_complete = true) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/updateagent/updateagent.rb', line 200

def finish_sync(create_items=true, mark_complete=true)
  if create_items
    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
    @sync.success_count = (@create + @update).length - errors.length
    @sync.save
  end
end

#has_work?Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/updateagent/updateagent.rb', line 103

def has_work?
  (@create + @update).any?
end

#idsObject



91
92
93
# File 'lib/updateagent/updateagent.rb', line 91

def ids
  @data.map { |r| r['id'] }.compact
end

#legacy_idsObject



95
96
97
# File 'lib/updateagent/updateagent.rb', line 95

def legacy_ids
  @data.map { |r| r['legacy_id'] }.compact
end

#presentObject



107
108
109
110
111
112
113
114
# File 'lib/updateagent/updateagent.rb', line 107

def present
  puts "The following #{resource.name.downcase} records will be pushed..."
  puts 'legacy id  name'
  puts '---------- -------------------------------------'
  @create.each { |r| present_record(r, true) }
  @update.each { |r| present_record(r) }
  puts
end

#present_record(row, new = false) ⇒ Object



116
117
118
119
120
121
122
# File 'lib/updateagent/updateagent.rb', line 116

def present_record(row, new=false)
  puts "#{row['legacy_id'].to_s.ljust(10)} #{name_for(row).to_s.ljust(40)} #{new ? '(new)' : '     '}"
  if @options['debug']
    puts "Local values:  #{row.values_for_hash(@attributes).join}"
    puts "Remote values: #{row['remote_hash']}"
  end
end

#pushObject

use ActiveResource to create/update records on remote end



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/updateagent/updateagent.rb', line 129

def push
  @errors = []
  puts 'Updating remote end...'
  index = 0
  print "#{resource.name} 0/0\r"; STDOUT.flush
  (@create + @update).each_slice(MAX_TO_BATCH_AT_A_TIME) do |records|
    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
        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']
      record['status'] = status['status']
      if status['error']
        puts "#{status['legacy_id']}: #{status['error']}"
        @errors << {:record => record, :error => status['error']}
        record['error_messages'] = status['error']
      end
    end
    index += records.length
    print "#{resource.name} #{index}/#{@create.length + @update.length}\r"; STDOUT.flush
    sleep SLEEP_PERIOD
  end
  puts
end

#read_from_file(filename) ⇒ Object

load data from csv file and do some type conversion for bools and dates first row must be attribute names



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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/updateagent/updateagent.rb', line 40

def read_from_file(filename)
  csv = FasterCSV.open(filename, 'r')
  @attributes = csv.shift
  record_count = 0
  @data = csv.map do |row|
    hash = {}
    row.each_with_index do |value, index|
      key = @attributes[index]
      next if IGNORE_ATTRIBUTES.include?(key)
      if DATETIME_ATTRIBUTES.include?(key)
        if value.blank?
          value = nil
        else
          begin
            value = DateTime.parse(value)
            value = DateTime.parse(value.strftime('1900-%m-%d %H:%M')) if value.year == 0
          rescue ArgumentError
            puts "Invalid date in #{filename} record #{index} (#{key}) - #{value}"
            exit(1)
          end
        end
      elsif BOOLEAN_ATTRIBUTES.include?(key)
        if value == '' or value == nil
          value = nil
        elsif %w(no false 0).include?(value.downcase)
          value = false
        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"
    if hash.any?
      hash['deleted'] = false
      hash
    else
      nil
    end
  end
  @data.compact!
  puts
  @attributes << 'deleted'
  @attributes.reject! { |a| IGNORE_ATTRIBUTES.include?(a) }
end

#resourceObject



245
# File 'lib/updateagent/updateagent.rb', line 245

def resource; self.class.resource; end

#send_notificationObject



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/updateagent/updateagent.rb', line 176

def send_notification
  if n = @options['notifications']
    puts 'Sending notification...'
    if @errors and @errors.any?
      subject = 'OneBody UpdateAgent Errors'
      body = "There were #{@errors.length} error(s) running UpdateAgent.\n\nPlease visit #{ONEBODY_SITE}/admin/syncs for details."
    else
      subject = 'OneBody UpdateAgent Success'
      body = "OneBody UpdateAgent completed without any errors.\n"
    end
    Net::SMTP.start(n['host'], n['port'].to_i) do |smtp|
      smtp.send_message(
        "From: #{n['from_email']}\nTo: #{n['to_email']}\nSubject: #{subject}\n\n#{body}",
        n['from_email'],
        n['to_email']
      )
    end
  end
end

#start_syncObject



196
197
198
# File 'lib/updateagent/updateagent.rb', line 196

def start_sync
  @sync = Sync.create(:complete => false, :started_at => Time.now)
end

#to_sync_item(record, operation) ⇒ Object



218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/updateagent/updateagent.rb', line 218

def to_sync_item(record, operation)
  h = {
    :syncable_type  => self.resource.name,
    :syncable_id    => record['id'],
    :name           => record['name'],
    :legacy_id      => record['legacy_id'],
    :operation      => operation,
    :status         => record['status']
  }
  if record['error_messages']
    h[:error_messages] = record['error_messages']
  end
  return h
end