Konrad Reiche About Photos Talks

Debugging Sidekiq With Pry

Sidekiq is a Redis-backed Ruby framework for moving asynchronous tasks into background processes. Using threads instead of forks it claims to be much more memory efficient in contrast to Resque. Redis, a key-value store, is used for storage of the jobs. The Sidekiq server processes messages as a multi-threaded process.

This setup might come in handy for the production environment, but if you are developing for your own it becomes cumbersome to start Sidekiq and Redis every time you want to put your code to use. Another disadvantage is the lacking capability for debugging. Invoking Pry at runtime by writing

require 'pry'; binding.pry

will not have any effect. The Sidekiq server will not start Pry when executing the statement. This problem, however, can be addressed with the Sidekiq inline infrastructure.

# The Sidekiq inline infrastructure overrides perform_async so that it
# actually calls perform instead. This allows workers to be run inline in a
# testing environment.

You can simply use the inline infrastructure by requiring it like so:

require 'sidekiq'
require 'sidekiq/testing/inline'

As expected Pry will start in the environment which calls the perform method, now synchronously. If your job does not have to be run in a threaded environment to test concurrency or for performance critical reasons running Sidekiq inline will just be sufficient.