2013-08-29 23:41:03 +02:00
|
|
|
from __future__ import absolute_import
|
|
|
|
|
|
|
|
from django.core.management.base import BaseCommand
|
|
|
|
from django.core.management import CommandError
|
|
|
|
from django.conf import settings
|
|
|
|
from zerver.worker.queue_processors import get_worker
|
|
|
|
import sys
|
|
|
|
import signal
|
|
|
|
import logging
|
|
|
|
|
|
|
|
class Command(BaseCommand):
|
2013-09-06 21:58:15 +02:00
|
|
|
args = "<queue name> [<worker number>]"
|
2013-08-29 23:41:03 +02:00
|
|
|
help = "Runs a queue processing worker"
|
|
|
|
def handle(self, *args, **options):
|
|
|
|
logging.basicConfig()
|
|
|
|
logger = logging.getLogger('process_queue')
|
|
|
|
|
2013-09-06 21:58:15 +02:00
|
|
|
if len(args) not in (1, 2):
|
2013-08-29 23:41:03 +02:00
|
|
|
raise CommandError("Wrong number of arguments")
|
|
|
|
|
2013-09-06 21:58:15 +02:00
|
|
|
queue_name = args[0]
|
|
|
|
if len(args) > 1:
|
|
|
|
worker_num = int(args[1])
|
|
|
|
else:
|
|
|
|
worker_num = 0
|
|
|
|
|
2013-08-29 23:41:03 +02:00
|
|
|
def signal_handler(signal, frame):
|
2013-09-06 21:58:15 +02:00
|
|
|
logger.info("Worker %d disconnecting from queue %s" % (worker_num, queue_name))
|
2013-08-29 23:41:03 +02:00
|
|
|
worker.stop()
|
|
|
|
sys.exit(0)
|
|
|
|
|
2013-10-23 21:14:17 +02:00
|
|
|
if not settings.USING_RABBITMQ:
|
|
|
|
logger.error("Cannot run a queue processor when USING_RABBITMQ is False!")
|
|
|
|
sys.exit(1)
|
|
|
|
|
2013-08-29 23:41:03 +02:00
|
|
|
signal.signal(signal.SIGTERM, signal_handler)
|
|
|
|
signal.signal(signal.SIGINT, signal_handler)
|
|
|
|
|
2013-09-06 21:58:15 +02:00
|
|
|
logger.info("Worker %d connecting to queue %s" % (worker_num, queue_name))
|
|
|
|
worker = get_worker(queue_name)
|
2013-08-29 23:41:03 +02:00
|
|
|
worker.start()
|
|
|
|
|