Bogdanp / django_dramatiq

A Django app that integrates with Dramatiq.
https://dramatiq.io
Other
347 stars 77 forks source link

Compatibility with django-configurations #6

Closed BATCOH closed 6 years ago

BATCOH commented 6 years ago

django-configurations is popular app that provide class-based configurations for Django.

django-configurations require to replace django.core.management.execute_from_command_line in manage.py and wsgi.py with configurations.management.execute_from_command_line, its just wrapper around standart Django function that called after configurations.importer.install() magic.

That works fine with most of commands called from command line, but not with rundramatiq because rundramatiq spawn new process with its own django setup via django_dramatiq.setup. So dramatiq crashed with exception ImproperlyConfigured: django-configurations settings importer wasn't correctly installed when using with django-configurations.

My workaround is to place custom rundramatiq command in my project with something like this:

from django_dramatiq.management.commands.rundramatiq import Command as RunDramatiqCommand

class Command(RunDramatiqCommand):
    def discover_tasks_modules(self):
        tasks_modules = super().discover_tasks_modules()
        tasks_modules[0] = 'project.dramatiq_setup'
        return tasks_modules

and project.dramatiq_setup:

import django
from configurations.importer import install

install(check_options=True)
django.setup()

Don't know is there some changes in django_dramatiq really required, but maybe this is the case for documentation.

P.S. Sorry for possible writing mistakes, English is not my primary language.

Bogdanp commented 6 years ago

Hi @BATCOH, your approach makes a lot of sense. I'll add a little section to the README on how to use django_dramatiq with django-configurations sometime this week.

Thanks!

anapaulagomes commented 3 years ago

For me, this only worked after I renamed my custom command to something different than rundramatiq. Currently running my dramatiq_setup.py like this:

import configurations
from dotenv import find_dotenv, load_dotenv

load_dotenv(find_dotenv())
configurations.setup()

Documenting it here in case other people needs help with this. :)