Improves handling of database connections 36/7536/2
authorBenjamin Diaz <bdiaz@whitestack.com>
Thu, 23 May 2019 17:19:06 +0000 (14:19 -0300)
committerBenjamin Diaz <bdiaz@whitestack.com>
Thu, 23 May 2019 17:31:04 +0000 (14:31 -0300)
Change-Id: Ice70378bb0bad3566f51b65476eafcfdac3c991c
Signed-off-by: Benjamin Diaz <bdiaz@whitestack.com>
osm_policy_module/alarming/service.py

index 590b06a..b916724 100644 (file)
@@ -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()