|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module SolidQueue |
| 4 | + class BatchRecord < Record |
| 5 | + self.table_name = "solid_queue_job_batches" |
| 6 | + |
| 7 | + STATUSES = %w[pending processing completed failed] |
| 8 | + |
| 9 | + belongs_to :parent_job_batch, foreign_key: :parent_job_batch_id, class_name: "SolidQueue::BatchRecord", optional: true |
| 10 | + has_many :jobs, foreign_key: :batch_id, primary_key: :batch_id |
| 11 | + has_many :children, foreign_key: :parent_job_batch_id, primary_key: :batch_id, class_name: "SolidQueue::BatchRecord" |
| 12 | + |
| 13 | + serialize :on_finish, coder: JSON |
| 14 | + serialize :on_success, coder: JSON |
| 15 | + serialize :on_failure, coder: JSON |
| 16 | + serialize :metadata, coder: JSON |
| 17 | + |
| 18 | + validates :status, inclusion: { in: STATUSES } |
| 19 | + |
| 20 | + scope :pending, -> { where(status: "pending") } |
| 21 | + scope :processing, -> { where(status: "processing") } |
| 22 | + scope :completed, -> { where(status: "completed") } |
| 23 | + scope :failed, -> { where(status: "failed") } |
| 24 | + scope :finished, -> { where(status: %w[completed failed]) } |
| 25 | + scope :unfinished, -> { where(status: %w[pending processing]) } |
| 26 | + |
| 27 | + after_initialize :set_batch_id |
| 28 | + before_create :set_parent_job_batch_id |
| 29 | + |
| 30 | + def on_success=(value) |
| 31 | + super(serialize_callback(value)) |
| 32 | + end |
| 33 | + |
| 34 | + def on_failure=(value) |
| 35 | + super(serialize_callback(value)) |
| 36 | + end |
| 37 | + |
| 38 | + def on_finish=(value) |
| 39 | + super(serialize_callback(value)) |
| 40 | + end |
| 41 | + |
| 42 | + def job_finished!(job) |
| 43 | + return if finished? |
| 44 | + return if job.batch_processed_at? |
| 45 | + |
| 46 | + job.with_lock do |
| 47 | + if job.batch_processed_at.blank? |
| 48 | + job.update!(batch_processed_at: Time.current) |
| 49 | + |
| 50 | + if job.failed_execution.present? |
| 51 | + self.class.where(id: id).update_all( |
| 52 | + "failed_jobs = failed_jobs + 1, pending_jobs = pending_jobs - 1" |
| 53 | + ) |
| 54 | + else |
| 55 | + self.class.where(id: id).update_all( |
| 56 | + "completed_jobs = completed_jobs + 1, pending_jobs = pending_jobs - 1" |
| 57 | + ) |
| 58 | + end |
| 59 | + end |
| 60 | + end |
| 61 | + |
| 62 | + reload |
| 63 | + check_completion! |
| 64 | + end |
| 65 | + |
| 66 | + def check_completion! |
| 67 | + return if finished? |
| 68 | + |
| 69 | + actual_children = children.count |
| 70 | + return if actual_children < expected_children |
| 71 | + |
| 72 | + children.find_each do |child| |
| 73 | + return unless child.finished? |
| 74 | + end |
| 75 | + |
| 76 | + with_lock do |
| 77 | + if finished? |
| 78 | + # do nothing |
| 79 | + elsif pending_jobs <= 0 |
| 80 | + if failed_jobs > 0 |
| 81 | + mark_as_failed! |
| 82 | + else |
| 83 | + mark_as_completed! |
| 84 | + end |
| 85 | + clear_unpreserved_jobs |
| 86 | + elsif status == "pending" |
| 87 | + update!(status: "processing") |
| 88 | + end |
| 89 | + end |
| 90 | + end |
| 91 | + |
| 92 | + def finished? |
| 93 | + status.in?(%w[completed failed]) |
| 94 | + end |
| 95 | + |
| 96 | + def processing? |
| 97 | + status == "processing" |
| 98 | + end |
| 99 | + |
| 100 | + def pending? |
| 101 | + status == "pending" |
| 102 | + end |
| 103 | + |
| 104 | + def progress_percentage |
| 105 | + return 0 if total_jobs == 0 |
| 106 | + ((completed_jobs + failed_jobs) * 100.0 / total_jobs).round(2) |
| 107 | + end |
| 108 | + |
| 109 | + private |
| 110 | + |
| 111 | + def set_parent_job_batch_id |
| 112 | + self.parent_job_batch_id ||= Batch.current_batch_id if Batch.current_batch_id.present? |
| 113 | + end |
| 114 | + |
| 115 | + def set_batch_id |
| 116 | + self.batch_id ||= SecureRandom.uuid |
| 117 | + end |
| 118 | + |
| 119 | + def as_active_job(active_job_klass) |
| 120 | + active_job_klass.is_a?(ActiveJob::Base) ? active_job_klass : active_job_klass.new |
| 121 | + end |
| 122 | + |
| 123 | + def serialize_callback(value) |
| 124 | + return value if value.blank? |
| 125 | + active_job = as_active_job(value) |
| 126 | + # We can pick up batch ids from context, but callbacks should never be considered a part of the batch |
| 127 | + active_job.batch_id = nil |
| 128 | + active_job.serialize |
| 129 | + end |
| 130 | + |
| 131 | + def perform_completion_job(job_field, attrs) |
| 132 | + active_job = ActiveJob::Base.deserialize(send(job_field)) |
| 133 | + active_job.send(:deserialize_arguments_if_needed) |
| 134 | + active_job.arguments = [ Batch.new(_batch_record: self) ] + Array.wrap(active_job.arguments) |
| 135 | + SolidQueue::Job.enqueue_all([ active_job ]) |
| 136 | + |
| 137 | + active_job.provider_job_id = Job.find_by(active_job_id: active_job.job_id).id |
| 138 | + attrs[job_field] = active_job.serialize |
| 139 | + end |
| 140 | + |
| 141 | + def mark_as_completed! |
| 142 | + # SolidQueue does treats `discard_on` differently than failures. The job will report as being :finished, |
| 143 | + # and there is no record of the failure. |
| 144 | + # GoodJob would report a discard as an error. It's possible we should do that in the future? |
| 145 | + update!(status: "completed", finished_at: Time.current) |
| 146 | + |
| 147 | + perform_completion_job(:on_success, {}) if on_success.present? |
| 148 | + perform_completion_job(:on_finish, {}) if on_finish.present? |
| 149 | + |
| 150 | + if parent_job_batch_id.present? |
| 151 | + parent = BatchRecord.find_by(batch_id: parent_job_batch_id) |
| 152 | + parent&.reload&.check_completion! |
| 153 | + end |
| 154 | + end |
| 155 | + |
| 156 | + def mark_as_failed! |
| 157 | + update!(status: "failed", finished_at: Time.current) |
| 158 | + perform_completion_job(:on_failure, {}) if on_failure.present? |
| 159 | + perform_completion_job(:on_finish, {}) if on_finish.present? |
| 160 | + |
| 161 | + # Check if parent batch can now complete |
| 162 | + if parent_job_batch_id.present? |
| 163 | + parent = BatchRecord.find_by(batch_id: parent_job_batch_id) |
| 164 | + parent&.check_completion! |
| 165 | + end |
| 166 | + end |
| 167 | + |
| 168 | + def clear_unpreserved_jobs |
| 169 | + SolidQueue::Batch::CleanupJob.perform_later(self) unless SolidQueue.preserve_finished_jobs? |
| 170 | + end |
| 171 | + end |
| 172 | +end |
| 173 | + |
| 174 | +require_relative "batch_record/buffer" |
0 commit comments