Last Updated: February 25, 2016
·
491
· protoswype

Faster Rails-Application start

After deploying an Rails-App you usually want to restart your application. While this part is fine, the first page-load can take quite some time. If you're always the first one who calls the page, this might be acceptable but not if it is an customer. I like to add a commad to my deployment workflow in order to make sure the first call has already been made.

The basic idea is using wget or being more specific: wget --spider {app-url}. In this case {app-url} specifies your Application-URL on which you'll call the deployed app, like example.com/my-great-app

While wget {app-url} downloads a page via the command line, wget --spider {app-url} just makes the request without downloading the page (http://www.gnu.org/software/wget/manual/html_node/Download-Options.html). Of course, both techniques will do the job, but you don't want an extra download of your page, right?

For me, I'm using Capistrano and Phusion Passenger. So basically the application will be restarted on each deploy by touching /tmp/restart.txt.
To automate the wget-command you can add wget --spider {app-url} to the "[...] Here we can do anything [...]"-part of the auto-generated Capistrano-template (example: https://github.com/capistrano/capistrano/blob/bb2ed22893f1116face0c39fc24c5e0792e86a26/lib/capistrano/templates/deploy.rb.erb#L51).

after :restart, :clear_cache do
   on roles(:web), in: :groups, limit: 3, wait: 10 do
    execute "wget --spider {app-url}"
   end
 end

Of course, you can put this line in whatever task you like. I just want to give a basic example.

If you're using something like capistrano-maintenance (https://github.com/capistrano/maintenance), you can actually set things up, that visitors will still see the the maintenance page, while your app is loading.

Happy Coding!