12
12
from rasa .constants import ENV_LOG_LEVEL_LIBRARIES , DEFAULT_LOG_LEVEL_LIBRARIES
13
13
from rasa .core .brokers .event_channel import EventChannel
14
14
from rasa .utils .endpoints import EndpointConfig
15
+ from rasa .utils .io import DEFAULT_ENCODING
15
16
16
17
if typing .TYPE_CHECKING :
17
18
from pika .adapters .blocking_connection import BlockingChannel
18
- from pika import SelectConnection , BlockingConnection
19
+ from pika import SelectConnection , BlockingConnection , BasicProperties
19
20
from pika .channel import Channel
20
21
from pika .connection import Parameters , Connection
21
22
@@ -139,16 +140,16 @@ def initialise_pika_channel(
139
140
"""Initialise a Pika channel with a durable queue.
140
141
141
142
Args:
142
- host: Pika host
143
- queue: Pika queue to declare
144
- username: username for authentication with Pika host
145
- password: password for authentication with Pika host
146
- port: port of the Pika host
147
- connection_attempts: number of channel attempts before giving up
148
- retry_delay_in_seconds: delay in seconds between channel attempts
143
+ host: Pika host.
144
+ queue: Pika queue to declare.
145
+ username: Username for authentication with Pika host.
146
+ password: Password for authentication with Pika host.
147
+ port: port of the Pika host.
148
+ connection_attempts: Number of channel attempts before giving up.
149
+ retry_delay_in_seconds: Delay in seconds between channel attempts.
149
150
150
151
Returns:
151
- Pika `BlockingChannel` with declared queue
152
+ Pika `BlockingChannel` with declared queue.
152
153
153
154
"""
154
155
@@ -206,6 +207,17 @@ def __init__(
206
207
ENV_LOG_LEVEL_LIBRARIES , DEFAULT_LOG_LEVEL_LIBRARIES
207
208
),
208
209
):
210
+ """RabbitMQ event producer.
211
+
212
+ Args:
213
+ host: Pika host.
214
+ username: Username for authentication with Pika host.
215
+ password: Password for authentication with Pika host.
216
+ port: port of the Pika host.
217
+ queue: Pika queue to declare.
218
+ loglevel: Logging level.
219
+
220
+ """
209
221
logging .getLogger ("pika" ).setLevel (loglevel )
210
222
211
223
self .queue = queue
@@ -224,6 +236,10 @@ def __del__(self) -> None:
224
236
close_pika_channel (self .channel )
225
237
close_pika_connection (self .channel .connection )
226
238
239
+ @property
240
+ def rasa_environment (self ) -> Optional [Text ]:
241
+ return os .environ .get ("RASA_ENVIRONMENT" )
242
+
227
243
@classmethod
228
244
def from_endpoint_config (
229
245
cls , broker_config : Optional ["EndpointConfig" ]
@@ -285,7 +301,6 @@ def publish(
285
301
body = json .dumps (event )
286
302
287
303
while retries :
288
- # noinspection PyBroadException
289
304
try :
290
305
self ._publish (body )
291
306
return
@@ -304,6 +319,22 @@ def publish(
304
319
"'{}':\n {}" .format (self .queue , self .host , body )
305
320
)
306
321
322
+ @property
323
+ def _message_properties (self ) -> "BasicProperties" :
324
+ """Create RabbitMQ message properties.
325
+
326
+ Returns:
327
+ pika.spec.BasicProperties with the `RASA_ENVIRONMENT` environment
328
+ variable as the properties' `app_id` value. If this variable is unset, empty
329
+ pika.spec.BasicProperties.
330
+
331
+ """
332
+ from pika .spec import BasicProperties
333
+
334
+ kwargs = {"app_id" : self .rasa_environment } if self .rasa_environment else {}
335
+
336
+ return BasicProperties (** kwargs )
337
+
307
338
def _publish (self , body : Text ) -> None :
308
339
if self ._pika_connection .is_closed :
309
340
# Try to reset connection
@@ -317,7 +348,12 @@ def _publish(self, body: Text) -> None:
317
348
)
318
349
self ._unpublished_messages .append (body )
319
350
else :
320
- self .channel .basic_publish ("" , self .queue , body )
351
+ self .channel .basic_publish (
352
+ "" ,
353
+ self .queue ,
354
+ body .encode (DEFAULT_ENCODING ),
355
+ properties = self ._message_properties ,
356
+ )
321
357
322
358
logger .debug (
323
359
f"Published Pika events to queue '{ self .queue } ' on host "
0 commit comments