Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Byebug with Sidekiq and Foreman

I have a rails application in which I use foreman to start my rails and sidekiq servers. Since foreman doesn't interact well with regular byebug (you can't see the prompt as you type), I have set up remote debugging for both my rails and sidekiq servers. This works perfectly for the rails server, but when I connect to the byebug server for the sidekiq server, I get the following:

$ bundle exec byebug -R localhost:58501
Connecting to byebug server localhost:58501...
Connected.
(byebug:ctrl)

And I'm unable to catch any byebug breakpoints.

According to the documentation, the (byebug:ctrl) prompt means that the program has terminated normally (https://github.com/deivid-rodriguez/byebug/blob/master/GUIDE.md), but sidekiq is running jobs just fine.

Is there something incorrect in my configuration, or is sidekiq just not compatible with byebug's remote debugging?

Procfile:

sidekiq: bundle exec sidekiq
rails: rails server

config/initializers/byebug.rb:

if Rails.env.development?
  require 'byebug'

  def find_available_port
    server = TCPServer.new(nil, 0)
    server.addr[1]
  ensure
    server.close if server
  end

  port = find_available_port

  puts "Starting remote debugger..."
  Byebug.start_server 'localhost', port
  puts "Remote debugger on port #{port}"
end

Note that when I don't use remote debugging, byebug functions fine with sidekiq (although in foreman I can't see the prompt as I type).

Also note that I've tried using Byebug.wait_connection = true before Byebug.start_server, but I have the same issue.

like image 767
Tommy Steimel Avatar asked Jan 05 '16 17:01

Tommy Steimel


1 Answers

I've tried to replicate this locally, and with sidekiq 3.3.1 and byebug 9.0.5, it seems to work fine with a minor adjustment to the require:

require 'byebug/core'

def find_available_port
  server = TCPServer.new(nil, 0)
  server.addr[1]
ensure
  server.close if server
end

port = find_available_port

puts "Starting remote debugger..."
Byebug.start_server 'localhost', port
puts "Remote debugger on port #{port}"

Job:

class TestJob
  include Sidekiq::Worker

  def perform
    byebug
  end
end
like image 107
Codebeef Avatar answered Nov 10 '22 11:11

Codebeef