Cyril Duchon-Doris
Cyril Duchon-Doris

Reputation: 14039

Rails change ActiveJob adapter for a single job

I have some scheduled tasks that run jobs with a real queue service.

Now, I want to be able to run those tasks manually from a dashboard, wait for its execution, and read/process some of its outputs in a controller action. The typical case being when an admin wants to manually reload some statistics, and it's fine to stall the server for a couple secs.

Is there a way to change the ActiveJob adapter to "inline" in order to do that ? If possible I'd also like to read some artifacts generated by this job (could be instance variables, or the return value of the performaction).

Any way to do that ?

Sample Job

class TopLevelJob < ApplicationJob
  def perform
    some_iterator.each do 
      SomeNestedJob.rand.perform_later
      # There are several types of sub-jobs that can be called
      # Passing a flag param (perform vs perform_later) would be kind of annoying
    end
end

module SomeNestedJob
  class A < ApplicationJob; 
    def perform
       # May in turn spawn an other job with perform_later
    end
  end
  ...
  class Z < ApplicationJob; ...; end

  def self.rand
    [A..Z].sample
  end
end

Upvotes: 1

Views: 618

Answers (1)

Canardgarou
Canardgarou

Reputation: 41

I believe "perform_now" is what you're looking for ... runs the task right away, without going in queue.

cf. ActiveJob on http://edgeapi.rubyonrails.org/classes/ActiveJob/Base.html

Other than that, really, just make your "perform" have a flag ("has_whatever_output_you_have_in_mind") and use it in your perform action to trigger an output.

Upvotes: 0

Related Questions