Class: AsyncJob::Job

Inherits:
Delayed::PerformableMethod
  • Object
show all
Defined in:
lib/async_job/job.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, method, args, results_id) ⇒ Job

Returns a new instance of Job.



9
10
11
12
# File 'lib/async_job/job.rb', line 9

def initialize(object, method, args, results_id)
  super(object, method, args)
  @results_id = results_id
end

Class Method Details

.perform_async(user, object, method, *args) ⇒ Object



3
4
5
6
7
# File 'lib/async_job/job.rb', line 3

def self.perform_async(user, object, method, *args)
  AsyncJob::Results.create.tap do |results|
    Delayed::Job.enqueue(self.new(object, method, args, results.id))
  end
end

Instance Method Details

#performObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/async_job/job.rb', line 14

def perform
  job_results = AsyncJob::Results.find(@results_id)

  begin
    results = super
    job_results.success = true
    job_results.results = results
  rescue => error
    job_results.success = false
    job_results.results = [error.message, error.backtrace]
    Rails.logger.error "Error processing async job: #{error.message}\n#{error.backtrace.join("\n")}"
  end

  job_results.save
end