Browse Source

Merge "Add mechanism that send msg again if request fail"

Zuul 7 months ago
parent
commit
29b2d3036a
1 changed files with 28 additions and 10 deletions
  1. 28
    10
      monasca_events_api/app/common/events_publisher.py

+ 28
- 10
monasca_events_api/app/common/events_publisher.py View File

@@ -15,6 +15,7 @@
15 15
 
16 16
 import falcon
17 17
 from monasca_common.kafka import producer
18
+from monasca_common.kafka_lib.common import FailedPayloadsError
18 19
 from monasca_common.rest import utils as rest_utils
19 20
 from oslo_log import log
20 21
 
@@ -149,16 +150,33 @@ class EventPublisher(object):
149 150
 
150 151
         LOG.debug('Publishing %d messages', num_of_msg)
151 152
 
152
-        try:
153
-            for topic in self._topics:
154
-                self._kafka_publisher.publish(
155
-                    topic,
156
-                    messages
157
-                )
158
-                LOG.debug('Sent %d messages to topic %s', num_of_msg, topic)
159
-        except Exception as ex:
160
-            raise falcon.HTTPServiceUnavailable('Service unavailable',
161
-                                                str(ex), 60)
153
+        first = True
154
+        while True:
155
+            try:
156
+                for topic in self._topics:
157
+                    self._kafka_publisher.publish(
158
+                        topic,
159
+                        messages
160
+                    )
161
+                    LOG.debug('Sent %d messages to topic %s',
162
+                              num_of_msg, topic)
163
+                break
164
+            except FailedPayloadsError as ex:
165
+                # FailedPayloadsError exception can be cause by connection
166
+                # problem, to make sure that is not connection issue
167
+                # message is sent again.
168
+                LOG.error('Failed to send messages %s', ex)
169
+                if first:
170
+                    LOG.error('Retrying')
171
+                    first = False
172
+                    continue
173
+                else:
174
+                    raise falcon.HTTPServiceUnavailable('Service unavailable',
175
+                                                        str(ex), 60)
176
+            except Exception as ex:
177
+                LOG.error('Failed to send messages %s', ex)
178
+                raise falcon.HTTPServiceUnavailable('Service unavailable',
179
+                                                    str(ex), 60)
162 180
 
163 181
     def _check_if_all_messages_was_publish(self, send_count, to_send_count):
164 182
         """Executed after publishing to sent metrics.

Loading…
Cancel
Save