: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
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",
}
def run(self):
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,
+ self.aiomain_task = asyncio.ensure_future(self.msg.aioread(("ns", "nsi"), 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
+ # 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()
"""
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:
but not immediately.
:return: None
"""
+ self.to_terminate = True
self.loop.call_soon_threadsafe(self.aiomain_task.cancel)