Warning users that the site is coming down for maintenance

Hi. I have a site that is currently running in alpha mode. I have
warned my users that it will be bouncing up and down as I deploy
updates. That said, i’d like to broadcast a real time warning to them
when I am about to do so. Something along the lines of:

The site will be brought down for maintenance in five minutes. Save
your work and logout to avoid losing data.

Any clever suggestions on how to do this?

A colleague suggested to me that I have a javascript on each page that
polls the server for a change in status and pos up an alert. I’m
looking for other creative suggestions…

Thanks,
Yoram

Hi Yoram,

There are lots of different approaches, especially depending on what
your app does and how it is implemented, but I usually do something
like the following, which works for both development env (setup to re-
reload all code/templates/etc on each request) and production env
(setup to re-load code/templates/etc on app restart):

in ./app/controllers/application.rb


require ‘time’

MAINT_DOWN_TIME_FILE = “#{RAILS_ROOT}/tmp/down_time_#
{RAILS_ENV}.txt”

before_filter :fetch_maint_down_time

def fetch_maint_down_time
@maint_down_time = nil
begin
s = IO.read(MAINT_DOWN_TIME_FILE)
@maint_down_time = Time.parse(s) if s && s =~ /\d/
rescue
#ignore, … meaning no scheduled down time.
end
end

in ./app/views/shared/_maintenance_msg.html.erb:

<% if @maint_down_time -%>
<% mins = (@maint_down_time - Time.now).to_i/60 -%>
<% down_when = mins > 1 ? “in #{mins} minutes” : “imminently” -%>

Notice: App going offline <%= h(down_when) %>. ...
<% end -%>

in each layout, like ./app/views/layouts/testapp.html.erb:


<%= render :partial=> ‘shared/maintenance_msg’ %>

Then anytime you want to warn your users for a given env about a
planned maint down_time, you just stuff a parsable datetime in the
appropriate env down time file, like:

$ echo ‘2009-02-28 18:20:00’ > ./tmp/down_time_production.txt

which will automatically get picked up with the next request, and thus
will display the maint msg on every screen of your app like:


Notice: App going offline in 22 minutes. …

And then once you’ve completed your maintenance, you just clear out
that down time file:

$ echo ‘’ > ./tmp/down_time_production.txt

and no maintenance msg will be displayed.

Jeff