Class: ExportRestaurantPackagesWorker

Inherits:
ApplicationWorker show all
Includes:
ActionView::Helpers::DateHelper, Sidekiq::Status::Worker
Defined in:
app/workers/export_restaurant_packages_worker.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationWorker

unlimited_retry

Instance Attribute Details

#attachmentObject

Returns the value of attribute attachment.



6
7
8
# File 'app/workers/export_restaurant_packages_worker.rb', line 6

def attachment
  @attachment
end

#emailsObject

Returns the value of attribute emails.



6
7
8
# File 'app/workers/export_restaurant_packages_worker.rb', line 6

def emails
  @emails
end

#job_idObject

Returns the value of attribute job_id.



6
7
8
# File 'app/workers/export_restaurant_packages_worker.rb', line 6

def job_id
  @job_id
end

Instance Method Details

#perform(emails) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/workers/export_restaurant_packages_worker.rb', line 8

def perform(emails)
  I18n.with_locale :en do
    self.emails = Array.wrap(emails)
    total 100
    store status: Attachment::ON_PROGRESS_STATUS
    store email_status: Attachment::PENDING_EMAIL_STATUS
    at 0

    begin
      # Assign job ID
      self.job_id = self.jid
      filename = "restaurant_packages-#{Time.zone.now.to_i}.csv"
      filepath = Rails.root.join('tmp', filename)

      File.open(filepath, 'w') do |f|
        csv = []
        generate_csv(csv)
        f.write csv.join
      end

      # Save the attachment and update the status
      create_attachment(filepath)
      send_email

      store status: Attachment::DONE_STATUS
      at 100

      # Clean up
      File.delete(filepath) if File.exist?(filepath)

    rescue StandardError => e
      APMErrorHandler.report(e)
      store status: Attachment::FAILED_STATUS
      raise e
    end
  end
end