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: Reconnect when RabbitMQ closes Tornado's channel.

Alex Vandiverba97739dd341
diff --git a/zerver/lib/queue.py b/zerver/lib/queue.py
index dacd55d10f6..7af8c095307 100644
--- a/zerver/lib/queue.py
+++ b/zerver/lib/queue.py
@@ -338,6 +338,15 @@ class TornadoQueueClient(QueueClient[Channel]):
)
ioloop.IOLoop.current().call_later(retry_secs, self._reconnect)
+ def _on_channel_closed(self, channel: Channel, reason: Exception) -> None:
+ # The channel is also closed when we close the connection or
+ # lose it; _on_connection_closed handles those.
+ if not isinstance(reason, pika.exceptions.ChannelClosedByBroker):
+ return
+ self.log.error("TornadoQueueClient's channel was closed by RabbitMQ: %s", reason)
+ assert self.connection is not None
+ self.connection.close()
+
def _on_open(self, connection: pika.connection.Connection) -> None:
assert self.connection is not None
self._connection_failure_count = 0
@@ -350,6 +359,7 @@ class TornadoQueueClient(QueueClient[Channel]):
def _on_channel_open(self, channel: Channel) -> None:
self.channel = channel
+ self.channel.add_on_close_callback(self._on_channel_closed)
self.channel.basic_qos(prefetch_count=self.prefetch)
for callback in self._on_open_cbs:
callback(channel)
diff --git a/zerver/tests/test_queue.py b/zerver/tests/test_queue.py
index 2b0929dd772..4478cb863cf 100644
--- a/zerver/tests/test_queue.py
+++ b/zerver/tests/test_queue.py
@@ -3,7 +3,13 @@ from unittest import mock
import orjson
from django.test import override_settings
-from pika.exceptions import AMQPConnectionError, ConnectionClosed
+from pika.exceptions import (
+ AMQPConnectionError,
+ ChannelClosedByBroker,
+ ChannelClosedByClient,
+ ConnectionClosed,
+ StreamLostError,
+)
from typing_extensions import override
from zerver.lib.queue import (
@@ -31,12 +37,38 @@ class TestTornadoQueueClient(ZulipTestCase):
self.assertEqual(
channel.mock_calls,
[
+ mock.call.add_on_close_callback(connection._on_channel_closed),
mock.call.basic_qos(prefetch_count=100),
mock.call.queue_declare(queue="test_suite", durable=True, callback=mock.ANY),
mock.call.basic_consume("test_suite", mock.ANY, consumer_tag=mock.ANY),
],
)
+ @mock.patch("zerver.lib.queue.ExceptionFreeTornadoConnection", autospec=True)
+ def test_channel_closed_by_broker(self, mock_cxn: mock.MagicMock) -> None:
+ connection = TornadoQueueClient()
+ channel = mock.MagicMock()
+ with self.assertLogs("zulip.queue", "INFO"):
+ connection._on_channel_open(channel)
+
+ # Closes from our side, or from losing the connection, are
+ # handled by _on_connection_closed.
+ connection._on_channel_closed(channel, ChannelClosedByClient(200, "Normal shutdown"))
+ connection._on_channel_closed(channel, StreamLostError("Transport indicated EOF"))
+ mock_cxn.return_value.close.assert_not_called()
+
+ with self.assertLogs("zulip.queue", "ERROR") as m:
+ connection._on_channel_closed(
+ channel, ChannelClosedByBroker(406, "PRECONDITION_FAILED")
+ )
+ self.assertEqual(
+ m.output,
+ [
+ "ERROR:zulip.queue:TornadoQueueClient's channel was closed by RabbitMQ: (406, 'PRECONDITION_FAILED')"
+ ],
+ )
+ mock_cxn.return_value.close.assert_called_once_with()
+
@mock.patch("zerver.lib.queue.ExceptionFreeTornadoConnection", autospec=True)
def test_on_open_closed(self, mock_cxn: mock.MagicMock) -> None:
with self.assertLogs("zulip.queue", "WARNING") as m: