How can I run an ActiveJob in Rails console for debugging?

天涯浪子 提交于 2019-12-23 08:49:39

问题


I currently have an ActiveJob that I've created and use Sidekiq to queue it. I'm wanting to debug the job, but for me to see any messages I program into it I have to check my log files. I feel like it would be more convenient to be able to see my puts messages in my job in the Rails Console. When I run the perform_later method though in rails console it just queues the job up and I never see the messages in console. Is there a way to make it where I will see them in the console?


回答1:


You can run a job with perform_now.

For example...

class Foo < ActiveJob::Base
  def perform(arg1)
    puts "Hello #{arg1}"
  end
end

Foo.perform_now('world')



回答2:


You can temporarily set your queue adapter to inline.

Right now your code in application.rb will look something like this:

Rails.application.config.active_job.queue_adapter = :sidekiq

Just comment out the line

# Rails.application.config.active_job.queue_adapter = :sidekiq

This will run your job inline, and you should see the results in the console.




回答3:


There is a configuration line you can add in development.rb

require 'sidekiq/testing/inline'

This should enable inline testing for development.



来源:https://stackoverflow.com/questions/34519414/how-can-i-run-an-activejob-in-rails-console-for-debugging

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!