17

I am trying to use foreman to start my rails app. Unfortunately I have difficulties connecting my IDE for debugging.

I read here about using

Debugger.wait_connection = true
Debugger.start_remote

to start a remote debugging session, but that does not really work out.

Question: Is there a way to debug a rails (3.2) app started by foreman? If so, what is the approach?

jones
  • 668
  • 7
  • 23

2 Answers2

28

If you use several workers with full rails environment you could use the following initializer:

# Enabled debugger with foreman, see https://github.com/ddollar/foreman/issues/58
if Rails.env.development?
  require 'debugger'
  Debugger.wait_connection = true

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

  port = find_available_port
  puts "Remote debugger on port #{port}"
  Debugger.start_remote(nil, port)
end

And in the foreman's logs you'll be able to find debugger's ports:

$ foreman start
12:48:42 web.1     | started with pid 29916
12:48:42 worker.1  | started with pid 29921
12:48:44 web.1     | I, [2012-10-30T12:48:44.810464 #29916]  INFO -- : listening on addr=0.0.0.0:5000 fd=10
12:48:44 web.1     | I, [2012-10-30T12:48:44.810636 #29916]  INFO -- : Refreshing Gem list
12:48:47 web.1     | Remote debugger on port 59269
12:48:48 worker.1  | Remote debugger on port 41301

Now run debugger using:

rdebug -c -p [PORT]
knagode
  • 5,816
  • 5
  • 49
  • 65
luacassus
  • 6,540
  • 2
  • 40
  • 58
  • Clearly, you need more upvotes. Thanks to you, I *finally* got this to work. Thanks! – Jimmy Apr 10 '13 at 15:45
  • For completeness, the final step is to connect to the debugger in a separate shell with `rails -c -p [PORT]` – Philipp May 06 '13 at 23:45
  • 1
    @Philipp - Did you mean ```rdebug -c -p [PORT]```? Using the ```rails``` command did not work for me. – Tim Scott Jun 04 '13 at 18:10
  • @Tim, sorry about that -- yes, it should be `rdebug`. – Philipp Jun 12 '13 at 20:27
  • 2
    so, I have this working, however my debugger process gets killed every 30 seconds as I'm using Unicorn server and `timeout 30` in my unicorn.rb config file. i have tried several solutions to change the timeout if i'm in the development environment but it doesn't work. here's the code i'm trying: `if ENV['development'] timeout 300 else timeout 30 end`. Any ideas how to get this to work on Unicorn/Foreman without it timing out only in dev env? thanks! i upvoted – FireDragon Dec 27 '13 at 03:23
  • I solved the Unicorn timeout using `Rails.env.development?` instead of `ENV['development']` – Juanda Jan 17 '14 at 16:34
2

One approach is to require debugger normally in your gemfile, and add debugger normally in your code as needed. When the server hits that line, it will stop, but foreman won't be verbose about it. In your foreman console you can blindly type irb, and only then will you see a prompt appear. Bad UX, right?

Another (augmentative) approach is to tail your logs:

tail -f log/development.log

Hope this helps.

Galen
  • 636
  • 6
  • 12