Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

queue: Switch TornadoQueueClient to the new base QueueClient.

Anders Kaseorg5751479932b9
diff --git a/zerver/lib/queue.py b/zerver/lib/queue.py
index 0fbca5dbb2c..49bcea65b8c 100644
--- a/zerver/lib/queue.py
+++ b/zerver/lib/queue.py
@@ -4,7 +4,7 @@ import threading
import time
from abc import ABCMeta, abstractmethod
from collections import defaultdict
-from typing import Any, Callable, Dict, List, Mapping, Optional, Set
+from typing import Any, Callable, Dict, List, Mapping, Optional, Set, Union
import orjson
import pika
@@ -227,7 +227,9 @@ calling _adapter_disconnect, ignoring",
)
-class TornadoQueueClient(SimpleQueueClient):
+class TornadoQueueClient(QueueClient):
+ connection: Optional[ExceptionFreeTornadoConnection]
+
# Based on:
# https://pika.readthedocs.io/en/0.9.8/examples/asynchronous_consumer_example.html
def __init__(self) -> None:
@@ -294,6 +296,7 @@ class TornadoQueueClient(SimpleQueueClient):
ioloop.IOLoop.instance().call_later(retry_secs, self._reconnect)
def _on_open(self, connection: pika.connection.Connection) -> None:
+ assert self.connection is not None
self._connection_failure_count = 0
try:
self.connection.channel(on_open_callback=self._on_channel_open)
@@ -309,6 +312,10 @@ class TornadoQueueClient(SimpleQueueClient):
self._reconnect_consumer_callbacks()
self.log.info("TornadoQueueClient connected")
+ def close(self) -> None:
+ if self.connection is not None:
+ self.connection.close()
+
def ensure_queue(self, queue_name: str, callback: Callable[[BlockingChannel], None]) -> None:
def finish(frame: Any) -> None:
assert self.channel is not None
@@ -361,10 +368,10 @@ class TornadoQueueClient(SimpleQueueClient):
)
-queue_client: Optional[SimpleQueueClient] = None
+queue_client: Optional[Union[SimpleQueueClient, TornadoQueueClient]] = None
-def get_queue_client() -> SimpleQueueClient:
+def get_queue_client() -> Union[SimpleQueueClient, TornadoQueueClient]:
global queue_client
if queue_client is None:
if settings.RUNNING_INSIDE_TORNADO and settings.USING_RABBITMQ:
diff --git a/zerver/tests/test_queue.py b/zerver/tests/test_queue.py
index f5c98d5c72f..bdae3247766 100644
--- a/zerver/tests/test_queue.py
+++ b/zerver/tests/test_queue.py
@@ -5,7 +5,12 @@ import orjson
from django.test import override_settings
from pika.exceptions import AMQPConnectionError, ConnectionClosed
-from zerver.lib.queue import TornadoQueueClient, get_queue_client, queue_json_publish
+from zerver.lib.queue import (
+ SimpleQueueClient,
+ TornadoQueueClient,
+ get_queue_client,
+ queue_json_publish,
+)
from zerver.lib.test_classes import ZulipTestCase
@@ -32,6 +37,7 @@ class TestQueueImplementation(ZulipTestCase):
queue_client = get_queue_client()
def collect(events: List[Dict[str, Any]]) -> None:
+ assert isinstance(queue_client, SimpleQueueClient)
assert len(events) == 1
output.append(events[0])
queue_client.stop_consuming()
@@ -51,6 +57,7 @@ class TestQueueImplementation(ZulipTestCase):
queue_client = get_queue_client()
def collect(events: List[Dict[str, Any]]) -> None:
+ assert isinstance(queue_client, SimpleQueueClient)
assert len(events) == 1
queue_client.stop_consuming()
nonlocal count