Class: BatchProcessor::BatchJob

Inherits:
ActiveJob::Base
  • Object
show all
Includes:
Technologic
Defined in:
lib/batch_processor/batch_job.rb

Overview

BatchProcessor depends on ActiveJob for handling the processing of individual items in a collection.

Direct Known Subclasses

ApplicationJob

Defined Under Namespace

Classes: BatchAbortedError

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#batch_idObject

Returns the value of attribute batch_id.



8
9
10
# File 'lib/batch_processor/batch_job.rb', line 8

def batch_id
  @batch_id
end

#tracked_batch_failureObject

Returns the value of attribute tracked_batch_failure.



8
9
10
# File 'lib/batch_processor/batch_job.rb', line 8

def tracked_batch_failure
  @tracked_batch_failure
end

#tracked_batch_runningObject

Returns the value of attribute tracked_batch_running.



8
9
10
# File 'lib/batch_processor/batch_job.rb', line 8

def tracked_batch_running
  @tracked_batch_running
end

Instance Method Details

#batchObject



60
61
62
63
64
# File 'lib/batch_processor/batch_job.rb', line 60

def batch
  return unless batch_job?

  @batch ||= BatchProcessor::BatchBase.find(batch_id)
end

#batch_job?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/batch_processor/batch_job.rb', line 66

def batch_job?
  batch_id.present?
end

#deserialize(job_data) ⇒ Object



55
56
57
58
# File 'lib/batch_processor/batch_job.rb', line 55

def deserialize(job_data)
  super(job_data)
  self.batch_id = job_data["batch_id"]
end

#rescue_with_handler(exception) ⇒ Object

Some combination of Sidekiq + ActiveJob + Postgres + Deadlocks = this getting called twice for the same instance. It is unclear WHY that situation happens, but during the second execution, the instance no longer has it’s job_id but somehow still has a batch ID. It seems regardless, an internal semaphore seems to prevent miscounting in that situation. I’d love to know what the root cause is behind it, but async debugging is time consuming and hard. :(



36
37
38
39
40
41
42
43
# File 'lib/batch_processor/batch_job.rb', line 36

def rescue_with_handler(exception)
  batch.job_canceled and return exception if exception.is_a?(BatchAbortedError)

  batch_job_failure(exception) if batch_job? && !tracked_batch_failure
  self.tracked_batch_failure = true

  super
end

#retry_jobObject



45
46
47
48
49
# File 'lib/batch_processor/batch_job.rb', line 45

def retry_job(*)
  return if batch_job? && batch.processor_class.disable_retries?

  super
end

#serializeObject



51
52
53
# File 'lib/batch_processor/batch_job.rb', line 51

def serialize
  super.merge("batch_id" => batch_id) # rubocop:disable Style/StringHashKeys
end