mirror of
https://github.com/django/daphne.git
synced 2025-07-10 16:02:18 +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.core.exceptions import ImproperlyConfigured
|
||||||
from django.utils import six
|
from django.utils import six
|
||||||
|
|
||||||
|
from .handler import ViewConsumer
|
||||||
from .utils import name_that_thing
|
from .utils import name_that_thing
|
||||||
|
|
||||||
|
|
||||||
|
@ -75,3 +76,11 @@ class ConsumerRegistry(object):
|
||||||
message.reply_channel.send({
|
message.reply_channel.send({
|
||||||
"content": message.content.get("content", None),
|
"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):
|
def inner_run(self, *args, **options):
|
||||||
# Check a handler is registered for http reqs; if not, add default one
|
# Check a handler is registered for http reqs; if not, add default one
|
||||||
self.channel_layer = channel_layers[DEFAULT_CHANNEL_LAYER]
|
self.channel_layer = channel_layers[DEFAULT_CHANNEL_LAYER]
|
||||||
if not self.channel_layer.registry.consumer_for_channel("http.request"):
|
self.channel_layer.registry.check_default()
|
||||||
self.channel_layer.registry.add_consumer(ViewConsumer(), ["http.request"])
|
|
||||||
# Run checks
|
# Run checks
|
||||||
self.stdout.write("Performing system checks...\n\n")
|
self.stdout.write("Performing system checks...\n\n")
|
||||||
self.check(display_num_errors=True)
|
self.check(display_num_errors=True)
|
||||||
|
|
|
@ -13,20 +13,18 @@ class Command(BaseCommand):
|
||||||
# Get the backend to use
|
# Get the backend to use
|
||||||
self.verbosity = options.get("verbosity", 1)
|
self.verbosity = options.get("verbosity", 1)
|
||||||
self.logger = setup_logger('django.channels', self.verbosity)
|
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
|
# Check a handler is registered for http reqs
|
||||||
if not channel_layer.registry.consumer_for_channel("http.request"):
|
self.channel_layer.registry.check_default()
|
||||||
# Register the default one
|
|
||||||
channel_layer.registry.add_consumer(ViewConsumer(), ["http.request"])
|
|
||||||
# Launch a worker
|
# 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
|
# Optionally provide an output callback
|
||||||
callback = None
|
callback = None
|
||||||
if self.verbosity > 1:
|
if self.verbosity > 1:
|
||||||
callback = self.consumer_called
|
callback = self.consumer_called
|
||||||
# Run the worker
|
# Run the worker
|
||||||
try:
|
try:
|
||||||
Worker(channel_layer=channel_layer, callback=callback).run()
|
Worker(channel_layer=self.channel_layer, callback=callback).run()
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user