Running ActiveJobs inline temporarily

Pushing execution of code that take a long time to complete to ActiveJob can be great to increase the (feeling of) responsiveness for any Rails application. Sometimes it can be desirable to run triggered jobs inline instead though (in certain rake tasks for example), you can do this by overriding the queue adapter with the inline adapter like so:

ActiveJob::Base.queue_adapter = ActiveJob::QueueAdapters::InlineAdapter.new

If you want to ensure that the adapter is switched back to its original setting after you are done (because more code is executed around the part where you want to run the jobs inline) you can that using ensure like in the below example rake task:

desc 'My rake task'
task my_task: :environment do
  old_queue_adapter = ActiveJob::Base.queue_adapter
  ActiveJob::Base.queue_adapter = ActiveJob::QueueAdapters::InlineAdapter.new
  # your code here
ensure
  ActiveJob::Base.queue_adapter = old_queue_adapter
end

Note that if you are using different queueing backends for specific jobs (as documented in the Rails guide here) you would have to override each one of those too in the same way (if you would want to run those inline that is).