mirror of
https://github.com/django/daphne.git
synced 2025-04-21 01:02:06 +03:00
Move default-request handling into common place
This commit is contained in:
parent
b468ddd930
commit
1328c367dc
|
@ -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"])
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user