X-Git-Url: https://osm.etsi.org/gitweb/?p=osm%2FNBI.git;a=blobdiff_plain;f=osm_nbi%2Fsubscriptions.py;h=39eed76f18e6a3e7403f347bddfc65f3cd8461a2;hp=64fc2bfbd3f0fd7f63279485b4bbdf3b4ad5e429;hb=0952a48159c11b6d31fff6617f04f06351ed79f3;hpb=932499c09d729d235ccd1fc002156b8b23e9f165;ds=sidebyside diff --git a/osm_nbi/subscriptions.py b/osm_nbi/subscriptions.py index 64fc2bf..39eed76 100644 --- a/osm_nbi/subscriptions.py +++ b/osm_nbi/subscriptions.py @@ -27,7 +27,7 @@ from http import HTTPStatus from osm_common import dbmongo, dbmemory, msglocal, msgkafka from osm_common.dbbase import DbException from osm_common.msgbase import MsgException -from engine import EngineException +from osm_nbi.engine import EngineException __author__ = "Alfonso Tierno " @@ -48,7 +48,7 @@ class SubscriptionThread(threading.Thread): :param engine: an instance of Engine class, used for deleting instances """ threading.Thread.__init__(self) - + self.to_terminate = False self.config = config self.db = None self.msg = None @@ -57,12 +57,42 @@ class SubscriptionThread(threading.Thread): self.logger = logging.getLogger("nbi.subscriptions") self.aiomain_task = None # asyncio task for receiving kafka bus self.internal_session = { # used for a session to the engine methods - "_id": "subscription", - "id": "subscription", - "project_id": "admin", - "admin": True + "project_id": (), + "set_project": (), + "admin": True, + "force": False, + "public": None, + "method": "delete", } + async def start_kafka(self): + # timeout_wait_for_kafka = 3*60 + kafka_working = True + while not self.to_terminate: + try: + # bug 710 635. The library aiokafka does not recieve anything when the topci at kafka has not been + # created. + # Before subscribe, send dummy messages + await self.msg.aiowrite("admin", "echo", "dummy message", loop=self.loop) + await self.msg.aiowrite("ns", "echo", "dummy message", loop=self.loop) + await self.msg.aiowrite("nsi", "echo", "dummy message", loop=self.loop) + if not kafka_working: + self.logger.critical("kafka is working again") + kafka_working = True + await asyncio.sleep(10, loop=self.loop) + self.aiomain_task = asyncio.ensure_future(self.msg.aioread(("ns", "nsi"), loop=self.loop, + callback=self._msg_callback), + loop=self.loop) + await asyncio.wait_for(self.aiomain_task, timeout=None, loop=self.loop) + except Exception as e: + if self.to_terminate: + return + if kafka_working: + # logging only first time + self.logger.critical("Error accessing kafka '{}'. Retrying ...".format(e)) + kafka_working = False + await asyncio.sleep(10, loop=self.loop) + def run(self): """ Start of the thread @@ -97,16 +127,15 @@ class SubscriptionThread(threading.Thread): raise SubscriptionException(str(e), http_code=e.http_code) self.logger.debug("Starting") - while True: + while not self.to_terminate: try: - self.aiomain_task = asyncio.ensure_future(self.msg.aioread("ns", loop=self.loop, - callback=self._msg_callback), - loop=self.loop) - self.loop.run_until_complete(self.aiomain_task) - except asyncio.CancelledError: - break # if cancelled it should end, breaking loop + + self.loop.run_until_complete(asyncio.ensure_future(self.start_kafka(), loop=self.loop)) + # except asyncio.CancelledError: + # break # if cancelled it should end, breaking loop except Exception as e: - self.logger.exception("Exception '{}' at messaging read loop".format(e), exc_info=True) + if not self.to_terminate: + self.logger.exception("Exception '{}' at messaging read loop".format(e), exc_info=True) self.logger.debug("Finishing") self._stop() @@ -122,12 +151,19 @@ class SubscriptionThread(threading.Thread): """ try: if topic == "ns": - if command == "terminated": + if command == "terminated" and params["operationState"] in ("COMPLETED", "PARTIALLY_COMPLETED"): self.logger.debug("received ns terminated {}".format(params)) if params.get("autoremove"): self.engine.del_item(self.internal_session, "nsrs", _id=params["nsr_id"]) self.logger.debug("ns={} deleted from database".format(params["nsr_id"])) return + if topic == "nsi": + if command == "terminated" and params["operationState"] in ("COMPLETED", "PARTIALLY_COMPLETED"): + self.logger.debug("received nsi terminated {}".format(params)) + if params.get("autoremove"): + self.engine.del_item(self.internal_session, "nsis", _id=params["nsir_id"]) + self.logger.debug("nsis={} deleted from database".format(params["nsir_id"])) + return except (EngineException, DbException, MsgException) as e: self.logger.error("Error while processing topic={} command={}: {}".format(topic, command, e)) except Exception as e: @@ -153,4 +189,6 @@ class SubscriptionThread(threading.Thread): but not immediately. :return: None """ - self.loop.call_soon_threadsafe(self.aiomain_task.cancel) + self.to_terminate = True + if self.aiomain_task: + self.loop.call_soon_threadsafe(self.aiomain_task.cancel)