2017-11-16 00:43:27 +01:00
|
|
|
from argparse import ArgumentParser
|
2016-06-04 16:52:18 +02:00
|
|
|
from typing import Any
|
|
|
|
|
2013-09-11 19:58:58 +02:00
|
|
|
from django.core.management import CommandError
|
2017-11-16 00:43:27 +01:00
|
|
|
from django.core.management.base import BaseCommand
|
|
|
|
|
2013-09-11 19:58:58 +02:00
|
|
|
from zerver.lib.queue import SimpleQueueClient
|
2017-06-05 07:47:03 +02:00
|
|
|
from zerver.worker.queue_processors import get_active_worker_queues
|
2013-09-11 19:58:58 +02:00
|
|
|
|
|
|
|
class Command(BaseCommand):
|
2017-10-26 11:35:57 +02:00
|
|
|
def add_arguments(self, parser: ArgumentParser) -> None:
|
2017-06-05 07:47:03 +02:00
|
|
|
parser.add_argument(dest="queue_name", type=str, nargs='?',
|
|
|
|
help="queue to purge", default=None)
|
|
|
|
parser.add_argument('--all', dest="all", action="store_true",
|
|
|
|
default=False, help="purge all queues")
|
2015-08-21 02:10:41 +02:00
|
|
|
|
2013-09-11 19:58:58 +02:00
|
|
|
help = "Discards all messages from the given queue"
|
2016-11-29 07:22:02 +01:00
|
|
|
|
2017-10-26 11:35:57 +02:00
|
|
|
def handle(self, *args: Any, **options: str) -> None:
|
|
|
|
def purge_queue(queue_name: str) -> None:
|
2017-06-05 07:47:03 +02:00
|
|
|
queue = SimpleQueueClient()
|
|
|
|
queue.ensure_queue(queue_name, lambda: None)
|
|
|
|
queue.channel.queue_purge(queue_name)
|
|
|
|
|
|
|
|
if options['all']:
|
|
|
|
for queue_name in get_active_worker_queues():
|
|
|
|
purge_queue(queue_name)
|
|
|
|
print("All queues purged")
|
|
|
|
elif not options['queue_name']:
|
|
|
|
raise CommandError("Missing queue_name argument!")
|
|
|
|
else:
|
|
|
|
queue_name = options['queue_name']
|
2018-11-03 00:05:13 +01:00
|
|
|
if not (queue_name in get_active_worker_queues() or
|
|
|
|
queue_name.startswith("notify_tornado") or
|
|
|
|
queue_name.startswith("tornado_return")):
|
2017-06-05 07:47:03 +02:00
|
|
|
raise CommandError("Unknown queue %s" % (queue_name,))
|
|
|
|
|
|
|
|
print("Purging queue %s" % (queue_name,))
|
|
|
|
purge_queue(queue_name)
|
|
|
|
|
2015-11-01 17:11:06 +01:00
|
|
|
print("Done")
|