From acac7559b430f370dd06d09a40a3a387e440f922 Mon Sep 17 00:00:00 2001 From: Benjamin Diaz Date: Thu, 23 May 2019 14:19:06 -0300 Subject: [PATCH] Improves handling of database connections Change-Id: Ice70378bb0bad3566f51b65476eafcfdac3c991c Signed-off-by: Benjamin Diaz --- osm_policy_module/alarming/service.py | 54 ++++++++++++++------------- 1 file changed, 28 insertions(+), 26 deletions(-) diff --git a/osm_policy_module/alarming/service.py b/osm_policy_module/alarming/service.py index 590b06a..b916724 100644 --- a/osm_policy_module/alarming/service.py +++ b/osm_policy_module/alarming/service.py @@ -52,9 +52,9 @@ class AlarmingService: async def configure_vnf_alarms(self, nsr_id: str): log.info("Configuring vnf alarms for network service %s", nsr_id) alarms_created = [] - database.db.connect(reuse_if_open=True) - with database.db.atomic() as tx: - try: + database.db.connect() + try: + with database.db.atomic(): vnfrs = self.db_client.get_vnfrs(nsr_id) for vnfr in vnfrs: log.debug("Processing vnfr: %s", vnfr) @@ -112,24 +112,27 @@ class AlarmingService: ) alarms_created.append(alarm) - except Exception as e: - log.exception("Error configuring VNF alarms:") - tx.rollback() - if len(alarms_created) > 0: - log.debug("Cleaning alarm resources in MON") - for alarm in alarms_created: + except Exception as e: + log.exception("Error configuring VNF alarms:") + if len(alarms_created) > 0: + log.debug("Cleaning alarm resources in MON") + for alarm in alarms_created: + try: await self.mon_client.delete_alarm(alarm.nsr_id, alarm.vnf_member_index, alarm.vdu_name, alarm.alarm_uuid) - raise e - database.db.close() + except ValueError: + log.exception("Error deleting alarm in MON %s", alarm.alarm_uuid) + raise e + finally: + database.db.close() async def delete_orphaned_alarms(self, nsr_id): log.info("Deleting orphaned vnf alarms for network service %s", nsr_id) database.db.connect() - with database.db.atomic() as tx: - try: + try: + with database.db.atomic(): for alarm in VnfAlarmRepository.list(VnfAlarm.nsr_id == nsr_id): try: self.db_client.get_vdur(nsr_id, alarm.vnf_member_index, alarm.vdu_name) @@ -144,18 +147,17 @@ class AlarmingService: except ValueError: log.exception("Error deleting alarm in MON %s", alarm.alarm_uuid) alarm.delete_instance() - - except Exception as e: - log.exception("Error deleting orphaned alarms:") - tx.rollback() - raise e - database.db.close() + except Exception as e: + log.exception("Error deleting orphaned alarms:") + raise e + finally: + database.db.close() async def delete_vnf_alarms(self, nsr_id): log.info("Deleting vnf alarms for network service %s", nsr_id) database.db.connect() - with database.db.atomic() as tx: - try: + try: + with database.db.atomic(): for alarm in VnfAlarmRepository.list(VnfAlarm.nsr_id == nsr_id): log.debug("Deleting vnf alarm %s", alarm.alarm_uuid) try: @@ -168,11 +170,11 @@ class AlarmingService: log.exception("Error deleting alarm in MON %s", alarm.alarm_uuid) alarm.delete_instance() - except Exception as e: - log.exception("Error deleting orphaned alarms:") - tx.rollback() - raise e - database.db.close() + except Exception as e: + log.exception("Error deleting vnf alarms:") + raise e + finally: + database.db.close() async def handle_alarm(self, alarm_uuid: str, status: str, payload: dict): database.db.connect() -- 2.25.1