Class: BatchesTaskProcessor::Model
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- BatchesTaskProcessor::Model
- Defined in:
- lib/batches_task_processor/model.rb
Instance Method Summary collapse
- #all_processed? ⇒ Boolean
- #cancel ⇒ Object
-
#data ⇒ Object
Text data columns support (Mysql only).
- #export ⇒ Object
- #finish! ⇒ Object
-
#qty_items_job ⇒ Object
state: :pending, :processing, :finished, :canceled.
- #retry_failures ⇒ Object
-
#start! ⇒ Object
********* user methods.
- #status ⇒ Object
Instance Method Details
#all_processed? ⇒ Boolean
22 23 24 |
# File 'lib/batches_task_processor/model.rb', line 22 def all_processed? items.count == data.count end |
#cancel ⇒ Object
36 37 38 |
# File 'lib/batches_task_processor/model.rb', line 36 def cancel update!(state: :canceled) end |
#data ⇒ Object
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 |
#export ⇒ Object
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_job ⇒ Object
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_failures ⇒ Object
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 |
#status ⇒ Object
40 41 42 |
# File 'lib/batches_task_processor/model.rb', line 40 def status Rails.logger.info "Process status: #{items.count}/#{data.count}" end |