From a0adf03e0aec7c275ecc7d2afc3e803db048dc7d Mon Sep 17 00:00:00 2001
From: kpoulakakis <kpoulakakis@ubitech.eu>
Date: Tue, 4 Jun 2024 12:18:25 +0300
Subject: [PATCH] Some changes for starting the app.

---
 src/automation/service/AutomationService.py   |  5 ++-
 .../service/AutomationServiceServicerImpl.py  | 17 +++++----
 src/automation/service/__main__.py            | 36 +++++++++++++++----
 3 files changed, 43 insertions(+), 15 deletions(-)

diff --git a/src/automation/service/AutomationService.py b/src/automation/service/AutomationService.py
index 52180e5bb..4ff0beb3a 100644
--- a/src/automation/service/AutomationService.py
+++ b/src/automation/service/AutomationService.py
@@ -17,13 +17,12 @@ from common.Settings import get_service_port_grpc
 from common.proto.automation_pb2_grpc import add_AutomationServiceServicer_to_server
 from common.tools.service.GenericGrpcService import GenericGrpcService
 from automation.service.AutomationServiceServicerImpl import AutomationServiceServicerImpl
-from automation.service.NameMapping import NameMapping
 
 class AutomationService(GenericGrpcService):
-    def __init__(self, name_mapping : NameMapping, cls_name: str = __name__) -> None:
+    def __init__(self, cls_name: str = __name__) -> None:
         port = get_service_port_grpc(ServiceNameEnum.AUTOMATION)
         super().__init__(port, cls_name=cls_name)
-        self.automation_servicer = AutomationServiceServicerImpl(name_mapping)
+        self.automation_servicer = AutomationServiceServicerImpl()
 
     def install_servicers(self):
         add_AutomationServiceServicer_to_server(self.automation_servicer, self.server)
diff --git a/src/automation/service/AutomationServiceServicerImpl.py b/src/automation/service/AutomationServiceServicerImpl.py
index 157fdf503..173cb89e3 100644
--- a/src/automation/service/AutomationServiceServicerImpl.py
+++ b/src/automation/service/AutomationServiceServicerImpl.py
@@ -13,27 +13,32 @@
 # limitations under the License.
 
 import logging, os, grpc
-
+from common.method_wrappers.Decorator import MetricsPool, safe_and_metered_rpc_method
+from common.method_wrappers.Decorator import MetricsPool
 from common.proto.automation_pb2_grpc import AutomationServiceServicer
 
+LOGGER = logging.getLogger(__name__)
+METRICS_POOL = MetricsPool('Automation', 'RPC')
+
 class AutomationServiceServicerImpl(AutomationServiceServicer):
 
-    @safe_and_metered_rpc_method(LOGGER)
+    @safe_and_metered_rpc_method(METRICS_POOL,LOGGER)
     def ZSMCreate(self) -> None:
         LOGGER.info('NOT IMPLEMENTED ZSMCreate')
 
-    @safe_and_metered_rpc_method(LOGGER)
+    @safe_and_metered_rpc_method(METRICS_POOL,LOGGER)
     def ZSMUpdate(self) -> None:
         LOGGER.info('NOT IMPLEMENTED ZSMUpdate')
 
-    @safe_and_metered_rpc_method(LOGGER)
+    @safe_and_metered_rpc_method(METRICS_POOL,LOGGER)
     def ZSMDelete(self) -> None:
         LOGGER.info('NOT IMPLEMENTED ZSMDelete')
 
-    @safe_and_metered_rpc_method(LOGGER)
+    @safe_and_metered_rpc_method(METRICS_POOL,LOGGER)
     def ZSMGetById(self) -> None:
         LOGGER.info('NOT IMPLEMENTED ZSMGetById')
 
-    @safe_and_metered_rpc_method(LOGGER)
+
+    @safe_and_metered_rpc_method(METRICS_POOL,LOGGER)
     def ZSMGetByService(self) -> None:
         LOGGER.info('NOT IMPLEMENTED ZSMGetByService')
diff --git a/src/automation/service/__main__.py b/src/automation/service/__main__.py
index a1a2c9941..ef0f107b4 100644
--- a/src/automation/service/__main__.py
+++ b/src/automation/service/__main__.py
@@ -12,18 +12,42 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-import logging,sys
+import logging, signal, sys, threading
+from prometheus_client import start_http_server
+from common.Settings import get_log_level, get_metrics_port
+from .AutomationService import AutomationService
 
-LOGGER : logging.Logger = None
+LOG_LEVEL = get_log_level()
+logging.basicConfig(level=LOG_LEVEL, format="[%(asctime)s] %(levelname)s:%(name)s:%(message)s")
+LOGGER = logging.getLogger(__name__)
+
+terminate = threading.Event()
+
+def signal_handler(signal, frame): # pylint: disable=redefined-outer-name,unused-argument
+    LOGGER.warning('Terminate signal received')
+    terminate.set()
 
 def main():
-    global LOGGER # pylint: disable=global-statement
-    LOGGER = logging.getLogger(__name__)
+    LOGGER.info('Starting...')
+    signal.signal(signal.SIGINT,  signal_handler)
+    signal.signal(signal.SIGTERM, signal_handler)
+
+    # Start metrics server
+    metrics_port = get_metrics_port()
+    start_http_server(metrics_port)
+
+    # Starting context service
+    grpc_service = AutomationService()
+    grpc_service.start()
+
+    # Wait for Ctrl+C or termination signal
+    while not terminate.wait(timeout=1.0): pass
 
-    LOGGER.info('Starting Tests...')
+    LOGGER.info('Terminating...')
+    grpc_service.stop()
 
     LOGGER.info('Bye')
     return 0
 
 if __name__ == '__main__':
-    sys.exit(main())
+    sys.exit(main())
\ No newline at end of file
-- 
GitLab