Tag: capistrano

Apache (Passenger) Maintenance mode for Ruby on Rails application with Capistrano

Same of Nginx and Puma: Nginx (and Puma behind) maintenance mode for Rack/Rails applications with Capistrano

There is a time, when we need to switch our apps into maintenance mode. Maybe it is because of some data processing stuff, maybe because of backups, Capistrano or whatever good reason you might have. To be honest it doesn't matter why. What does matter, is how we should handle working users of our apps. Of course all the downtimes should take place when there is the smallest amount of users online. In most cases it might be a good idea to switch application off in the middle of the night (or on Sunday, etc.), but this won't solve our primary problem: what should we show users that are already online?

The worst scenario ever would be showing them nothing (for example by shutting down whole application server). Users probably will think, that something bad happened. Much better idea is to show users a maintenance page with some sort of information like "Temporary down for maintenance". It would be even better, it such a page would automatically show when needed.

To do so, we can use Apache Mod Rewrite module and a static 503.html page.

Mod Rewrite for Maintenance mode detection

How to determine if we are in maintenance mode? Let's check if maintenance.txt file exists in tmp/ dir of our app:

RewriteCond %{DOCUMENT_ROOT}/../tmp/maintenance.txt !-f

When it exists, we need to redirect user to our 503.html static page:

RewriteRule ^(.*)$ /503.html [NC,R,L]

Of course the whole .htaccess should include also enabling RewriteEngine, ignoring redirects of CSS files and redirecting from 503.html to root, when the maintenance is off:

RewriteEngine On
# Set error 503 static page
ErrorDocument 503 /503.html

# Don't redirect when someone requests assets used in 503.html
RewriteCond %{REQUEST_URI} !^/assets/layouts/portal/favicon.ico$
RewriteCond %{REQUEST_URI} !^/assets/libraries/bootstrap/bootstrap.min.css$
RewriteCond %{REQUEST_URI} !^/assets/layouts/portal/application.css$
RewriteCond %{REQUEST_URI} !^/503.html$
RewriteCond %{DOCUMENT_ROOT}/../tmp/maintenance.txt -f
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
# Set 503 status for all requests
RewriteRule ^(.*)$ /503.html [NC,R=503,L]

# When it is not maintenance mode redirect to root_path from 503.html
RewriteCond %{DOCUMENT_ROOT}/../tmp/maintenance.txt !-f
RewriteCond %{REQUEST_URI} ^/503.html
RewriteRule ^503.html http://www.project.domain/ [R=302,L]

Of course, you need to remember to create your own 503.html file, put it in public/ dir of your project and customize all the htaccess rules based on your needs.

Capistrano hookup

To automate enabling and disabling my projects, I use a set of simple Capistrano tasks, enclosed in an Apache namespace:

namespace :apache do

  desc 'Restarts the current Passenger project'
  task :restart do
    run "touch #{current_path}/tmp/restart.txt"
  end

  desc 'Sets project server in dev mode - so the 503 page is served'
  task :lock do
    run "touch #{current_path}/tmp/maintenance.txt"
  end

  desc 'Sets project to a standard mode'
  task :unlock do
    run "rm -f #{current_path}/tmp/maintenance.txt"
  end

end

Usage example:

before 'deploy:update' do
  apache.lock
end

after 'deploy:update' do
  apache.restart
  apache.unlock
end

That's all. Good luck and as few maintenance downtime as possible! P.S. With a bit of modifications, this code might be used also for PHP/Python Passenger based projects.

Clear memcached without restart with Ruby and Capistrano (Rake task)

After successful Capistrano update (cap deploy) if we use Memcached - we should clear it. How can we do it without root access to the server? There are two ways to clear memcached without restarting it:

  • Memcached command: flush_all
  • Rails.cache.clear

One flush to rule them all - flush_all

If we have a dedicated server with one application on it - we can clear whole memcached memory, to do so, we create a rake task (/lib/tasks/memcached.rake):

require 'socket'

namespace :memcached do
  desc 'Flushes whole memcached local instance'
  task :flush do
    server  = '127.0.0.1'
    port    = 11211
    command = "flush_all\r\n"

    socket = TCPSocket.new(server, port)
    socket.write(command)
    result = socket.recv(2)

    if result != 'OK'
      STDERR.puts "Error flushing memcached: #{result}"
    end

    socket.close
  end
end

Usage:

bundle exec rake memcached:flush

It is worth mentioning, that this task doesn't require the Rails environment to be loaded. If it goes about the server address and port - you can always modify it so it will accept the env settings instead of hardcoding it.

Be aware, that this command will clear out all the data that is stored in Memcached instance, even the data that was used by other applications (other than our). If you want to clear out data used by one of many apps that are using same Memcached server, see the solution presented below.

Clearing single Rails app memcached data - Rails.cache.clear

Apart from flushing all the data that is in Memcached, we can always clear only the Rails cache by creating a really simple rake task (/lib/tasks/memcached.rake):

namespace :memcached do
  desc 'Clears the Rails cache'
  task :flush => :environment do
    Rails.cache.clear
  end
end

The execution process is exactly like in the previous case:

bundle exec rake memcached:flush

In this Rake task we do load the Rails environment (because we want to use Rails.cache instance). In multi application environment, this Memcached cleaning method seems way better because we work with our application scope only.

Capistrano task for clearing Memcached

So we have our rake task, but it would mean nothing without a Capistrano hookup:

namespace :memcached do

  desc "Flushes memcached local instance"
  task :flush, :roles => [:app] do
    run("cd #{current_path} && rake memcached:flush")
  end

end

Now we can use it like this:

bundle exec cap memcached:flush

Or we can hookup it to update process:

after 'deploy:update' do
  memcached.flush
end

Copyright © 2024 Closer to Code

Theme by Anders NorenUp ↑