From 1328c367dc1cdf7c92c9d8e9b8d97afe53aadb7a Mon Sep 17 00:00:00 2001 From: Andrew Godwin Date: Sun, 14 Feb 2016 17:11:44 +0000 Subject: [PATCH] Move default-request handling into common place --- channels/consumer_registry.py | 9 +++++++++ channels/management/commands/runserver.py | 3 +-- channels/management/commands/runworker.py | 10 ++++------ 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/channels/consumer_registry.py b/channels/consumer_registry.py index 0b7f8d2..69743e0 100644 --- a/channels/consumer_registry.py +++ b/channels/consumer_registry.py @@ -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"]) diff --git a/channels/management/commands/runserver.py b/channels/management/commands/runserver.py index 7ecc3b6..d78b899 100644 --- a/channels/management/commands/runserver.py +++ b/channels/management/commands/runserver.py @@ -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) diff --git a/channels/management/commands/runworker.py b/channels/management/commands/runworker.py index 5406548..82ddace 100644 --- a/channels/management/commands/runworker.py +++ b/channels/management/commands/runworker.py @@ -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