What is the best way to implement a ‘Down for Maintenance’ page across
your Rails app?
Ideally I would like to have a button in my admin section that toggles
the display of a ‘currently under maintenance’ page to every public
request to the app (possibly with some dynamic content like estimated
down-time)
except for: -
requests from a specified IP address (so I could access the live site
from while developing/debugging and use the site as normal while
everyone else sees the ‘down’ page)
the admin section (so I’m not locked out!)
What is the best strategy to achieve this? I’ve been looking for stuff
online but it’s fairly thin on the ground concerning this idea - by
which I mean I can’t seem to find it :0)
Any ideas or advice very gratefully received,
Chris
I’d highly suggest you take a look at some of the Capistrano tasks for
this. For example, you could take your site down to the public and
still be able to do some administrative things, such as access the
console and such.
I’d highly suggest you take a look at some of the Capistrano tasks for
this. For example, you could take your site down to the public and
still be able to do some administrative things, such as access the
console and such.
Josh,
Thanks for the link, and that’s certainly something I will investigate
more, the problem with that is it seems to be dependent upon using
Apache, whereas I am using Lighttpd (and would like to continue using if
possible). Therefore I was hoping to develop a strategy/receipe that
would just involve a basic rails app without recourse to plugins, gems
or external scripts etc.
Will go and do some reading on capistrano though, might give some clues
how to implement my own version.
Submimage,
Whilst this would work it wouldn’t block visitors with bookmarked links,
or those following links from search engines - but yes it would work to
a degree!
Just something I pulled out of my … well … Regardless, what would
be the downsides/upsides to just setting a flag in a “configs” table
(or even hardcoded if you’re worried about speed) and using an
application “before_filter” to redirect based upon whatever
conditions?
before_filter :application_mode_redirect
def application_mode_redirect #test conditions #redirect to “currently down” notification if appropriate #pass-through for admins
end
I doubt the speed hit would be too bad… If so you could “cache” the
check condition and only check it once every few minutes or
something… :: shrug ::
Capistrano is webserver-agnostic. You might(?) have to modify the recipe
to run lighttpd startup and shutdown commands, but Capistrano’s your
friend either way as long as you’re serving off something Unixy.
Another way to redirect things might be a different routres.rb file for
downtime, with pretty much everything mapped to your “Sorry” page.
Capistrano would still be a super way to automate switching it in and
out.
Chris wrote:
I’d highly suggest you take a look at some of the Capistrano tasks for
this. For example, you could take your site down to the public and
still be able to do some administrative things, such as access the
console and such.
Josh,
Thanks for the link, and that’s certainly something I will investigate
more, the problem with that is it seems to be dependent upon using
Apache, whereas I am using Lighttpd (and would like to continue using if
possible). Therefore I was hoping to develop a strategy/receipe that
would just involve a basic rails app without recourse to plugins, gems
or external scripts etc.
Will go and do some reading on capistrano though, might give some clues
how to implement my own version.
Submimage,
Whilst this would work it wouldn’t block visitors with bookmarked links,
or those following links from search engines - but yes it would work to
a degree!
Couldn’t you just add something in your routes that would make all
requests go to the temporarily down page? Then comment this out when
you wanted the site back live?
On Thu, 2006-06-15 at 12:39 -0700, Ezra Z. wrote:
You might be able to put a conditional before_filter in
unless current_user.is_admin?
the admin who will have normal access.
You might be able to put a conditional before_filter in
application.rb. Maybe something like this:
before_filter :check_for_maintenence
protected
def check_for_maintenence
if File.exist? “#{RAILS_ROOT}/maintenence.html”
return render( :file => “#{RAILS_ROOT}/maintenence.html”)
unless current_user.is_admin?
end
end
Curtis & Ezra, Thanks very much to both of you, i think this proves
great minds think alike since you both posted at the same time! This
method looks like it’ll work a perfectly, I didn’t realise I could do a
before_filter on application.rb - there’s an added bonus if using Ezra’s
current_user idea since I wouldn’t have to enter my IP, just log in as
admin’s equivalent of root and hack away! Though I suppose I would have
to design some kind of fail-safe in case I got diconnected and lost my
session details.
Steve - I’m not all that up on capistrano, though it’s great to know
that it’s webserver agnostic, having just learnt how to deploy rails
apps the textdrive-by-hand method I’m a little apprehensive about
learning another system (despite the amazing power of the thing) -
though now I suppose I’ll have to add it to my to-learn list - is
Capistrano quickly becoming the de-facto deployment system for rails? is
it likely to be incorporated into the core? i.e. is it a ‘must-learn’ or
more of a ‘could-learn’?
Thanks to everyone for the awesome response, the community around rails
is just fantastic!
You might be able to put a conditional before_filter in
application.rb. Maybe something like this:
before_filter :check_for_maintenence
protected
def check_for_maintenence
if File.exist? “#{RAILS_ROOT}/maintenence.html”
return render( :file => “#{RAILS_ROOT}/maintenence.html”)
unless current_user.is_admin?
end
end
Of course that means that every hit to your app will always check
for the maintenence.html page. File.exist? is pretty fast thouhg.
Certainly faster then a database lookup. Then you would just need to
make sure you have a current_user object setup and an is_admin?
method to telkl whether the user is an admin or not. This way
everyone else that hits the app will get the maintenence page expect
the admin who will have normal access.
TextDrive supports SVN repositories and Capistrano deployment. I
would imagine most of the major RoR hosting companies are doing so.
It’s a little more difficult to learn how to use Capistrano if your
core development OS is Windows; but there are enough tutorials that
you can learn how to use it with your TextDrive account.
-Curtis
P.S. No problem. I’m no expert, but I have fun with RoR. I like to
contribute. It’s either that or go flame people on the political
forums. And while that’s fun too, it’s a different kind of
fun…heehee :: cough ::
TextDrive supports SVN repositories and Capistrano deployment. I
would imagine most of the major RoR hosting companies are doing so.
It’s a little more difficult to learn how to use Capistrano if your
core development OS is Windows; but there are enough tutorials that
you can learn how to use it with your TextDrive account.
yeah that was one of my fears, I’m stuck on windows unfortunately, I’m
planning to move to mac on my next upgrade but was holding off until the
Rev. B Intel laptops are out, at the current rate of annoyances with
windows though I think I might get one of those mac mini things in the
mean time
(I used to run linux but the lack of photoshop was killin’ me!)
P.S. No problem. I’m no expert, but I have fun with RoR. I like to
contribute. It’s either that or go flame people on the political
forums. And while that’s fun too, it’s a different kind of
fun…heehee :: cough ::
Haha, funnily enough I just came from yelling on a political blog! A
different kind of fun for sure but great after a day battling IE’s CSS
implementation!
…which is similar to that rewrite rule in Apache. This way, you
bypass your Rails app if maintenance is being performed.
Oh wow, thanks Michael, that’s very useful indeed (not just for
maintenance but also as per their example of serving up thumbnails etc)
this looks great
Only trouble is now which method to use, the before_filter allows
greater flexibility I would imagine but the lighttpd should be quicker,
and it’s very easy to implement (which is always nice!)
Thanks very much Michael, I think I’m going to play around with some
test apps tomorrow and see what happens, depending upon how I get on I
might write up the results for a mini-guide for any future people with
the same issue.
Using the magnet from lighttpd will require you to have the LUA
scripting language installed. It’s not always included in default
configs, so you might want to compile it yourself in.
It’s more secure than the disable web method, as the deploying user
doesn’t write anything into the server config.
I’m also using rewrite and aliasing in lighty to serve other static
info as the performance is just uncomparable to serving it with rails.
You can see that on my blog as well.
Last and not directly related, if you need SSL on lighty read my blog
post about it. The documentation is missing some stuff: