简体   繁体   English

如何调试由工头启动的rails(3.2)应用程序?

[英]How to debug a rails (3.2) app started by foreman?

I am trying to use foreman to start my rails app. 我正在尝试使用工头来启动我的rails应用程序。 Unfortunately I have difficulties connecting my IDE for debugging. 不幸的是我连接IDE进行调试时遇到了困难。

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? 问题:有没有办法调试由工头启动的rails(3.2)应用程序? If so, what is the approach? 如果是这样,那么方法是什么?

If you use several workers with full rails environment you could use the following initializer: 如果您使用具有完整rails环境的多个worker,则可以使用以下初始化程序:

# 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]

One approach is to require debugger normally in your gemfile, and add debugger normally in your code as needed. 一种方法是在gemfile中正常需要调试器,并根据需要在代码中正常添加debugger 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. 在您的工头控制台中,您可以盲目地键入irb ,然后才会看到提示符。 Bad UX, right? 用户体验不好,对吗?

Another (augmentative) approach is to tail your logs: 另一种(补充)方法是拖尾日志:

tail -f log/development.log

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM