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
|
2015-11-24 07:01:35 +01:00
|
|
|
from zerver.worker.queue_processors import get_worker, get_active_worker_queues
|
2013-08-29 23:41:03 +02:00
|
|
|
import sys
|
|
|
|
import signal
|
|
|
|
import logging
|
2015-11-24 07:01:35 +01:00
|
|
|
import threading
|
2013-08-29 23:41:03 +02:00
|
|
|
|
|
|
|
class Command(BaseCommand):
|
2015-08-21 02:10:41 +02:00
|
|
|
def add_arguments(self, parser):
|
2015-11-24 07:01:35 +01:00
|
|
|
parser.add_argument('--queue_name', metavar='<queue name>', type=str,
|
2015-08-21 02:10:41 +02:00
|
|
|
help="queue to process")
|
2015-11-24 07:01:35 +01:00
|
|
|
parser.add_argument('--worker_num', metavar='<worker number>', type=int, nargs='?', default=0,
|
2015-08-21 02:10:41 +02:00
|
|
|
help="worker label")
|
2015-11-24 07:01:35 +01:00
|
|
|
parser.add_argument('--all', dest="all", action="store_true", default=False,
|
|
|
|
help="run all queues")
|
2015-08-21 02:10:41 +02:00
|
|
|
|
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-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)
|
|
|
|
|
2015-11-24 07:01:35 +01:00
|
|
|
if options['all']:
|
|
|
|
for queue_name in get_active_worker_queues():
|
|
|
|
logger.info('launching queue worker thread ' + queue_name)
|
|
|
|
td = Threaded_worker(queue_name)
|
|
|
|
td.start()
|
|
|
|
else:
|
|
|
|
queue_name = options['queue_name']
|
|
|
|
worker_num = options['worker_num']
|
|
|
|
|
|
|
|
logger.info("Worker %d connecting to queue %s" % (worker_num, queue_name))
|
|
|
|
worker = get_worker(queue_name)
|
|
|
|
|
|
|
|
def signal_handler(signal, frame):
|
|
|
|
logger.info("Worker %d disconnecting from queue %s" % (worker_num, queue_name))
|
|
|
|
worker.stop()
|
|
|
|
sys.exit(0)
|
|
|
|
signal.signal(signal.SIGTERM, signal_handler)
|
|
|
|
signal.signal(signal.SIGINT, signal_handler)
|
|
|
|
|
|
|
|
worker.start()
|
|
|
|
|
|
|
|
class Threaded_worker(threading.Thread):
|
|
|
|
def __init__(self, queue_name):
|
|
|
|
threading.Thread.__init__(self)
|
|
|
|
self.worker = get_worker(queue_name)
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
self.worker.setup()
|
|
|
|
logging.debug('starting consuming ' + self.worker.queue_name)
|
|
|
|
self.worker.start()
|