mirror of
https://github.com/zulip/zulip.git
synced 2025-11-19 22:19:48 +00:00
tornado: Switch to start_json_consumer interface.
This commit is contained in:
committed by
Tim Abbott
parent
f0b23b0752
commit
179c387409
@@ -372,19 +372,25 @@ class TornadoQueueClient(SimpleQueueClient):
|
|||||||
assert self.channel is not None
|
assert self.channel is not None
|
||||||
callback(self.channel)
|
callback(self.channel)
|
||||||
|
|
||||||
def register_consumer(self, queue_name: str, consumer: Consumer) -> None:
|
def start_json_consumer(self,
|
||||||
|
queue_name: str,
|
||||||
|
callback: Callable[[List[Dict[str, Any]]], None],
|
||||||
|
batch_size: int=1,
|
||||||
|
timeout: Optional[int]=None) -> None:
|
||||||
def wrapped_consumer(ch: BlockingChannel,
|
def wrapped_consumer(ch: BlockingChannel,
|
||||||
method: Basic.Deliver,
|
method: Basic.Deliver,
|
||||||
properties: pika.BasicProperties,
|
properties: pika.BasicProperties,
|
||||||
body: bytes) -> None:
|
body: bytes) -> None:
|
||||||
consumer(ch, method, properties, body)
|
callback([orjson.loads(body)])
|
||||||
ch.basic_ack(delivery_tag=method.delivery_tag)
|
ch.basic_ack(delivery_tag=method.delivery_tag)
|
||||||
|
|
||||||
if not self.ready():
|
assert batch_size == 1
|
||||||
|
assert timeout is None
|
||||||
self.consumers[queue_name].add(wrapped_consumer)
|
self.consumers[queue_name].add(wrapped_consumer)
|
||||||
|
|
||||||
|
if not self.ready():
|
||||||
return
|
return
|
||||||
|
|
||||||
self.consumers[queue_name].add(wrapped_consumer)
|
|
||||||
self.ensure_queue(
|
self.ensure_queue(
|
||||||
queue_name,
|
queue_name,
|
||||||
lambda channel: channel.basic_consume(
|
lambda channel: channel.basic_consume(
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ from zerver.tornado.event_queue import (
|
|||||||
from zerver.tornado.sharding import notify_tornado_queue_name
|
from zerver.tornado.sharding import notify_tornado_queue_name
|
||||||
|
|
||||||
if settings.USING_RABBITMQ:
|
if settings.USING_RABBITMQ:
|
||||||
from zerver.lib.queue import get_queue_client
|
from zerver.lib.queue import TornadoQueueClient, get_queue_client
|
||||||
|
|
||||||
|
|
||||||
def handle_callback_exception(callback: Callable[..., Any]) -> None:
|
def handle_callback_exception(callback: Callable[..., Any]) -> None:
|
||||||
@@ -89,9 +89,10 @@ class Command(BaseCommand):
|
|||||||
|
|
||||||
if settings.USING_RABBITMQ:
|
if settings.USING_RABBITMQ:
|
||||||
queue_client = get_queue_client()
|
queue_client = get_queue_client()
|
||||||
|
assert isinstance(queue_client, TornadoQueueClient)
|
||||||
# Process notifications received via RabbitMQ
|
# Process notifications received via RabbitMQ
|
||||||
queue_name = notify_tornado_queue_name(port)
|
queue_name = notify_tornado_queue_name(port)
|
||||||
queue_client.register_json_consumer(queue_name,
|
queue_client.start_json_consumer(queue_name,
|
||||||
get_wrapped_process_notification(queue_name))
|
get_wrapped_process_notification(queue_name))
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|||||||
@@ -1081,13 +1081,14 @@ def process_notification(notice: Mapping[str, Any]) -> None:
|
|||||||
event['type'], len(users), int(1000 * (time.time() - start_time)),
|
event['type'], len(users), int(1000 * (time.time() - start_time)),
|
||||||
)
|
)
|
||||||
|
|
||||||
def get_wrapped_process_notification(queue_name: str) -> Callable[[Dict[str, Any]], None]:
|
def get_wrapped_process_notification(queue_name: str) -> Callable[[List[Dict[str, Any]]], None]:
|
||||||
def failure_processor(notice: Dict[str, Any]) -> None:
|
def failure_processor(notice: Dict[str, Any]) -> None:
|
||||||
logging.error(
|
logging.error(
|
||||||
"Maximum retries exceeded for Tornado notice:%s\nStack trace:\n%s\n",
|
"Maximum retries exceeded for Tornado notice:%s\nStack trace:\n%s\n",
|
||||||
notice, traceback.format_exc())
|
notice, traceback.format_exc())
|
||||||
|
|
||||||
def wrapped_process_notification(notice: Dict[str, Any]) -> None:
|
def wrapped_process_notification(notices: List[Dict[str, Any]]) -> None:
|
||||||
|
for notice in notices:
|
||||||
try:
|
try:
|
||||||
process_notification(notice)
|
process_notification(notice)
|
||||||
except Exception:
|
except Exception:
|
||||||
|
|||||||
Reference in New Issue
Block a user