daphne/channels/management/commands/runworker.py
2015-12-02 12:38:02 -08:00

39 lines
1.5 KiB
Python

from django.core.management import BaseCommand, CommandError
from channels import channel_backends, DEFAULT_CHANNEL_BACKEND
from channels.log import setup_logger
from channels.adapters import UrlConsumer
from channels.worker import Worker
class Command(BaseCommand):
def handle(self, *args, **options):
# Get the backend to use
self.verbosity = options.get("verbosity", 1)
self.logger = setup_logger('django.channels', self.verbosity)
channel_backend = channel_backends[DEFAULT_CHANNEL_BACKEND]
if channel_backend.local_only:
raise CommandError(
"You have a process-local channel backend configured, and so cannot run separate workers.\n"
"Configure a network-based backend in CHANNEL_BACKENDS to use this command."
)
# Check a handler is registered for http reqs
if not channel_backend.registry.consumer_for_channel("http.request"):
# Register the default one
channel_backend.registry.add_consumer(UrlConsumer(), ["http.request"])
# Launch a worker
self.logger.info("Running worker against backend %s", channel_backend)
# Optionally provide an output callback
callback = None
if self.verbosity > 1:
callback = self.consumer_called
# Run the worker
try:
Worker(channel_backend=channel_backend, callback=callback).run()
except KeyboardInterrupt:
pass
def consumer_called(self, channel, message):
self.logger.debug("%s", channel)