Move default-request handling into common place

This commit is contained in:
Andrew Godwin 2016-02-14 17:11:44 +00:00
parent b468ddd930
commit 1328c367dc
3 changed files with 14 additions and 8 deletions

View File

@ -5,6 +5,7 @@ import importlib
from django.core.exceptions import ImproperlyConfigured
from django.utils import six
from .handler import ViewConsumer
from .utils import name_that_thing
@ -75,3 +76,11 @@ class ConsumerRegistry(object):
message.reply_channel.send({
"content": message.content.get("content", None),
})
def check_default(self):
"""
Checks to see if default handlers need to be registered
for channels, and adds them if they need to be.
"""
if not self.consumer_for_channel("http.request"):
self.add_consumer(ViewConsumer(), ["http.request"])

View File

@ -24,8 +24,7 @@ class Command(RunserverCommand):
def inner_run(self, *args, **options):
# Check a handler is registered for http reqs; if not, add default one
self.channel_layer = channel_layers[DEFAULT_CHANNEL_LAYER]
if not self.channel_layer.registry.consumer_for_channel("http.request"):
self.channel_layer.registry.add_consumer(ViewConsumer(), ["http.request"])
self.channel_layer.registry.check_default()
# Run checks
self.stdout.write("Performing system checks...\n\n")
self.check(display_num_errors=True)

View File

@ -13,20 +13,18 @@ class Command(BaseCommand):
# Get the backend to use
self.verbosity = options.get("verbosity", 1)
self.logger = setup_logger('django.channels', self.verbosity)
channel_layer = channel_layers[DEFAULT_CHANNEL_LAYER]
self.channel_layer = channel_layers[DEFAULT_CHANNEL_LAYER]
# Check a handler is registered for http reqs
if not channel_layer.registry.consumer_for_channel("http.request"):
# Register the default one
channel_layer.registry.add_consumer(ViewConsumer(), ["http.request"])
self.channel_layer.registry.check_default()
# Launch a worker
self.logger.info("Running worker against backend %s", channel_layer.alias)
self.logger.info("Running worker against backend %s", self.channel_layer.alias)
# Optionally provide an output callback
callback = None
if self.verbosity > 1:
callback = self.consumer_called
# Run the worker
try:
Worker(channel_layer=channel_layer, callback=callback).run()
Worker(channel_layer=self.channel_layer, callback=callback).run()
except KeyboardInterrupt:
pass