Class: GoodJob::BatchRecord

Inherits:
BaseRecord
  • Object
show all
Includes:
AdvisoryLockable
Defined in:
app/models/good_job/batch_record.rb

Defined Under Namespace

Classes: PropertySerializer

Constant Summary

Constants included from AdvisoryLockable

AdvisoryLockable::RecordAlreadyAdvisoryLockedError

Class Method Summary collapse

Instance Method Summary collapse

Methods included from AdvisoryLockable

#advisory_lock, #advisory_lock!, #advisory_locked?, #advisory_unlock, #advisory_unlock!, #advisory_unlocked?, #lockable_column_key, #lockable_key, #owns_advisory_lock?, #with_advisory_lock

Methods inherited from BaseRecord

bind_value, migrated?, migration_pending_warning!, with_logger_silenced

Class Method Details

.jobs_finished_at_migrated?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'app/models/good_job/batch_record.rb', line 41

def self.jobs_finished_at_migrated?
  column_names.include?('jobs_finished_at')
end

Instance Method Details

#_continue_discard_or_finish(job = nil, lock: true) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'app/models/good_job/batch_record.rb', line 59

def _continue_discard_or_finish(job = nil, lock: true)
  job_discarded = job && job.finished_at.present? && job.error.present?
  buffer = GoodJob::Adapter::InlineBuffer.capture do
    advisory_lock_maybe(lock) do
      Batch.within_thread(batch_id: nil, batch_callback_id: id) do
        reload

        if job_discarded && !discarded_at
          update(discarded_at: Time.current)
          on_discard.constantize.set(priority: callback_priority, queue: callback_queue_name).perform_later(to_batch, { event: :discard }) if on_discard.present?
        end

        if enqueued_at && !(self.class.jobs_finished_at_migrated? ? jobs_finished_at : finished_at) && jobs.where(finished_at: nil).count.zero?
          self.class.jobs_finished_at_migrated? ? update(jobs_finished_at: Time.current) : update(finished_at: Time.current)

          on_success.constantize.set(priority: callback_priority, queue: callback_queue_name).perform_later(to_batch, { event: :success }) if !discarded_at && on_success.present?
          on_finish.constantize.set(priority: callback_priority, queue: callback_queue_name).perform_later(to_batch, { event: :finish }) if on_finish.present?
        end

        update(finished_at: Time.current) if !finished_at && self.class.jobs_finished_at_migrated? && jobs_finished? && callback_jobs.where(finished_at: nil).count.zero?
      end
    end
  end
  buffer.call
end

#display_attributesObject



55
56
57
# File 'app/models/good_job/batch_record.rb', line 55

def display_attributes
  attributes.except('serialized_properties').merge(properties: properties)
end

#jobs_finished?Boolean

Returns:

  • (Boolean)


108
109
110
# File 'app/models/good_job/batch_record.rb', line 108

def jobs_finished?
  self.class.jobs_finished_at_migrated? ? jobs_finished_at : finished_at
end

#jobs_finished_atObject



112
113
114
# File 'app/models/good_job/batch_record.rb', line 112

def jobs_finished_at
  self.class.jobs_finished_at_migrated? ? self[:jobs_finished_at] : self[:finished_at]
end

#properties=(value) ⇒ Object

Raises:

  • (ArgumentError)


102
103
104
105
106
# File 'app/models/good_job/batch_record.rb', line 102

def properties=(value)
  raise ArgumentError, "Properties must be a Hash" unless value.is_a?(Hash)

  self.serialized_properties = value
end

#succeeded?Boolean

Whether the batch has finished and no jobs were discarded

Returns:

  • (Boolean)


47
48
49
# File 'app/models/good_job/batch_record.rb', line 47

def succeeded?
  !discarded? && finished?
end

#to_batchObject



51
52
53
# File 'app/models/good_job/batch_record.rb', line 51

def to_batch
  Batch.new(_record: self)
end