Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Tuesday, April 26, 2016

Debug into method from production irb

Debug into method from production irb


When I look for a problem, for example with a specific ActiveRecord object, I often find myself doing the following on my production system:

# RAILS_ENV=production bundle exec    irb(main)> article = Article.find(123)  => #
irb(main)> article.do_something(3) NoMethodError: undefined method `id' for nil:NilClass

Sometimes I can't reproduce, why the line article.do_something(3) throws an error, so I want to debug it directly on my server, in production mode.

The problem now is: How do I step into the method #do_something with the argument 3 on the object / instance article?

Of course, one could set a breakpoint in that method, reload production and let all their customers wait on that breakpoint till I'm done debugging... But that wouldn't be the best idea.

So, is there a way to debug into a method of a specific instance from a running irb / pry session? (both would be ok)

Answer by max for Debug into method from production irb


Debugging in your production server is a horrible idea. Coming from the PHP world we used to do it all time and things would blow up spectacularly and only if you where lucky was there a database backup.

Instead what you can do is setup a staging server - this would be a server which runs on the exact same platform with the exact same settings. You can even use cron together with pg_backups to mirror the production database daily so that you can try new features and even show them to collaborators. And if it blows up then its not the end of the world - and there is no risk of losing end user data.

But you might to consider that you actually should be writing tests instead of using breakpoints or logs. Write a test that replicates the issue and specifies the desired behavior. You can then play around with the code and you know from the test state if what your are doing is working.

Answer by Matt for Debug into method from production irb


You can use pry-debugger (through ruby 1.9.3) or pry-byebug (Ruby >= 2).

Both of these allow you to set breakpoints allowing you to interactively step through your code as it runs, inspecting variable values, method return values, etc.

How you'll manage this with production data I'm not exactly sure, but this will allow you to debug a particular method.

Answer by yozzz for Debug into method from production irb


You can go in simple way, and try do debug via puts. You can add as many puts inside your method as you need, before each line that possible may cause a problem

Answer by Nguyen Cindy for Debug into method from production irb


You can change code on your production to puts value you want to check in #do_something method and run irb to check it. Because we do not restart production server so do not worry about your change.

Answer by 23tux for Debug into method from production irb


After trying around and more googling, I think I found a solution that works for me.

  1. Log in to your rails console / irb / pry session
  2. Setup your case (e.g. load your models, require dependencies...), so that you can execute the code you want to debug in one line
  3. require 'byebug' (or require 'debugger' for older ruby versions)
  4. Now the interesting part: Put a debugger statement in front of the line you want to debug like this binding.pry; user.do_something or debugger; user.do_something
  5. Now you are in your debugger. Maybe you have to jump to the next line with next (or just n if you have shortcuts enabled) to step into your method.

Here is a complete example from our production system:

[1] pry(main)> require 'byebug'  => true  [2] pry(main)> user = User.find(2)    User Load (0.3ms)  SELECT `users`.* FROM `users` WHERE `users`.`id` = 2 LIMIT 1  => #  [3] pry(main)> user.full_name  NameError: undefined local variable or method address for #    [4] pry(main)> binding.pry; user.full_name    [68, 73] in /usr/src/app/app/models/user.rb     68:   end     69:      70:   def full_name  => 71:     "#{address.firstname} #{address.last_name}"     72:   end     73: end  (byebug)   


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.