Skip to content
This repository has been archived by the owner on Sep 3, 2024. It is now read-only.

Don't we need a DbConnectionsMiddleware ? #7

Open
dperetti opened this issue Mar 24, 2021 · 3 comments
Open

Don't we need a DbConnectionsMiddleware ? #7

dperetti opened this issue Mar 24, 2021 · 3 comments

Comments

@dperetti
Copy link

... like django-dramatiq has ?

@jarekwg
Copy link
Member

jarekwg commented Mar 24, 2021

Ha yes. We actually wrote our own in our production code. Should definitely live in this repo.
Feel free to add it, otherwise we'll get round to it eventually...

@pedrospdc
Copy link

@jarekwg any chance you could paste here a snippet of your middleware? I'm happy to add it myself :)

@jarekwg
Copy link
Member

jarekwg commented Oct 6, 2021

Here you are @pedrospdc

class DbConnectionsMiddleware(Middleware):
    """This middleware cleans up db connections on worker shutdown.
    """

    def _close_connections(self, *args, **kwargs):
        for conn in db.connections.all():
            conn.close()

    before_process_message = _close_connections
    after_process_message = _close_connections

    before_consumer_thread_shutdown = _close_connections
    before_worker_thread_shutdown = _close_connections
    before_worker_shutdown = _close_connections

Thanks in advance! :)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants