ruby-on-railsrails-activejobactiondispatch

Rails - how to use ActionDispatch::Integration::Session in ActiveJob


In Rails' console app is defined as

[1] pry(main)> app
=> #<ActionDispatch::Integration::Session:0x000000189028e8

Now, I have a simple job like:

class MyJob < ActiveJob::Base
  queue_as :low

  def perform
    app.get('/my/path', nil, {'Accept-Language' => "it"})
  end
end

If I call MyJob.perform_now I get

NameError: undefined local variable or method `app' for

How can I use app in a Rails' ActiveJob?


Solution

  • class MyJob < ActiveJob::Base
      queue_as :low
    
      def perform
        app = ActionDispatch::Integration::Session.new(Rails.application)
        app.get('/my/path', nil, {'Accept-Language' => "it"})
      end
    end