How to use ruby-debug-ide with unicorn_rails?
First you need to install Ruby
extensionAdd following gems in your Gemfile:
gem 'debase'
gem 'ruby-debug-base', :platforms => [:jruby, :ruby_18, :mingw_18]
gem 'ruby-debug-base19x', '>= 0.11.30.pre4', :platforms => [:ruby_19, :mingw_19]
gem 'ruby-debug-ide' , "~>0.6.1"
Then you need to let rdebug-ide know you are using unicorn (multi-process app) by providing the --dispatcher-port
option. Please take a look at rdebug-ide file to see all the available options.
--dispatcher-port: It is a same port that you will use to run unicorn. In your case 3010
.
So it should look like this:
bundle exec rdebug-ide --debug --port 1234 --dispatcher-port 3010 -- vendor/bundle/ruby/2.6.0/bin/unicorn -E "develop_against_staging" -p 3010 -c "${PWD}/config/unicorn.rb
Running above command alone wont start the debugging, infact your Unicorn server wont be starting yet. When looking at the logs after running the above command in the terminal window you will notice a message something like this
Fast Debugger (ruby-debug-ide 0.6.1, debase 0.2.4.1, file filtering is supported) listens on 127.0.0.1:1234
The logs telling us rdebug-ide is ready to be connected at port 1234. Create a launch.json file if it is not already created and add this configuration.
{
"version": "0.2.0",
"configurations": [
{
"name": "1234 Listen for rdebug-ide",
"type": "Ruby",
"request": "attach",
"remoteHost": "127.0.0.1",
"remotePort": "1234",
"remoteWorkspaceRoot": "${workspaceRoot}",
"cwd": "${workspaceRoot}"
}
]
}
Once you add the entry, go ahead and click on Play
Image here button to start debugging.
Now that your unicorn server is started, if you try to access your application you wont be able to access it because the worker process haven't started yet.
Continue to look at logs carefully you will notice122: Ide process dispatcher notified about sub-debugger which listens on 34865
. This is telling us a new sub-debugging process is started on port 34865
. This is a randomly generated port find_free_port.
Note: There will a one port per unicorn-worker.
Once you see the above log add another entry into your launch.json file and copy the newly generated port into the file. Like this
{
"version": "0.2.0",
"configurations": [
{
"name": "1234 Listen for rdebug-ide",
"type": "Ruby",
"request": "attach",
"remoteHost": "127.0.0.1",
"remotePort": "1234",
"remoteWorkspaceRoot": "${workspaceRoot}",
"cwd": "${workspaceRoot}"
},
{
"name": "34865 Listen for sub-rdebug-ide",
"type": "Ruby",
"request": "attach",
"remoteHost": "127.0.0.1",
"remotePort": "34865",
"remoteWorkspaceRoot": "${workspaceRoot}",
"cwd": "${workspaceRoot}"
}
]
}
Once added, select the new configuration and click play button. If you had set number of worker to one only in your unicorn.config file, you should see the the log something like this.I, [2022-07-13T19:44:26.914412 #122] INFO -- : worker=0 ready
. Now put a breakpoint and start using your application, it will break once it reach that code path.
If you have successfully setup everything and got to this point, there will be some gotcha that you may need to deal with.
- Worker timing out
- Re-linking to sub-debugger with different random port.
- ...
This complexity is because of unicorn master-worker design.
Answering this in a bit rush, please let me know if you have any questions. I apologies if I made this more confusion for you.
Could not find 'ruby-debug-ide' in VS Code
In my case, the solution was quite simple. As a newbie in Ruby on Rails, I did not realize that instead of installing gems into Ruby root, I should specify them in my Gemfile. So, inside Gemfile I put only:
group :development do
gem 'ruby-debug-ide', '0.6.0'
gem 'debase', '0.2.1'
gem 'web-console', '~> 2.0'
gem 'spring'
end
and this solved the issue for me.
ruby-debug-ide command exploading in my face
Ok so using gem list and passing grep debug I found I had some odd gems in my box
$ gem list | grep debug
debug_inspector (0.0.3)
debugger-linecache (1.2.0)
debugger-ruby_core_source (1.3.8)
ruby-debug-base19 (0.11.25)
ruby-debug-base19x (0.11.32)
ruby-debug-ide (0.7.2)
ruby-debug19 (0.11.6)
Note the two ruby-debug-base19 thingies? yeah, I suspected that might be a problem along with a few others. So doing some gem uninstall I end up with...
$ gem list | grep debug
debug_inspector (0.0.3)
debugger-ruby_core_source (1.3.8)
ruby-debug-base19x (0.11.32)
ruby-debug-ide (0.7.2)
Now when I run my debug command for rubymine it works
rdebug-ide --host 0.0.0.0 --port 1234 --dispatcher-port 26162 -- rails s
Fast Debugger (ruby-debug-ide 0.7.2, ruby-debug-base19x 0.11.32, file filtering is not supported) listens on 0.0.0.0:1234
Woo Hoo!
Related Topics
How to Break Out of a Map/Collect and Return Whatever Has Been Collected Up to That Point
Gemfile.Lock Always Has Changes Not Staged for Commit
Converting External CSS to Inline CSS for Mail in Rails
"Ruby.Exe Is Not Recognized as an Internal or External Command" in Windows 7
How to Split a Directory String in Ruby
How to Replicate Class_Inheritable_Accessor's Behavior in Rails 3.1
What Is The Logic Behind This Result
Ruby on Rails Active Record Return Value When Create Fails
Installing Rails with Gem, Error Loading Command: Install Undefined Method 'Invoke_With_Build_Args'
Ruby Yaml Parser by Passing Constructor
Git Post-Receive Hook Not Running Bundle Install
Ruby Syntactic Sugar: Dealing with Nils