Friday, July 29, 2016

Git: Let's back it up

Say you are developing a new feature and you realize after few commits that you went off to a way different route that you suppose to and you need to back it up few commits and start over... this definitely would be a cleaner way vs trying to remove what was done manually. How to do it though?

Check out what you want and get rid of all that code...

git reset --hard 0d3b7ac32

Then you would push it up

git push origin +master
 
 
Pretty simple once you know it.

Thursday, July 28, 2016

Running rails server... iptables and binds...

Say you want to run your rail server... you type in the command and your rails application is up and running on localhost:3000

Great.

Now you want to access this server from other machine and suddenly you can't seem to access it? Proxy? Firewall?

Well, first things first: iptables. Make sure to add port 3000 to the list

Either edit iptables directly or just add to it. The choice is yours

Option 1:
sudo vi /etc/sysconfig/iptables

Option 2:
sudo iptables -I INPUT -p tcp --dport 3000 -j ACCEPT 
sudo service iptables save 
 
Now... when you start the server, by default it would bind to 127.0.0.1 address which is not accessing to the outside. Instead, you need to specify eth0 address that is externally accessible, like so

rails server -b <ip of eth0 - using ifconfig to find out what it is>

Hope this helps someone since i personally got stuck trying to figure out why my iptables were not allowing port 3000 to be seen when I had bind issues all along :)

Tuesday, July 26, 2016

Ruby on Rails: Bundle Install vs Update

Both can install gems that are specified in Gemfile, however bundle update would also update the gems to the latest version, if no version is specified in Gemfile, or will update it to the latest patch version (i.e. ~> 1.2.3 will update to the newest version of 1.2.x, it might be 1.2.4, 1.2.5, etc)

Bundle install on the other hand, would install the latest patch version of the gem if the gem is missing from the Gemfile, but won't do anything if it is already present.

So... bundle update might surprise you if something changed between patch version or make the gem incompatible with some other gem, hence caution must be taken with bundle install command.

And as a best practice, always specify versions in the Gemfile!

Monday, July 11, 2016

Value of print statements while debugging.

While I always smirk at System.out.println("Got here somehow..."); statements... there is some value to print statements in general.

Now... you should never ever use System.out print statements. They are easy to forget in your code and make others laugh at your print statements that sometimes have great level of profanity in them. The most proper way is to you one of the logging frameworks that would allow you great level of control (display all levels of logging including debug on development environment, while only displaying error level messages on production).

Personally, I favor stepping through my code via a debugger to see what's going on.

So when do I use print statements to debug? Counters...

While stepping through your code one by one might one on a single run, what do you do when you need to process thousands records? Are you going to do it manually? Think not.

The way to work with print lines here is to print out certain message, for example

"Retrieved record from database"
"Cleaned raw record"
"Record passed through process A"
"Exception occurred while processing A logic"
"Record passed through process B"
"Exception occurred while processing B logic" 
"Finished"

now, when your process finished running, you will have quiet of few of there records... Yes, you could have tried to declare global variable to give you number of results for each... but what if you are running multiprocessing program, what if <insert some type of complication here>. To make things easier, take generated records in the log and use good old grep command to count number of instances of that particular phrase in the log file.

This way you can generate statistics of
"Retrieved record from database" 1000
"Cleaned raw record" 1000
"Record passed through process A" 820
"Exception occurred while processing A logic" 180
"Record passed through process B" 620
"Exception occurred while processing B logic"  200
"Finished" 620

Now it is easy to see how much records you loose and where and now you can pint point the problem and get a bigger magnifying glass to the trouble spot and you did not have to run it through 1,000 times yourself by hand.

Hope this simple example showed the value of print statements and provided you with another way to troubleshoot your code!