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
|
|
|
|
2020-01-14 21:59:46 +01:00
|
|
|
|
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:
|
2021-02-12 08:20:45 +01:00
|
|
|
parser.add_argument(dest="queue_name", nargs="?", help="queue to purge")
|
|
|
|
parser.add_argument("--all", action="store_true", 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()
|
2020-07-05 03:18:11 +02:00
|
|
|
queue.ensure_queue(queue_name, lambda channel: channel.queue_purge(queue_name))
|
2017-06-05 07:47:03 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
if options["all"]:
|
2017-06-05 07:47:03 +02:00
|
|
|
for queue_name in get_active_worker_queues():
|
|
|
|
purge_queue(queue_name)
|
|
|
|
print("All queues purged")
|
2021-02-12 08:20:45 +01:00
|
|
|
elif not options["queue_name"]:
|
2017-06-05 07:47:03 +02:00
|
|
|
raise CommandError("Missing queue_name argument!")
|
|
|
|
else:
|
2021-02-12 08:20:45 +01:00
|
|
|
queue_name = options["queue_name"]
|
2021-02-12 08:19:30 +01:00
|
|
|
if not (
|
|
|
|
queue_name in get_active_worker_queues() or queue_name.startswith("notify_tornado")
|
|
|
|
):
|
2020-06-10 06:41:04 +02:00
|
|
|
raise CommandError(f"Unknown queue {queue_name}")
|
2017-06-05 07:47:03 +02:00
|
|
|
|
2020-06-10 06:41:04 +02:00
|
|
|
print(f"Purging queue {queue_name}")
|
2017-06-05 07:47:03 +02:00
|
|
|
purge_queue(queue_name)
|
|
|
|
|
2015-11-01 17:11:06 +01:00
|
|
|
print("Done")
|