Posts tagged ROR

11 Things to Consider Before Deploying Your Rails Application


Cross Posted from http://vinsol.com/blog

At VinSol, we have been developing and deploying Rails applications for more than four years. During this period, we have identified some best practices that we prefer to follow while deploying rails application to production server.

Below is the checklist of these practices:

 

1. Ensure that NS records and MX records are changed if they need to be changed

Changing nameservers will point the domain to the hosting server, and changing MX records will redirect incoming mails to the mail server. As a very first step, we should make sure that name servers of the domain are set to be the correct one. Changing MX record is a must if our application is parsing incoming mails or we wants to use other mail services for e-mail exchange, for example Gmail.

 

2. Ensure some backup mechanism in place for both data as well as user uploaded content like images/documents etc.

Since production data is very critical, we must setup backup mechanism. It could be some type of scheduled task that takes periodic backup of all critical data, Or it could be some type of backup service provided by hosting company. When we talk about critical production data, it includes production DB, content generated by application users like images, documents, etc.

 

3. Ensure database indexes

We might have done development without having proper database indexes, but we should avoid going to production without them. Adding indexes might slow down insert queries a bit but it increases the performance of read queries. It applies when application in production has percentage of read operations much more than write operations.

 

4. Enable your slow query log

This is specific to MySQL. Enabling slow query log allows MySQL to log slow running queries to a file. And this log can be used to find queries that take a long time to execute and are therefore candidates for optimization.

 

5. Ensure exception capturing is in place

We might want to be notified when something bad happens to our application. There are several hosted services available who receive and track exceptions, for example Hoptoadapp.com, GetExceptional.com etc… Either we can choose one from these hosted services or we can use “exception notifier” plugin.

 

6. Ensure adding entries for cron/scheduled jobs

Most of the applications have some functionality/jobs that need to be run periodically, for example generating invoices, sending newsletters etc. In most cases these jobs are done by a rake task. We should make sure that we have added such jobs to cron or similar program.

 

7. Monitoring important processes

To ensure that our site is up 24×7 we need to ensure that all processes that our application needs are up. There can be many processes like MySQL, Mongrel, Apache etc.. These processes are very important as our application directly depends on them. For example if MySQL process get killed accidentally, our application would not be able to connect to MySQL and will start throwing exceptions.

We can choose any of the available monitoring tools like God, Monit, 24×7 etc…

 

8. Ensure confidential data filtering

We would never like to leak/share confidential information of our application users. We should make sure that none of the user’s confidential data like SSN, Credit card info, password are being written to log files. We might not have paid much attention on this while developing the application.

 

9. Rotate log files

Once our site is up and running, every single request write some text in log file. And hence size of the log file keeps on increasing. Larger log files can put us in trouble if we get it beyond certain size. Its difficult to manage these log files, as larger files need more memory to open and need more time to download. In one of the rescue project we did , the log file size was 3GB.

We would recommend having logrotate setup for the application.

 

10. Setup Asset Host

Setting up asset hosts can reduce loading time by 50% or more. We must setup asset hosts for our application. Once asset hosts are all set, our static files will be delivered via asset hosts for example asset1.hostname.com, asset2.hostname.com

 

11. Clearing up stale sessions

We should make sure we should not left any stale session on the server. If our application is using DB or file system as session store, we must add a schedule task to delete stale sessions.

These are some of the points we have identified from our past experience and we might be missing some. Feel free to always add them as comments, and I’ll keep this post updated.


Akhil is a senior software engineer working with Vinsol for last 5 years. He is an inhouse deployment ninja.

 

 

We also provide affordable rails deployment services.

 

 

Displaying information about your git repository

Want to see information about your git repository?

Based on a discussion here Duane Johnson wrote a very useful bash script. I am using this script from months and would like to share with you. You can download this script here(git-info.txt (232)).

You can also add an alias like below, so that I can be a accessed by a single command “gitinfo”

alias gitinfo="/home/akhil/git-info.txt"

When you run this script from your working copy it displays:

  • Remote URL
  • Remote Branches
  • Local Branches
  • Configuration (.git/config)
  • Most Recent Commit

Isn’t it useful, give it a try ;-)

Taskit: Another task scheduler for rails

While searching something I found and interesting plugin ‘Taskit‘, which I want to test for sure in production/staging when ever I get the chance.

Anybody tried it already?

Printing large image diagrams generated by RailRoad

I use RailRoad for generating Ruby on Rails diagrams, but always wish I could print those diagrams. Generally diagrams are too big to print on a single A4 size paper and I didn’t find any tool to print larger images in parts so that I can join them. If I print the image generated by RailRoad on single page, it of no use as it is hardly readable.

Fortunately, two days ago I found something which resolved this issue. It is a linux command and print a particular image on four pages. Here is that command:

lp -o scaling=200 models.png

Hassle free installation of rails stack on debian based system

Want to install rails stack on a machine? Just follow these steps. It will setup a rails stack(Apache + passenger + mysql + ruby + rubygems + common gems + git) on any server(debian based)

  1. apt-get update
  2. apt-get upgrade -y
  3. apt-get -y install build-essential libssl-dev libreadline5-dev zlib1g-dev
  4. apt-get -y install mysql-server libmysqlclient15-dev mysql-client
  5. apt-get -y install ruby ruby1.8-dev irb ri rdoc libopenssl-ruby1.8
  6. install rubygems manually:
    1. download rubygems form rubyforge, >=1.3
    2. unzip files
    3. ruby setup.rb
    4. Check that gem command is in path. Sometimes ‘gem1.8′ is available but ‘gem’ not. In that case copy /usr/bin/gem1.8 to /usr/bin/gem using “cp /usr/bin/gem1.8 /usr/bin/gem”
  7. apt-get -y install libmagick9-dev
  8. apt-get -y install imagemagick
  9. apt-get -y install postfix mailx
  10. apt-get -y install apache2
  11. apt-get -y install apache2-prefork-dev
  12. wget http://webonrails.com/wp-content/plugins/download-monitor/download.php?id=7
  13. ruby install_gems.txt
  14. passenger-install-apache2-module
  15. Download git from git-scm.com

    1. Unzip files
    2. ./configure –without-tcltk
    3. make -j 2
    4. make install

You are all set now, go deploy you rails app. I have tested it on linode(ubuntu8.10), slicehost(ubuntu 8.10), should work for you too

Edge rails: ActiveRecord::Base.each and ActiveRecord::Base.find_in_batches for batch processing

You guys must faced a situation when you need to process large number of records, for example sending newsletters. Then you must have done some sort of batch processing to save it eating up all of your memory. This feature is committed to rails core by DHH.

Now you can do something like User.each and User.find_in_batches.

Please refer here for more details.

Rails Plugin: role_requirment, Clean role-based security for restful_authentication

I just found a very useful plugin role_requirement to manage roles in rails app.

RoleRequirement focuses on a simple approach to role-based authentication. RoleRequirement leverages the power of !Ruby to strike a marvelous balance between simplicity and flexibility.

Features:

* A user can have many roles or one role
* Full test helpers to make it easy to test your controllers.
* Squeaky clean implementation – don’t repeat yourself!
* Code generators: spend more time coding and less time wading through installation instructions.

rake -T does not list my custom rake task

This is just a tip, may be you guys are already aware of it.

Sometimes I wonder why my custom rake task doesn’t appear in rake -T list, however it runs well. I just noticed that if I do not specify description for a task then that particular rake task does not appear in the task list.

Tip: Open DB shell/console from rails root dir

Just a quick tip you might be using this already.

If you guys want to open your app’s DB shell. Then you can use rails utility ‘dbconsole’ by issuing “script/dbconsole” from rails root directory.

It will ask for DB password, and open your db shell.

If you use sake, you may like following sake task:

desc 'Launches the database shell using the values defined in config/database.yml'
task 'db:shell', :needs => [ 'environment' ] do
  config = ActiveRecord::Base.configurations[(RAILS_ENV or "development")]
  command = ""
  case config["adapter"]
  when "mysql" then
    (command << "mysql ")
    (command << "--host=#{(config["host"] or "localhost")} ")
    (command << "--port=#{(config["port"] or 3306)} ")
    (command << "--user=#{(config["username"] or "root")} ")
    (command << "--password=#{(config["password"] or "")} ")
    (command << config["database"])
  when "postgresql" then
    puts("You should consider switching to MySQL or get off your butt and submit a patch")
  else
    (command << "echo Unsupported database adapter: #{config["adapter"]}")
  end
  system(command)
end

quick_scopes: Rails plugin to automatically add some quick named_scopes to your models

Today I came across a new plugin named quick_scopes, and thought you guys might be interested to give it a try. Checkout http://github.com/internuity/quick_scopes/tree/master