1

I am using Django 1.3 with mod_wsgi

In my settings.py

DISABLE_SYSTEM = False
DISABLE_USER_INTERFACE = False
MIDDLEWARE_CLASSES = [
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.locale.LocaleMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
]
if DISABLE_SYSTEM:
    MIDDLEWARE_CLASSES.insert(0, 'SomeMiddleware')
if DISABLE_USER_INTERFACE:
    MIDDLEWARE_CLASSES.append('SomeOtherMiddleware')

When i set DISABLE_SYSTEM to True, SomeMiddleware's process_request function returns a warning message with HttpResponse and following Middewares do not run at all. That gives me a kind of System shutdown for maintenance

DISABLE_USER_INTERFACE filters request and any view function called by a user is blocked, while admin urls and management functions runs normally.

Up to now, i used this for maintenance of different kinds, and i simply change it from the file and touch wsgi to re-reload python modules. But now, i need to use a kind of scheduled routine to stop user based requests and do some maintanance on the background and some from admin.

At this point, i system (but not user) needs to set DISABLE_USER_INTERFACE to True from settings.py and start maintenance. But i could not find porper way to do it or a better way to handle this not from settings.py but somewhere else.

Lines in settings py than changes DISABLE_USER_INTERFACE is because i need to use this function once per day and using these two middleware for each request do not seem logical to me. So in my current model, they run only when they are needed to be run

Any suggestions will be appreciated.

UPDATE: What i want to do is disabling user interface during 19:00 - 19:30 everyday. I am not sure about making a middleware level check like:

if 19:00<now()<19:30:
    stop system

for every request. I need something more efficient that avoids unnecessary process. Or is middleware the right choice for that kind of works?

1 Answer 1

1

When you have scheduled changes to your settings, you might want to reconsider what you're doing.

Your Middleware can be much, much smarter. This makes it possible for your settings to be much, much dumber.

I would strongly suggest that you have one SomeMiddleware class which is always installed.

That SomeMiddleware class can then check the settings and decide which behavior it should perform.

There are still better ways to do this.

You don't necessarily need to revise the settings for the scheduled maintenance. You have lots and lots of ways of communicating with your middleware in a running Django application. One of the fastest is through the database.

You have can an "operating mode" class definition with one (or a few) attributes which are simply fetched by the middleware to see what's going on. You can write your admin apps to do a simple Update on this table to change the mode.

You can have an "operating mode" file names which (if present) change the middleware's behavior. You need only do os.path.exists() kind functions to check (quickly) what to do.

Sign up to request clarification or add additional context in comments.

5 Comments

What i want is to close the system between 19.00 to 19.30, i thought about writing a middleware which checks time for each request and blocks them if it is maintenance time, but running a if 19.00<now()<19.30: stop system control for each request seemed unnecessary. I am looking for something more efficient...
"but running a if 19.00<now()<19.30: stop system control for each request seemed unnecessary". Actually, the overhead is unmeasurably small. However, that's why I suggested of creating a file in a known location. cron can create and remove this file on your schedule.
Thanks, since i am using memcached module,i set a cron to create a cache key and my MiddleWare checks for existance of that key. Since memcache supports lifetime for each memcache object, there is no need for a second cron to remove related cache key.
"my MiddleWare checks for existance of that key." It's probably much less expensive to check the time. However, since you're happy with this, cheers.
Thanks, i will make some stress test to check the diffrence between two (:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.