How do I start Rails with foreman on a dedicated debug port with Rails 4.2?

503 views Asked by At

I’m using Rails 4.2. I start Rails using foreman, and I would like to attach a debugger when starting Rails. This question details the process for Rails 3.2 — How to debug a rails (3.2) app started by foreman?, however I believe this file is outdated

$ cat config/initializers/start_debugger.rb
# 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

Since I don’t think the “debugger” gem is supported by Rails 4.2. How would I start Rails using foreman on a dedicated debug port using Rails 4.2?

1

There are 1 answers

2
Saiqul Haq On

based on your comment, you said that you open to use byebug so this is the guide to set it up for Foreman

edit config/environments/development.rb

require "byebug/core"
port = ENV['DEBUGGER_PORT'] # any port that you like
Byebug.wait_connection = true
Byebug.start_server("localhost", port)

let say you would like to debug your controller

def index
  @posts = Post.all
end

and you want to see @posts value then just add byebug after the @posts

def index
  @posts = Post.all
  byebug
end

and do request to your #index endpoint rails server will be paused on byebug line you need to execute byebug -R localhost:8899 in another terminal


if you see this one https://github.com/deivid-rodriguez/byebug/blob/master/lib/byebug/remote/server.rb#L30 . it uses TCPServer too. so it's similar to your own implementation.

your foreman setting can read ENV['DEBUGGER_PORT'] too