|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module SolidQueue |
| 4 | + class JobBatch < Record |
| 5 | + belongs_to :job, foreign_key: :job_id, optional: true |
| 6 | + has_many :jobs, foreign_key: :batch_id |
| 7 | + |
| 8 | + scope :incomplete, -> { |
| 9 | + where(finished_at: nil).where("changed_at IS NOT NULL OR last_changed_at < ?", 1.hour.ago) |
| 10 | + } |
| 11 | + |
| 12 | + class << self |
| 13 | + def current_batch_id |
| 14 | + Thread.current[:current_batch_id] |
| 15 | + end |
| 16 | + |
| 17 | + def enqueue(attributes = {}) |
| 18 | + previous_batch_id = current_batch_id.presence || nil |
| 19 | + |
| 20 | + job_batch = nil |
| 21 | + transaction do |
| 22 | + job_batch = create!(batch_attributes(attributes)) |
| 23 | + Thread.current[:current_batch_id] = job_batch.id |
| 24 | + yield |
| 25 | + end |
| 26 | + |
| 27 | + job_batch |
| 28 | + ensure |
| 29 | + Thread.current[:current_batch_id] = previous_batch_id |
| 30 | + end |
| 31 | + |
| 32 | + def dispatch_finished_batches |
| 33 | + incomplete.order(:id).pluck(:id).each do |id| |
| 34 | + transaction do |
| 35 | + where(id:).non_blocking_lock.each(&:finish) |
| 36 | + end |
| 37 | + end |
| 38 | + end |
| 39 | + |
| 40 | + private |
| 41 | + |
| 42 | + def batch_attributes(attributes) |
| 43 | + attributes = case attributes |
| 44 | + in { on_finish: on_finish_klass } |
| 45 | + attributes.merge( |
| 46 | + job_class: on_finish_klass, |
| 47 | + completion_type: "success" |
| 48 | + ) |
| 49 | + in { on_success: on_success_klass } |
| 50 | + attributes.merge( |
| 51 | + job_class: on_success_klass, |
| 52 | + completion_type: "success" |
| 53 | + ) |
| 54 | + end |
| 55 | + |
| 56 | + attributes.except(:on_finish, :on_success) |
| 57 | + end |
| 58 | + end |
| 59 | + |
| 60 | + def finished? |
| 61 | + finished_at.present? |
| 62 | + end |
| 63 | + |
| 64 | + def finish |
| 65 | + return if finished? |
| 66 | + reset_changed_at |
| 67 | + jobs.find_each do |next_job| |
| 68 | + # FIXME: If it's failed but is going to retry, how do we know? |
| 69 | + # Because we need to know if we will determine what the failed execution means |
| 70 | + # FIXME: use "success" vs "finish" vs "discard" `completion_type` to determine |
| 71 | + # how to analyze each job |
| 72 | + return unless next_job.finished? |
| 73 | + end |
| 74 | + |
| 75 | + attrs = {} |
| 76 | + |
| 77 | + if job_class.present? |
| 78 | + job_klass = job_class.constantize |
| 79 | + active_job = job_klass.perform_later(self) |
| 80 | + attrs[:job] = Job.find_by(active_job_id: active_job.job_id) |
| 81 | + end |
| 82 | + |
| 83 | + update!({ finished_at: Time.zone.now }.merge(attrs)) |
| 84 | + end |
| 85 | + |
| 86 | + private |
| 87 | + |
| 88 | + def reset_changed_at |
| 89 | + if changed_at.blank? && last_changed_at.present? |
| 90 | + update_columns(last_changed_at: Time.zone.now) # wait another hour before we check again |
| 91 | + else |
| 92 | + update_columns(changed_at: nil) # clear out changed_at so we ignore this until the next job finishes |
| 93 | + end |
| 94 | + end |
| 95 | + end |
| 96 | +end |
0 commit comments