Warmup Mongrels on deploy

... so your users don't have to

After each deployment with Capistrano, there's a slight delay on the first request to each of your Mongrel processes.

Any Libraries, GEMS, Models, Helpers, Controllers etc. not referenced by plugins or during config.inititalize ( and the callback variants ) in your environment is registered and loaded by the Rails Dependency mechanism on the first request.

This delay is relative to your Application size and the number of third party dependencies (2 seconds for me).

A Warmup task for your deployment recipe

CAUTION: The namespaces is Capistrano 2 specific, just drop them if you're using 1.x revisions.

This snippet would only execute IF

  • a configuration file ( mongrelcluster.yml ) exists in RAILSROOT/config
  • your production system has the Wget package installed ( standard on most Linux distros ).Modify this for your OS specific CLI HTTP client.

A single request is sent to each of your mongrel ports as defined in mongrel_cluster.yml. This may 404 depending on your application configuration ( subdomain based? ), in other words any status code is fine, 'Connection refused' not.

namespace :deploy do
  task :warmup, :roles => :app do
    require 'yaml'
     c = YAML.load_file(File.join('./', 'config', 'mongrel_cluster.yml')) rescue {}
     ((c["port"].to_i)...(c["port"].to_i + c["servers"].to_i)).each do |port|
       run 'wget localhost:%04d > /dev/null' % port  rescue 'No wget binary found'
       sleep(0.2)
     end unless c.empty?
  end
end

The call chain

I use the pattern below as my default App server deploy recipe.

namespace :deploy do
  task :default, :roles => :app do
    update
    compress_and_package_assets
    web:disable
    restart
    sleep(20)
    warmup
    sleep(2)
    web:enable
  end
end
  • Update code

  • Gzip and merge all JS and CSS assets

  • Disable App tier via Proxy

  • Fire up Mongrels and allow 20 seconds for them to do so

  • Warm them up

  • Sleep another 2 seconds for everything to settle down

  • Allow traffic via Proxy


About this entry