# Warning users that the site is coming down for maintenance

**URL:** <https://www.ruby-forum.com/t/warning-users-that-the-site-is-coming-down-for-maintenance/161616>\
**Category:** Rails\
**Created:** [February 27, 2009, 9:28pm UTC](https://www.ruby-forum.com/t/warning-users-that-the-site-is-coming-down-for-maintenance/161616 "2009-02-27T21:28:55Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [February 27, 2009, 9:28pm UTC](https://www.ruby-forum.com/t/warning-users-that-the-site-is-coming-down-for-maintenance/161616/1 "2009-02-27T21:28:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 1, 2009, 3:13am UTC](https://www.ruby-forum.com/t/warning-users-that-the-site-is-coming-down-for-maintenance/161616/2 "2009-03-01T03:13:02Z")

</div>

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
