Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

invalidate django's settings cache without restarting the server?

I'm working with a django app involving multiple databases - dynamically added multiple databases, that is. Any time a new client signs up we give em a new database. The settings file can load these dynamically (as in, I run a shell script on the server and that updates the databases definition without changing any code - if you're really curious how, message me), but since uwsgi/django caches the settings, I have to bump uwsgi just to get the new database recognized.

Any advice on forcing django to reload settings or invalidate its settings cache? It can be from the command line, etc. At the moment I'm using import settings rather than from django.conf import settings because that has semi-worked in the past, but I can easily switch back.

Thanks!

ps - if the answer is, "you dummy, don't use dynamic multiple databases" well, that's ok too ;)

like image 290
Eli Albért Avatar asked Nov 13 '22 04:11

Eli Albért


1 Answers

First of all. If you can resign from multiple databases DO, Django support this well only if list of databases is fixed. If you can afford separate DB per client, you probably can afford separate wsgi processes (and separate DATABASES setting) for different clients. Then you just spawnin new wsgi process for new client and don't have to modify settings.

If you still want to do what you planed, here are some tips:

Reloading settings dynamically is not an option. There are to many problems out there. Even if you fight all of them, they may come back in moment you not expect and you will spend hours (if not days) on debugging.

If your database settings are consistent (ie. same password and user for all clients), you can consider using defaultdict for DATABASES variable. Default dict has factory option. You can have have in factory something like this:

def database_configuration_factory(name):
    # check if database exists (ie. raw SQL)

    return {
        'NAME': name,
        ...
    }
like image 110
Tomasz Wysocki Avatar answered Dec 22 '22 07:12

Tomasz Wysocki