Class: HouzzInventoryUpdaterWorker

Inherits:
Object
  • Object
show all
Includes:
Sidekiq::Job
Defined in:
app/workers/houzz_inventory_updater_worker.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.lock_args(args) ⇒ Object

First two keys are sufficient to determine this job as unique



8
9
10
# File 'app/workers/houzz_inventory_updater_worker.rb', line 8

def self.lock_args(args)
  [ args[0], args[1] ]
end

Instance Method Details

#instantiate_transporter(transporter, transporter_profile = nil) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'app/workers/houzz_inventory_updater_worker.rb', line 37

def instantiate_transporter(transporter, transporter_profile = nil)
  case transporter
  when :sftp
    'Transport::SftpConnection'.constantize.new(transporter_profile)
  when :http_api
    Transport::HttpApiConnection.new({ profile: transporter_profile })
  else
    raise "Unknown transporter: #{transporter}"
  end
end

#perform(ecl_id, count, total, inventory_entry_json, transporter, transporter_profile, remote_path, transporter_send_method) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/workers/houzz_inventory_updater_worker.rb', line 12

def perform(ecl_id, count, total, inventory_entry_json, transporter, transporter_profile, remote_path, transporter_send_method)
  ecl = EdiCommunicationLog.find(ecl_id)
  if ecl&.processing?
    transport = instantiate_transporter(transporter.to_sym, transporter_profile.to_sym)
    res = transport.send_data(inventory_entry_json, remote_path, transporter_send_method)
    logger.info "Sending inventory update for entry: #{count} of #{total} #{inventory_entry_json}, using #{transporter_send_method} #{remote_path}, res: #{res}"
    ecl.transmit_datetime = Time.current
    ecl.notes = ecl.notes.to_s + "| entry: #{count} of #{total}: HTTP CODE: #{res[:http_result].try(:code)}, HTTP BODY: #{res[:http_result].try(:body)}, "
    ecl.save
    if res[:success]
      if count == total # here we assume that if we got to the last one we can mark it complete if all good
        if ecl.notes.index('| error').nil?
          ecl.complete!
        else
          ecl.error!
        end
      end
    else
      # add an error and show its payload but keep going until the end
      ecl.notes = ecl.notes.to_s + "| error payload: #{inventory_entry_json}"
      ecl.save
    end
  end
end