From: Atul Agarwal Date: Mon, 14 Jun 2021 10:46:09 +0000 (+0000) Subject: Resolved Bug 1569 - Unable to subscribe to Kafka topics X-Git-Tag: release-v11.0-start~7 X-Git-Url: https://osm.etsi.org/gitweb/?p=osm%2FMON.git;a=commitdiff_plain;h=a353daa53d1b4d8da6fcbfefd40f309a5cf33688 Resolved Bug 1569 - Unable to subscribe to Kafka topics Change-Id: I6386869b855d559273b9e82bafa1c69aca43cbe9 Signed-off-by: Atul Agarwal --- diff --git a/osm_mon/dashboarder/dashboarder.py b/osm_mon/dashboarder/dashboarder.py index bed157a..b25a8d9 100644 --- a/osm_mon/dashboarder/dashboarder.py +++ b/osm_mon/dashboarder/dashboarder.py @@ -47,14 +47,20 @@ class Dashboarder: def run(self): self.loop.run_until_complete(self.start()) - async def start(self): + async def start(self, wait_time=5): topics = ["users", "project"] - try: - await self.msg_bus.aioread(topics, self._user_msg) - except Exception as e: - # Failed to subscribe to kafka topics - log.error("Error when subscribing to topics %s", str(topics)) - log.exception("Exception %s", str(e)) + while True: + try: + await self.msg_bus.aioread(topics, self._user_msg) + log.info("Sucessfully subscribed to kafka topic(s) %s", str(topics)) + break + except Exception as e: + # Failed to subscribe to kafka topics + log.error("Error when subscribing to topic(s) %s", str(topics)) + log.exception("Exception %s", str(e)) + # Wait for some time for kaka to stabilize and then reattempt to subscribe again + time.sleep(wait_time) + log.info("Retrying to subscribe the kafka topic(s) %s", str(topics)) async def _user_msg(self, topic, key, values): log.debug( diff --git a/osm_mon/server/server.py b/osm_mon/server/server.py index 62721ff..962a6f9 100755 --- a/osm_mon/server/server.py +++ b/osm_mon/server/server.py @@ -26,6 +26,7 @@ MON component in charge of CRUD operations for vim_accounts and alarms. It uses import asyncio import json import logging +import time from osm_mon.core.config import Config from osm_mon.core.message_bus_client import MessageBusClient @@ -48,14 +49,20 @@ class Server: def run(self): self.loop.run_until_complete(self.start()) - async def start(self): + async def start(self, wait_time=5): topics = ["alarm_request"] - try: - await self.msg_bus.aioread(topics, self._process_msg) - except Exception as e: - # Failed to subscribe to kafka topic - log.exception("Error when subscribing to topics %s", str(topics)) - raise e + while True: + try: + await self.msg_bus.aioread(topics, self._process_msg) + log.info("Sucessfully subscribed to kafka topic(s) %s", str(topics)) + break + except Exception as e: + # Failed to subscribe to kafka topic + log.error("Error when subscribing to topic(s) %s", str(topics)) + log.exception("Exception %s", str(e)) + # Wait for some time for kaka to stabilize and then reattempt to subscribe again + time.sleep(wait_time) + log.info("Retrying to subscribe the kafka topic(s) %s", str(topics)) async def _process_msg(self, topic, key, values): log.info("Message arrived: %s", values)