Class: GoodJob::BatchRecord

Inherits:
BaseRecord
  • Object
show all
Includes:
AdvisoryLockable, Filterable
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)


28
29
30
# File 'app/models/good_job/batch_record.rb', line 28

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



58
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'app/models/good_job/batch_record.rb', line 58

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
      reload

      if job_discarded && !discarded_at
        update(discarded_at: Time.current)

        if on_discard.present?
          discard_job_class = on_discard.constantize
          Job.defer_after_commit_maybe(discard_job_class) do
            Batch.within_thread(batch_id: nil, batch_callback_id: id) do
              discard_job_class.set(priority: callback_priority, queue: callback_queue_name).perform_later(to_batch, { event: :discard })
            end
          end
        end
      end

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

        if !discarded_at && on_success.present?
          success_job_class = on_success.constantize
          Job.defer_after_commit_maybe(success_job_class) do
            Batch.within_thread(batch_id: nil, batch_callback_id: id) do
              success_job_class.set(priority: callback_priority, queue: callback_queue_name).perform_later(to_batch, { event: :success })
            end
          end
        end

        if on_finish.present?
          finish_job_class = on_finish.constantize
          Job.defer_after_commit_maybe(finish_job_class) do
            Batch.within_thread(batch_id: nil, batch_callback_id: id) do
              on_finish.constantize.set(priority: callback_priority, queue: callback_queue_name).perform_later(to_batch, { event: :finish })
            end
          end
        end
      end

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

  buffer.call
end

#display_attributesObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/models/good_job/batch_record.rb', line 42

def display_attributes
  display_properties = begin
    serialized_properties
  rescue ActiveJob::DeserializationError
    JSON.parse(read_attribute_before_type_cast(:serialized_properties))
  end

  attribute_names.to_h do |name|
    if name == "serialized_properties"
      ["properties", display_properties]
    else
      [name, self[name]]
    end
  end
end

#jobs_finished?Boolean

Returns:

  • (Boolean)


129
130
131
# File 'app/models/good_job/batch_record.rb', line 129

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

#jobs_finished_atObject



133
134
135
# File 'app/models/good_job/batch_record.rb', line 133

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

#properties=(value) ⇒ Object

Raises:

  • (ArgumentError)


123
124
125
126
127
# File 'app/models/good_job/batch_record.rb', line 123

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)


34
35
36
# File 'app/models/good_job/batch_record.rb', line 34

def succeeded?
  !discarded? && finished?
end

#to_batchObject



38
39
40
# File 'app/models/good_job/batch_record.rb', line 38

def to_batch
  Batch.new(_record: self)
end