Class: Lev::ActiveJob::Base

Inherits:
Object show all
Defined in:
lib/lev/active_job/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#provider_job_idObject

Returns the value of attribute provider_job_id.



4
5
6
# File 'lib/lev/active_job/base.rb', line 4

def provider_job_id
  @provider_job_id
end

Instance Method Details

#perform(*args, &block) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/lev/active_job/base.rb', line 38

def perform(*args, &block)
  # Pop arguments added by perform_later
  id = args.pop
  routine_class = Kernel.const_get(args.pop)

  routine_instance = routine_class.new(routine_class.find_status(id))

  routine_instance.call(*args, &block)
end

#perform_later(routine_class, options, *args, &block) ⇒ Object



6
7
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
33
34
35
36
# File 'lib/lev/active_job/base.rb', line 6

def perform_later(routine_class, options, *args, &block)
  # Create a new status object
  status = routine_class.create_status

  # Push the routine class name on to the arguments
  # so that we can run the correct routine in `perform`
  args.push(routine_class.to_s)

  # Push the status's ID on to the arguments so that in `perform`
  # it can be used to retrieve the status when the routine is initialized
  args.push(status.id)

  # Set the job_name
  status.set_job_name(routine_class.name)

  # In theory we'd mark as queued right after the call to super, but this messes
  # up when the activejob adapter runs the job right away (inline mode)
  status.queued!

  # Queue up the job and set the provider_job_id
  # For delayed_job, requires either Rails 5 or
  # http://stackoverflow.com/questions/29855768/rails-4-2-get-delayed-job-id-from-active-job
  provider_job_id = self.class.send(:job_or_instantiate, *args, &block)
                              .enqueue(options)
                              .provider_job_id
  status.set_provider_job_id(provider_job_id) \
    if provider_job_id.present? && status.respond_to?(:set_provider_job_id)

  # Return the id of the status object
  status.id
end