@@ -203,7 +203,6 @@ def publish(self, event: Event):
203
203
try :
204
204
# Ensure connection is established before publishing.
205
205
if not self ._channel or not self ._connection :
206
- logging .error ("RabbitMQ publisher channel is closed, reconnecting..." )
207
206
self .connect ()
208
207
209
208
# Setting durable=True ensures that the queue will survive.
@@ -230,25 +229,30 @@ def publish(self, event: Event):
230
229
# Only retry on recoverable exceptions.
231
230
# AMQPConnectionError is assumed to be related to network issues,
232
231
# or temporary unavailable host.
233
- logging .error (
234
- "Failed to publish event: %s. Error: %s. Attempt: %d" ,
235
- message ,
236
- str (e ),
237
- attempt ,
238
- )
239
232
self ._handle_publish_error ()
240
233
attempt += 1
241
- if attempt <= self ._num_tries :
234
+ if attempt < self ._num_tries :
242
235
time .sleep (2 ** attempt )
236
+ else :
237
+ logging .error (
238
+ "Failed to publish event: %s after %d attempts. Error: %s." ,
239
+ message ,
240
+ attempt ,
241
+ str (e ),
242
+ )
243
243
except Exception as e : # pylint: disable=broad-except
244
- # Unknown errors. Don't retry. Log to avoid crashing clients.
245
- logging .error (
246
- "Unknown error. Failed to publish event: %s. Error: %s." , message , str (e )
247
- )
248
244
self ._handle_publish_error ()
249
245
attempt += 1
250
- if attempt <= self ._num_tries :
246
+ if attempt < self ._num_tries :
251
247
time .sleep (2 ** attempt )
248
+ else :
249
+ # Unknown errors. Don't retry. Log to avoid crashing clients.
250
+ logging .error (
251
+ "Unknown error. Failed to publish event: %s after %d attempts. Error: %s." ,
252
+ message ,
253
+ attempt ,
254
+ str (e ),
255
+ )
252
256
253
257
def _handle_publish_error (self ):
254
258
"""Handle publish errors with retrying on connection issue."""
0 commit comments