Class: SidekiqBatch::Middleware

Inherits:
Object
  • Object
show all
Defined in:
app/models/sidekiq_batch/middleware.rb

Overview

Sidekiq server middleware. Registered last in the chain so it runs outside Sidekiq’s retry middleware — we see the terminal disposition of every attempt.

  • On success: mark complete, run completion check.

  • On failure: only mark failed on the FINAL attempt (retries exhausted or retry: false). Non-terminal failures leave the row ‘pending` and skip the completion check; the next attempt will decide.

  • Skips gracefully for jobs with no SidekiqBatchJob row (untracked).

  • ‘mark_complete!` / `mark_failed!` are idempotent (atomic `WHERE status = ’pending’‘ update), so replays are no-ops.

Constant Summary collapse

DEFAULT_MAX_RETRIES =
25

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.handle_death(job, error) ⇒ Object

Also used by the Sidekiq death handler to reconcile jobs that died without re-entering middleware (SIGKILL, OOM, pod eviction, etc.).



39
40
41
42
43
44
45
46
# File 'app/models/sidekiq_batch/middleware.rb', line 39

def self.handle_death(job, error)
  batch_job = ::SidekiqBatchJob.find_by(jid: job["jid"])

  return unless batch_job
  return unless batch_job.mark_failed!(error)

  batch_job.sidekiq_batch.attempt_completion!
end

Instance Method Details

#call(worker, job, _queue) ⇒ Object

rubocop:disable Metrics/MethodLength



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/models/sidekiq_batch/middleware.rb', line 18

def call(worker, job, _queue) # rubocop:disable Metrics/MethodLength
  batch_job = ::SidekiqBatchJob.find_by(jid: job["jid"])

  unless batch_job
    yield

    return
  end

  begin
    yield
  rescue StandardError => e
    handle_failure(worker, job, batch_job, e)
    raise
  end

  handle_success(batch_job)
end