code-quality

How to debug a Sidekiq worker

Pry like debugger for Sidekiq

sidekiq
How to debug a Sidekiq worker

Are you looking to debug your sidekiq worker?

There is this gem called <a href=https://github.com/Mon-Ouie/pry-remote rel=“nofollow” target=“_blank”>pry-remote which stops all processes from running. And lets you debug just like with pry.

You will have to put binding.remote_pry in your code and you’ve got a stopping point.

Installation

gem install pry-remote

Usage

Here’s a program starting pry-remote:

  require 'pry-remote'
 
  class Foo
    def initialize(x, y)
      binding.remote_pry
    end
  end
 
  Foo.new 10, 20

Running it will prompt you with a message telling you Pry is waiting for a program to connect itself to it:

  [pry-remote] Waiting for client on drb://localhost:9876

You can then connect yourself using pry-remote:

  $ pry-remote
  From: example.rb @ line 7 in Foo#initialize:
        2:
        3: require 'pry-remote'
        4:
        5: class Foo
        6:   def initialize(x, y)
    =>  7:     binding.remote_pry
        8:   end
        9: end
      10:
      11: Foo.new 10, 20
  pry(#<Foo:0x00000000d9b5e8>):1> self
  => #<Foo:0x1efb3b0>
  pry(#<Foo:0x00000001efb3b0>):2> ls -l
  Local variables: [
    [0] :_,
    [1] :_dir_,
    [2] :_file_,
    [3] :_ex_,
    [4] :_pry_,
    [5] :_out_,
    [6] :_in_,
    [7] :x,
    [8] :y
  ]
  pry(#<Foo:0x00000001efb3b0>):3>

Alternative method:

You can also use pry in your code and call your your worker from rails console.

MyWorker.new.perform(some_args)

Did this work for you? Feel free to share in the comments below.

Happy Coding!

Reference: https://stackoverflow.com/a/43738392/4096120

About the author

Prakash Poudel Sharma

Engineering Manager · Product Owner · Varicon

Engineering Manager at Varicon, leading the Onboarding squad as Product Owner. Eleven years of building software — first as a programmer, then as a founder, now sharpening the product craft from the inside of a focused team.

Keep reading

More on this

Join the conversation0 comments

What did you take away?

Thoughts, pushback, or a story of your own? Drop a reply below — I read every one.

Comments are powered by Disqus. By posting you agree to theirterms.

0:000:00