Class: BatchesTaskProcessor::Model

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/batches_task_processor/model.rb

Instance Method Summary collapse

Instance Method Details

#all_processed?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/batches_task_processor/model.rb', line 22

def all_processed?
  items.count == data.count
end

#cancelObject



36
37
38
# File 'lib/batches_task_processor/model.rb', line 36

def cancel
  update!(state: :canceled)
end

#dataObject

Text data columns support (Mysql only)



27
28
29
# File 'lib/batches_task_processor/model.rb', line 27

def data
  self[:data].is_a?(String) ? JSON.parse(self[:data] || '[]') : self[:data]
end

#exportObject



48
49
50
51
52
53
54
55
# File 'lib/batches_task_processor/model.rb', line 48

def export
  filename = (key || 'batches_task_processor_result').try(:parameterize)
  path = Rails.root.join("tmp/#{filename}.csv")
  data = items.pluck(:key, :result, :error_details)
  data = [['Key', 'Result', 'Error details']] + data
  File.write(path, data.map(&:to_csv).join)
  Rails.logger.info "Exported to #{path}"
end

#finish!Object



18
19
20
# File 'lib/batches_task_processor/model.rb', line 18

def finish!
  update!(state: :finished, finished_at: Time.current)
end

#qty_items_jobObject

state: :pending, :processing, :finished, :canceled



14
15
16
# File 'lib/batches_task_processor/model.rb', line 14

def qty_items_job
  @qty_items_job ||= (data.count.to_f / qty_jobs).ceil
end

#retry_failuresObject



44
45
46
# File 'lib/batches_task_processor/model.rb', line 44

def retry_failures
  start!
end

#start!Object

********* user methods



32
33
34
# File 'lib/batches_task_processor/model.rb', line 32

def start!
  Processor.new(id).call
end

#statusObject



40
41
42
# File 'lib/batches_task_processor/model.rb', line 40

def status
  Rails.logger.info "Process status: #{items.count}/#{data.count}"
end