Debugging a resque background job for rails 4.2 using rails console
As the title implies, this article assumes you are using rails 4.2 and resque for background jobs. I suppose it also assumes that you want to quickly debug a piece of code. To be clear, the best way to make sure your code doesn’t have issues is to make sure you have full test coverage. However, I had a problem recently that my test was saying was working but in production it wasn’t working. The only way I know to fix such a thing is to work through it with a debugger and fix and then write a test that recreates the situation. Hopefully this short tutorial can help you if you find yourself in a similar spot.
let’s get started..
First, stop any background workers you might have running so that you don’t process the job accidentally.
Next, If you are using ruby > 2.1 you’ll want to be using byebug for all of your debugging needs. Add it to your Gemfile.
Next run the following command from a terminal
Now that we have byebug we are ready to get started. Open up the offending code. In my case the background job was being executed via the following code.
The next thing you will have to do is trigger the action that queues up the background job so that you have at least one of the background job in your queue. Once that’s complete you should trigger the rails console to give yourself an interactive session.
When your job executes it will launch into the debugger from there you can inspect variables and follow the code execution path. Here is a cheatsheet to get you started.
If you found this article helpful then please leave a comment.