mirror of
https://github.com/django/daphne.git
synced 2025-04-20 08:42:18 +03:00
Fixed #148: Close database connections when consumers finish.
This commit is contained in:
parent
a05f7d5a96
commit
e7a354e03c
9
channels/signals.py
Normal file
9
channels/signals.py
Normal file
|
@ -0,0 +1,9 @@
|
|||
from django.db import close_old_connections
|
||||
from django.dispatch import Signal
|
||||
|
||||
|
||||
consumer_started = Signal(providing_args=["environ"])
|
||||
consumer_finished = Signal()
|
||||
|
||||
# Connect connection closer to consumer finished as well
|
||||
consumer_finished.connect(close_old_connections)
|
|
@ -6,6 +6,7 @@ import signal
|
|||
import sys
|
||||
import time
|
||||
|
||||
from .signals import consumer_started, consumer_finished
|
||||
from .exceptions import ConsumeLater
|
||||
from .message import Message
|
||||
from .utils import name_that_thing
|
||||
|
@ -104,6 +105,9 @@ class Worker(object):
|
|||
self.callback(channel, message)
|
||||
try:
|
||||
logger.debug("Dispatching message on %s to %s", channel, name_that_thing(consumer))
|
||||
# Send consumer started to manage lifecycle stuff
|
||||
consumer_started.send(sender=self.__class__, environ={})
|
||||
# Run consumer
|
||||
consumer(message, **kwargs)
|
||||
except ConsumeLater:
|
||||
# They want to not handle it yet. Re-inject it with a number-of-tries marker.
|
||||
|
@ -127,3 +131,6 @@ class Worker(object):
|
|||
break
|
||||
except:
|
||||
logger.exception("Error processing message with consumer %s:", name_that_thing(consumer))
|
||||
else:
|
||||
# Send consumer finished so DB conns close etc.
|
||||
consumer_finished.send(sender=self.__class__)
|
||||
|
|
Loading…
Reference in New Issue
Block a user