Bogdanp / django_dramatiq

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

Adds support for additional kwargs in middlewares (Solves issue #82) #83

Closed dnmellen closed 3 years ago

dnmellen commented 3 years ago

I'm trying to make the middleware initialization more flexible by adding a hooks system to be able to override the initial kwargs for some middleware during the django_dramatiq app initialization.

The way you would provide this hook would be by overriding django_dramatiq django conf:

If you want to add dramatiq.middleware.GroupCallbacks middleware, the name of the hook would be middleware_groupcallbacks_kwargs. It takes the middleware name and converts it to lowercase (middleware_<middlewarename>_kwargs).

The hook is a classmethod that returns a dict containing the kwargs for that middleware.

from django_dramatiq.apps import DjangoDramatiqConfig, RATE_LIMITER_BACKEND

class MyDjangoDramatiqConfig(DjangoDramatiqConfig):

    @classmethod
    def middleware_groupcallbacks_kwargs(cls):
        return {"rate_limiter_backend": cls.get_rate_limiter_backend()}

I was completely unable to create tests for this feature without changing the current test settings, but these changes are not changing any functionality unless you override the DjangoDramatiqConfig.

Bogdanp commented 3 years ago

@dnmellen thanks! I'll dig into this next weekend.

Bogdanp commented 3 years ago

Thanks again. This looks good, but would you mind adding an example to the README? An adapted version of your OP here would be fine. If not, I can take care of it next week.

dnmellen commented 3 years ago

No problem! I can change the README.

dnmellen commented 3 years ago

@Bogdanp I added some documentation to the README. Feel free to complete or change anything you want for better clarification.

Thanks!

Bogdanp commented 3 years ago

Thanks again. I made some minor changes on top and merged your changes with rebase. I'll cut a release in the next couple of weeks.