Class: Asyncapi::Server::JobWorker

Inherits:
Object
  • Object
show all
Includes:
Sidekiq::Worker
Defined in:
app/workers/asyncapi/server/job_worker.rb

Constant Summary collapse

MAX_RETRIES =
2

Instance Method Summary collapse

Instance Method Details

#perform(job_id, retries = 0) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/workers/asyncapi/server/job_worker.rb', line 8

def perform(job_id, retries=0)
  job = Job.find(job_id)
  runner_class = job.class_name.constantize

  job_status = :success
  job_message = runner_class.call(job.params)
rescue => e
  job_status = :error
  job_message = [e.message, e.backtrace].flatten.join("\n")
  raise e
ensure
  if job
    job.update_attributes(status: job_status)
    report_job_status(job, job_message)
  else
    # For some reason "ActiveRecord::Base.after_transaction",
    # ":after_commit" and ":after_create" does not prevent
    # the ActiveRecord-Sidekiq race condition. In order to
    # prevent this just retry running JobWorker until it finds
    # the job by job_id.
    if retries <= MAX_RETRIES
      JobWorker.perform_async(job_id, retries+1)
    end
  end
end