How can I debug code running in a delayed_job task in the IRB console?

13,506

Solution 1

Start a standard rails console

ruby script/console

And start a worker inside here, this will see and trigger the debugger statement.

worker = Delayed::Worker.new
worker.start

Solution 2

I use pry as my console and remote debugger. Pry website here, Pry rails gem here. In your code, you add binding.pry statements to have your app stop executing and open the console. It works the same with delayed_job as it does with your rails app. Make sure you are running delayed_job in the foreground though, so it is still attached to the terminal. E.g., start delayed_job with:

rake jobs:work
Share:
13,506

Related videos on Youtube

s01ipsist
Author by

s01ipsist

Updated on June 04, 2022

Comments

  • s01ipsist
    s01ipsist almost 2 years

    I have a background task that runs using delayed_job.

    I can see that it does run from the logging statements. It does not seem to have the correct result, compared to running it in the foreground, so I want to debug it in the IRB console.

    I am running the background task with

    rake jobs:work
    

    and it does not trigger the debugger statement.

    How can I load the debugger?

  • Gaston Morixe
    Gaston Morixe over 9 years
    Delayed::Worker.new.start
  • Mapad
    Mapad over 9 years
    Actually worth noting that if you've added the breakpoint (binding.pry instruction) after you've run the queue, then you must first stop & relaunched the queue with rake jobs:work so that the breakpoint is taken into account
  • pixeltom
    pixeltom over 8 years
    bundle exec rails runner "Delayed::Worker.new.start"
  • Greg Blass
    Greg Blass over 6 years
    This is amazing. All this time I was in the dark on console output using a delayed job. Thank you!