Fixed #148: Close database connections when consumers finish.

This commit is contained in:
Andrew Godwin 2016-07-12 15:01:19 -07:00
parent a05f7d5a96
commit e7a354e03c
2 changed files with 16 additions and 0 deletions

9
channels/signals.py Normal file
View 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)

View File

@ -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__)