how to handle database down time gracefully?

82 views
Skip to first unread message

realfun

unread,
Apr 24, 2009, 7:51:54 AM4/24/09
to Django users
Hi,

I build a website(fayaa.com) on Bluehost using Django, but this month
database down 3 times, during that period, user can only see an
"Unexpected Exception" message, is there a way to redirect to a 404
page instead?

Regards,
-realfun

Timboy

unread,
Apr 24, 2009, 10:40:33 PM4/24/09
to Django users
I'm interested as well. You'd want to have a 500 with a nice
unexpected maintanence message.

Mike Ramirez

unread,
Apr 24, 2009, 11:17:00 PM4/24/09
to django...@googlegroups.com

I've seen a settings option that is used as a check in other projects for such
a situation. You can put the check in a custom middleware.

In settings.py add something like this:

# set to True to turn on maintaince page
MAINTAINANCE = True

and rewrite process_request to return the approriate response for a maintaince
page based on the setting on a true setting or return None on a false
setting, stating the site is going through maintainence or what ever message
you want to tell them.

http://docs.djangoproject.com/en/dev/topics/http/middleware/#process-request

If you want to have the middleware check the availability of the db, I'm not
exactly positive what you can check here in django.db, but possibly to check
the connection, a thumb through django.db with ipython against a sqlite3 db,
didn't show me a lot, but I also didn't go indepth into what each method is,
for those I'm not 100% on. But this check, I'm not positive how big of a
performance hit it would be or not, since this would be checked on each
request. I've normally just set the maintaince flag manually when required.

Mike

--
Big book, big bore.
-- Callimachus

signature.asc

Doug B

unread,
Apr 25, 2009, 12:57:47 AM4/25/09
to Django users
This has been on my todo list of a while now, so I thought I'd trying
to figure it out.

I -think- the proper way to go about this would be in a custom
middleware with a process_exception handler that checks to see if the
exception is an instanace of the database defined exceptions. Looking
in django/db/__init__.py shows "DatabaseError" gets assigned according
to backend, so I assume it's safe to use as a check regardless of
database settings.

Turning off my database any playing I did a check on the exception
generated when the database was down, and it looks ok - atleast for
postgres:

from django.db import DatabaseError

isinstance(exception,DatabaseError) returned true.

So I though I could add this method to a middleware class:

def process_exception(self,request,exception):
# In case django doesn't send use the original request
from django.db import DatabaseError
if isinstance(exception,DatabaseError):
return HttpResponse("Database offline for maintenance - please
try back later")
return None

Unfortunately, process_excpetion() never seems to get called.

Searching track shows a ticket that explains the problem. Exceptions
in middleware don't hit the middlware exception handlers.
Unfortunately one of my middlware does hit the database on each call,
so I had to modify my middlware to catch the exception and return a
response object.

http://code.djangoproject.com/ticket/6094

Other than making sure to handle any middlware DB exceptions manually
it seems to be working ok.

James Bennett

unread,
Apr 25, 2009, 1:01:23 AM4/25/09
to django...@googlegroups.com

If you don't mind doing things completely wrong, sure.

"My database is broken and nothing on my site works" is not "file not
found". It is and should always be "internal server error". Unless
you'd rather have the embarrassment of people thinking there's nothing
on your site to come back and see, as opposed to having them think
it's down all the time.


--
"Bureaucrat Conrad, you are technically correct -- the best kind of correct."

Reply all
Reply to author
Forward
0 new messages