Utilizing Maintenance Mode In Your Web App To Handle Your Ups & Downs
Maintenance Mode Yourself Before You Wreck Yourself

maintenance mode

We’ve all been in situations where we’d like to enable maintenance mode while we send out some new features or while we update data on production servers, but it’s not always straightforward.

In order to streamline this process it’s good to have a couple of methodologies on hand to help ease things along and today we’re going to be covering two very different approaches (a gem-based approach using Turnout and a code-based DIY approach).

First, we’ll turn our attention to the conventional approach using Turnout.

To get started, you’ll first need to add the gem to your Gemfile

gem 'turnout'

and then bundle install

bundle install

Then, to start maintenance mode, simply run

bundle exec rake maintenance:start

from your command line

Then, to end maintenance mode, simply run

bundle exec rake maintenance:end

from your command line

See how easy life is when you play conventionally with Ruby on Rails?

You can also spice up your maintenance page by specifying a reason with your start call like so

bundle exec rake maintenance:start reason="Danger Will Robinson, Danger!"

and that will insert your reason into the page loaded by Turnout

If you’ve been following along then you’ve noticed that, even with a customized reason, the default page is still a bit lackluster in terms of style and pizzazz, but don’t worry! You can adjust the look and feel of the page by adding your own customized maintenance page into your /public directory (just be sure that you enable all of your styling and graphics!).

There are a couple of other options available, too, so take a look at the github page for more information.

Second, we’ll dive into a method that gets your hands a bit dirtier.

Sometimes you don’t want to add another gem to your codebase, or you don’t want to deal with enabling a rewrite in .htaccess, or you simply don’t want to deal with all of that other hoopla.

Another option, that throws a lot of convention into the wind, is to do the following:

Modify your Application controller (application_controller.rb) to have a before filter to trigger the maintenance check like so:

before_filter :handle_maintenance_mode, :except => "/maintenance"

Then, enable the handle_maintenance_mode method in the Application controller:

def handle_maintenance_mode
    if ENV["SYSTEM_IS_DOWN_FOR_MAINTENANCE"] and request.path != "/maintenance"
        redirect_to "/maintenance"
    end
end

Next, you’ll need to create a named route and endpoint for the maintenance path like so:

#in routes.rb
get "/maintenance" => "home#maintenance"
#in home_controller.rb
def maintenance
    redirect_to :root if !ENV["SYSTEM_IS_DOWN_FOR_MAINTENANCE"]
end

In the maintenance method you can feel free to set any variables you need or perform any other functions as necessary.

Lastly, you’ll just need to create your maintenance view in the proper location based on your route and controller action (for this example, it would be in app/views/home).

In order to “turn on” maintenance mode using this method all you need to do is set the proper ENV variable (see our post on ENV variables and security) and restart the server (the same process is used to “turn off” maintenance mode).

Now, while this may not be the “ideal” method (it doesn’t return the 503 error code for one), it does have a few perks: mainly, generating a styled maintenance page within the style guidelines of your app is outrageously simple because you have access to all CSS, assets, and JavaScript using standard Ruby on Rails conventions (no need to deal with the public folder or anything along those lines).

And there you have it, your maintenance free path to enabling maintenance mode — pick your poison and have fun out there!