-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
pika.py
288 lines (228 loc) 路 8.86 KB
/
pika.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import json
import logging
import typing
import os
from threading import Thread
from typing import Dict, Optional, Text, Union, List
import time
import rasa.core.brokers.utils as rasa_broker_utils
from rasa.constants import ENV_LOG_LEVEL_LIBRARIES, DEFAULT_LOG_LEVEL_LIBRARIES
from rasa.core.brokers.event_channel import EventChannel
from rasa.utils.endpoints import EndpointConfig
if typing.TYPE_CHECKING:
from pika.adapters.blocking_connection import BlockingChannel
from pika import SelectConnection, BlockingConnection
from pika.channel import Channel
from pika.connection import Parameters, Connection
logger = logging.getLogger(__name__)
def initialise_pika_connection(
host: Text,
username: Text,
password: Text,
port: Union[Text, int] = 5672,
connection_attempts: int = 20,
retry_delay_in_seconds: Union[int, float] = 5,
) -> "BlockingConnection":
"""Create a Pika `BlockingConnection`.
Args:
host: Pika host
username: username for authentication with Pika host
password: password for authentication with Pika host
port: port of the Pika host
connection_attempts: number of channel attempts before giving up
retry_delay_in_seconds: delay in seconds between channel attempts
Returns:
Pika `BlockingConnection` with provided parameters
"""
import pika
parameters = _get_pika_parameters(
host, username, password, port, connection_attempts, retry_delay_in_seconds
)
return pika.BlockingConnection(parameters)
def _get_pika_parameters(
host: Text,
username: Text,
password: Text,
port: Union[Text, int] = 5672,
connection_attempts: int = 20,
retry_delay_in_seconds: Union[int, float] = 5,
) -> "Parameters":
import pika
if host.startswith("amqp"):
# user supplied a amqp url containing all the info
parameters = pika.URLParameters(host)
parameters.connection_attempts = connection_attempts
parameters.retry_delay = retry_delay_in_seconds
if username:
parameters.credentials = pika.PlainCredentials(username, password)
else:
# host seems to be just the host, so we use our parameters
parameters = pika.ConnectionParameters(
host,
port=port,
credentials=pika.PlainCredentials(username, password),
connection_attempts=connection_attempts,
# Wait between retries since
# it can take some time until
# RabbitMQ comes up.
retry_delay=retry_delay_in_seconds,
ssl_options=rasa_broker_utils.create_rabbitmq_ssl_options(host),
)
return parameters
def initialise_pika_channel(
host: Text,
queue: Text,
username: Text,
password: Text,
port: Union[Text, int] = 5672,
connection_attempts: int = 20,
retry_delay_in_seconds: Union[int, float] = 5,
) -> "BlockingChannel":
"""Initialise a Pika channel with a durable queue.
Args:
host: Pika host
queue: Pika queue to declare
username: username for authentication with Pika host
password: password for authentication with Pika host
port: port of the Pika host
connection_attempts: number of channel attempts before giving up
retry_delay_in_seconds: delay in seconds between channel attempts
Returns:
Pika `BlockingChannel` with declared queue
"""
connection = initialise_pika_connection(
host, username, password, port, connection_attempts, retry_delay_in_seconds
)
return _declare_pika_channel_with_queue(connection, queue)
def _declare_pika_channel_with_queue(
connection: "BlockingConnection", queue: Text
) -> "BlockingChannel":
"""Declare a durable queue on Pika channel."""
channel = connection.channel()
channel.queue_declare(queue, durable=True)
return channel
def close_pika_channel(channel: "Channel") -> None:
"""Attempt to close Pika channel."""
from pika.exceptions import AMQPError
try:
channel.close()
logger.debug("Successfully closed Pika channel.")
except AMQPError:
logger.exception("Failed to close Pika channel.")
def close_pika_connection(connection: "Connection") -> None:
"""Attempt to close Pika connection."""
from pika.exceptions import AMQPError
try:
connection.close()
logger.debug("Successfully closed Pika connection with host.")
except AMQPError:
logger.exception("Failed to close Pika connection with host.")
class PikaProducer(EventChannel):
def __init__(
self,
host: Text,
username: Text,
password: Text,
port: Union[int, Text] = 5672,
queue: Text = "rasa_core_events",
loglevel: Union[Text, int] = os.environ.get(
ENV_LOG_LEVEL_LIBRARIES, DEFAULT_LOG_LEVEL_LIBRARIES
),
):
logging.getLogger("pika").setLevel(loglevel)
self.queue = queue
self.host = host
self.username = username
self.password = password
self.port = port
self.channel: Optional["Channel"] = None
# List to store unpublished messages which hopefully will be published later
self._unpublished_messages: List[Text] = []
self._run_pika()
def __del__(self) -> None:
if self.channel:
close_pika_channel(self.channel)
close_pika_connection(self.channel.connection)
@classmethod
def from_endpoint_config(
cls, broker_config: Optional["EndpointConfig"]
) -> Optional["PikaProducer"]:
if broker_config is None:
return None
return cls(broker_config.url, **broker_config.kwargs)
def _run_pika(self) -> None:
self._pika_connection = self._get_connection()
# Run Pika io loop in extra thread so it's not blocking
self._run_pika_io_loop_in_thread()
def _get_connection(self) -> "Connection":
import pika
parameters = _get_pika_parameters(
self.host, self.username, self.password, self.port
)
return pika.SelectConnection(
parameters,
on_open_callback=self._on_open_connection,
on_open_error_callback=self._on_open_connection_error,
)
def _on_open_connection(self, connection: "SelectConnection") -> None:
logger.debug(f"Rabbit MQ connection to '{self.host}' was established.")
connection.channel(on_open_callback=self._on_channel_open)
def _on_open_connection_error(self, _, error: Text) -> None:
logger.warning(
f"Connecting to '{self.host}' failed with error '{error}. Trying again."
)
def _on_channel_open(self, channel: "Channel") -> None:
logger.debug("Rabbit MQ channel was opened.")
channel.queue_declare(self.queue, durable=True)
self.channel = channel
while len(self._unpublished_messages) > 0:
# Send unpublished messages
message = self._unpublished_messages.pop()
self._publish(message)
def _run_pika_io_loop_in_thread(self) -> None:
thread = Thread(target=self._run_pika_io_loop, daemon=True)
thread.start()
def _run_pika_io_loop(self) -> None:
self._pika_connection.ioloop.start()
def publish(
self, event: Dict, retries: int = 60, retry_delay_in_seconds: int = 5
) -> None:
"""Publish `event` into Pika queue.
Perform `retries` publish attempts with `retry_delay_in_seconds` between them.
"""
body = json.dumps(event)
while retries:
# noinspection PyBroadException
try:
self._publish(body)
return
except Exception as e:
logger.error(
"Could not open Pika channel at host '{}'. Failed with error: "
"{}".format(self.host, e)
)
self.channel = None
retries -= 1
time.sleep(retry_delay_in_seconds)
logger.error(
"Failed to publish Pika event to queue '{}' on host "
"'{}':\n{}".format(self.queue, self.host, body)
)
def _publish(self, body: Text) -> None:
if self._pika_connection.is_closed:
# Try to reset connection
self._pika_connection = self._get_connection()
self._run_pika_io_loop_in_thread()
elif not self.channel:
logger.warning(
"Rabbit MQ channel was not assigned yet. Adding message to "
"list of unpublished messages and trying to publish them "
"later."
)
self._unpublished_messages.append(body)
else:
self.channel.basic_publish("", self.queue, body)
logger.debug(
"Published Pika events to queue '{}' on host "
"'{}':\n{}".format(self.queue, self.host, body)
)