Class: Amigo::DurableJob::ServerMiddleware

Inherits:
Object
  • Object
show all
Defined in:
lib/amigo/durable_job.rb

Instance Method Summary collapse

Instance Method Details

#call(worker, job, _queue) ⇒ Object



392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
# File 'lib/amigo/durable_job.rb', line 392

def call(worker, job, _queue)
  return yield unless Amigo::DurableJob.enabled? && worker.class.respond_to?(:heartbeat_extension)
  ds, row = Amigo::DurableJob.lock_job(worker.jid, worker.class.heartbeat_extension)
  if row.nil?
    Sidekiq.logger.error "DurableJob: #{worker.class}[#{worker.jid}]: no row found in database"
    return yield
  end
  Thread.current[:durable_job_active_job] = worker, ds
  # rubocop:disable Lint/RescueException
  begin
    yield
  rescue Exception => e
    j2 = job.dup
    j2["error_class"] = e.class.to_s
    j2["error_message"] = e.to_s
    Amigo::DurableJob.unlock_job(ds, worker.jid, worker.class.heartbeat_extension, job_item_json: j2.to_json)
    raise
  ensure
    Thread.current[:durable_job_active_job] = nil
  end
  # rubocop:enable Lint/RescueException
  ds.where(job_id: row[:job_id]).delete
end