From ae0398a5de60fbdf55e735641dda554dc8057d4a Mon Sep 17 00:00:00 2001
From: fjmmuro <francisco.moreno@atos.net>
Date: Mon, 7 Nov 2022 17:15:57 +0100
Subject: [PATCH] Clean code

---
 src/monitoring/service/AlarmManager.py        | 3 ++-
 src/monitoring/service/SubscriptionManager.py | 3 ++-
 src/monitoring/tests/test_unitary.py          | 7 ++++++-
 3 files changed, 10 insertions(+), 3 deletions(-)

diff --git a/src/monitoring/service/AlarmManager.py b/src/monitoring/service/AlarmManager.py
index 27b169b16..d80d815fe 100644
--- a/src/monitoring/service/AlarmManager.py
+++ b/src/monitoring/service/AlarmManager.py
@@ -24,11 +24,12 @@ class AlarmManager():
             start_date = datetime.utcfromtimestamp(start_timestamp).isoformat()
             end_date = datetime.utcfromtimestamp(end_timestamp).isoformat()
 
-        self.scheduler.add_job(self.metrics_db.get_alarm_data,
+        job = self.scheduler.add_job(self.metrics_db.get_alarm_data,
                                args=(alarm_queue,kpi_id, kpiMinValue, kpiMaxValue, inRange, includeMinValue, includeMaxValue, subscription_frequency_ms),
                                trigger='interval', seconds=(subscription_frequency_ms/1000), start_date=start_date,
                                end_date=end_date,timezone=pytz.utc, id=str(alarm_id))
         LOGGER.debug(f"Alarm job {alarm_id} succesfully created")
+        job.remove()
 
     def delete_alarm(self, alarm_id):
         try:
diff --git a/src/monitoring/service/SubscriptionManager.py b/src/monitoring/service/SubscriptionManager.py
index f76cf8c39..6ff922c52 100644
--- a/src/monitoring/service/SubscriptionManager.py
+++ b/src/monitoring/service/SubscriptionManager.py
@@ -42,10 +42,11 @@ class SubscriptionManager():
         if end_timestamp:
             end_date = datetime.utcfromtimestamp(end_timestamp).isoformat()
 
-        self.scheduler.add_job(self.metrics_db.get_subscription_data, args=(subs_queue,kpi_id, sampling_interval_s),
+        job = self.scheduler.add_job(self.metrics_db.get_subscription_data, args=(subs_queue,kpi_id, sampling_interval_s),
                                trigger='interval', seconds=sampling_interval_s, start_date=start_date,
                                end_date=end_date, timezone=pytz.utc, id=str(subscription_id))
         LOGGER.debug(f"Subscrition job {subscription_id} succesfully created")
+        job.remove()
 
     def delete_subscription(self, subscription_id):
         self.scheduler.remove_job(subscription_id)
diff --git a/src/monitoring/tests/test_unitary.py b/src/monitoring/tests/test_unitary.py
index 823998899..55ac9a18b 100644
--- a/src/monitoring/tests/test_unitary.py
+++ b/src/monitoring/tests/test_unitary.py
@@ -22,6 +22,7 @@ from typing import Tuple
 
 from apscheduler.executors.pool import ProcessPoolExecutor
 from apscheduler.schedulers.background import BackgroundScheduler
+from apscheduler.schedulers.base import STATE_STOPPED
 from grpc._channel import _MultiThreadedRendezvous
 
 from common.Constants import ServiceNameEnum
@@ -299,6 +300,8 @@ def test_set_kpi_subscription(monitoring_client,metrics_db,subs_scheduler): # py
     for item in response:
         LOGGER.debug(item)
         assert isinstance(item, SubsResponse)
+    if (subs_scheduler.state != STATE_STOPPED):
+        subs_scheduler.shutdown()
 
 # Test case that makes use of client fixture to test server's GetSubsDescriptor method
 def test_get_subs_descriptor(monitoring_client):
@@ -364,6 +367,9 @@ def test_get_alarm_response_stream(monitoring_client,subs_scheduler):
         LOGGER.debug(item)
         assert isinstance(item,AlarmResponse)
 
+    if(subs_scheduler.state != STATE_STOPPED):
+        subs_scheduler.shutdown()
+
 # Test case that makes use of client fixture to test server's DeleteAlarm method
 def test_delete_alarm(monitoring_client):
     LOGGER.warning('test_delete_alarm')
@@ -390,7 +396,6 @@ def test_get_stream_kpi(monitoring_client): # pylint: disable=redefined-outer-na
 #     LOGGER.debug(response)
 #     assert isinstance(response, Kpi)
 
-
 def test_managementdb_tools_insert_kpi(management_db): # pylint: disable=redefined-outer-name
     LOGGER.warning('test_managementdb_tools_insert_kpi begin')
     _create_kpi_request = create_kpi_request()
-- 
GitLab