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

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

Instance Method Details

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



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'app/models/good_job/batch_record.rb', line 55

def _continue_discard_or_finish(execution = nil, lock: true)
  execution_discarded = execution && execution.finished_at.present? && execution.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 execution_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 && !finished_at && jobs.where(finished_at: nil).count.zero?
          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
      end
    end
  end
  buffer.call
end

#display_attributesObject



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

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

#properties=(value) ⇒ Object

Raises:

  • (ArgumentError)


94
95
96
97
98
# File 'app/models/good_job/batch_record.rb', line 94

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)


43
44
45
# File 'app/models/good_job/batch_record.rb', line 43

def succeeded?
  !discarded? && finished?
end

#to_batchObject



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

def to_batch
  Batch.new(_record: self)
end