From c7f55a29517448bd60866d065f5676f760e0b05a Mon Sep 17 00:00:00 2001 From: Lluis Gifre <lluis.gifre@cttc.es> Date: Fri, 29 Oct 2021 19:43:05 +0200 Subject: [PATCH] Minor corrections and fixes in main files. --- .../service/__main__.py | 40 +++++++++---------- src/compute/service/__main__.py | 22 +++++----- src/context/service/__main__.py | 2 +- src/service/service/__main__.py | 2 +- 4 files changed, 32 insertions(+), 34 deletions(-) diff --git a/src/centralizedattackdetector/service/__main__.py b/src/centralizedattackdetector/service/__main__.py index da571acb4..5737402c0 100644 --- a/src/centralizedattackdetector/service/__main__.py +++ b/src/centralizedattackdetector/service/__main__.py @@ -1,51 +1,49 @@ -import logging, os, signal, sys, threading +import logging, signal, sys, threading from prometheus_client import start_http_server -from common.database.Factory import get_database +from common.Settings import get_setting +from centralizedattackdetector.Config import ( + GRPC_SERVICE_PORT, GRPC_MAX_WORKERS, GRPC_GRACE_PERIOD, LOG_LEVEL, METRICS_PORT) from centralizedattackdetector.service.CentralizedAttackDetectorService import CentralizedAttackDetectorService -from centralizedattackdetector.Config import GRPC_SERVICE_PORT, GRPC_MAX_WORKERS, GRPC_GRACE_PERIOD, LOG_LEVEL, METRICS_PORT terminate = threading.Event() -logger = None +LOGGER = None -def signal_handler(signal, frame): - global terminate, logger - logger.warning('Terminate signal received') +def signal_handler(signal, frame): # pylint: disable=redefined-outer-name + LOGGER.warning('Terminate signal received') terminate.set() def main(): - global terminate, logger + global LOGGER # pylint: disable=global-statement - service_port = os.environ.get('CENTRALIZEDATTACKDETECTORSERVICE_SERVICE_PORT_GRPC', GRPC_SERVICE_PORT) - max_workers = os.environ.get('MAX_WORKERS', GRPC_MAX_WORKERS ) - grace_period = os.environ.get('GRACE_PERIOD', GRPC_GRACE_PERIOD) - log_level = os.environ.get('LOG_LEVEL', LOG_LEVEL ) - metrics_port = os.environ.get('METRICS_PORT', METRICS_PORT ) + service_port = get_setting('CENTRALIZEDATTACKDETECTORSERVICE_SERVICE_PORT_GRPC', default=GRPC_SERVICE_PORT) + max_workers = get_setting('MAX_WORKERS', default=GRPC_MAX_WORKERS ) + grace_period = get_setting('GRACE_PERIOD', default=GRPC_GRACE_PERIOD) + log_level = get_setting('LOG_LEVEL', default=LOG_LEVEL ) + metrics_port = get_setting('METRICS_PORT', default=METRICS_PORT ) logging.basicConfig(level=log_level) - logger = logging.getLogger(__name__) + LOGGER = logging.getLogger(__name__) signal.signal(signal.SIGINT, signal_handler) signal.signal(signal.SIGTERM, signal_handler) - logger.info('Starting...') + LOGGER.info('Starting...') # Start metrics server start_http_server(metrics_port) - # Get database instance - database = get_database() - # Starting CentralizedCybersecurity service - grpc_service = CentralizedAttackDetectorService(database, port=service_port, max_workers=max_workers, grace_period=grace_period) + grpc_service = CentralizedAttackDetectorService( + port=service_port, max_workers=max_workers, grace_period=grace_period) grpc_service.start() # Wait for Ctrl+C or termination signal while not terminate.wait(timeout=0.1): pass - logger.info('Terminating...') + LOGGER.info('Terminating...') grpc_service.stop() - logger.info('Bye') + LOGGER.info('Bye') return 0 if __name__ == '__main__': diff --git a/src/compute/service/__main__.py b/src/compute/service/__main__.py index ec6902175..f45af374c 100644 --- a/src/compute/service/__main__.py +++ b/src/compute/service/__main__.py @@ -1,22 +1,22 @@ import logging, signal, sys, threading from prometheus_client import start_http_server from common.Settings import get_setting -from compute.Config import GRPC_SERVICE_PORT, GRPC_MAX_WORKERS, GRPC_GRACE_PERIOD, LOG_LEVEL, RESTAPI_SERVICE_PORT, \ - RESTAPI_BASE_URL, METRICS_PORT +from compute.Config import ( + GRPC_SERVICE_PORT, GRPC_MAX_WORKERS, GRPC_GRACE_PERIOD, LOG_LEVEL, RESTAPI_SERVICE_PORT, RESTAPI_BASE_URL, + METRICS_PORT) from compute.service.ComputeService import ComputeService from compute.service.rest_server.Server import Server from compute.service.rest_server.resources.Compute import Compute terminate = threading.Event() -logger = None +LOGGER = None -def signal_handler(signal, frame): - global terminate, logger - logger.warning('Terminate signal received') +def signal_handler(signal, frame): # pylint: disable=redefined-outer-name + LOGGER.warning('Terminate signal received') terminate.set() def main(): - global terminate, logger + global LOGGER # pylint: disable=global-statement grpc_service_port = get_setting('COMPUTESERVICE_SERVICE_PORT_GRPC', default=GRPC_SERVICE_PORT ) max_workers = get_setting('MAX_WORKERS', default=GRPC_MAX_WORKERS ) @@ -27,12 +27,12 @@ def main(): metrics_port = get_setting('METRICS_PORT', default=METRICS_PORT ) logging.basicConfig(level=log_level) - logger = logging.getLogger(__name__) + LOGGER = logging.getLogger(__name__) signal.signal(signal.SIGINT, signal_handler) signal.signal(signal.SIGTERM, signal_handler) - logger.info('Starting...') + LOGGER.info('Starting...') # Start metrics server start_http_server(metrics_port) @@ -49,12 +49,12 @@ def main(): # Wait for Ctrl+C or termination signal while not terminate.wait(timeout=0.1): pass - logger.info('Terminating...') + LOGGER.info('Terminating...') grpc_service.stop() rest_server.shutdown() rest_server.join() - logger.info('Bye') + LOGGER.info('Bye') return 0 if __name__ == '__main__': diff --git a/src/context/service/__main__.py b/src/context/service/__main__.py index dbda296fa..495c203c9 100644 --- a/src/context/service/__main__.py +++ b/src/context/service/__main__.py @@ -16,7 +16,7 @@ from context.service.rest_server.Resources import RESOURCES terminate = threading.Event() LOGGER = None -def signal_handler(signal, frame): +def signal_handler(signal, frame): # pylint: disable=redefined-outer-name LOGGER.warning('Terminate signal received') terminate.set() diff --git a/src/service/service/__main__.py b/src/service/service/__main__.py index fdf602c39..7de072b00 100644 --- a/src/service/service/__main__.py +++ b/src/service/service/__main__.py @@ -7,7 +7,7 @@ from service.Config import GRPC_SERVICE_PORT, GRPC_MAX_WORKERS, GRPC_GRACE_PERIO terminate = threading.Event() LOGGER = None -def signal_handler(signal, frame): +def signal_handler(signal, frame): # pylint: disable=redefined-outer-name LOGGER.warning('Terminate signal received') terminate.set() -- GitLab