Index: cloud_sync.py =================================================================== diff -u -r57c12353bf0c2038da88b24dcaddc2a9d19a730b -r3736c29ba097740b4e99684625e78bfaaac6b1a1 --- cloud_sync.py (.../cloud_sync.py) (revision 57c12353bf0c2038da88b24dcaddc2a9d19a730b) +++ cloud_sync.py (.../cloud_sync.py) (revision 3736c29ba097740b4e99684625e78bfaaac6b1a1) @@ -1,65 +1,37 @@ +from cloudsync.handlers.error import Error +from cloudsync.utils.heartbeat import HeartBeatProvider +from cloudsync.busses.file_input_bus import FileInputBus +from cloudsync.handlers.ui_cs_request_handler import UICSMessageHandler +from cloudsync.handlers.cs_mft_dcs_request_handler import NetworkRequestHandler +from cloudsync.handlers.error_handler import ErrorHandler +from cloudsync.busses.file_output_bus import FileOutputBus +from cloudsync.utils.reachability import ReachabilityProvider +from cloudsync.utils.globals import * +from cloudsync.utils.helpers import * +from cloudsync.utils.logging import LoggingConfig +from flask_restplus import Api, Resource, fields, reqparse +from flask import Flask, Response, request import os import sys import logging from logging.handlers import TimedRotatingFileHandler +from cloudsync.handlers.logs_handler import CustomTimedRotatingFileHandlerHandler import werkzeug werkzeug.cached_property = werkzeug.utils.cached_property -from flask import Flask, Response, request -from flask_restplus import Api, Resource, fields, reqparse -from cloudsync.utils.helpers import * -from cloudsync.utils.globals import * -from cloudsync.utils.reachability import ReachabilityProvider -from cloudsync.busses.file_output_bus import FileOutputBus -from cloudsync.handlers.error_handler import ErrorHandler -from cloudsync.handlers.cs_mft_dcs_request_handler import NetworkRequestHandler -from cloudsync.handlers.ui_cs_request_handler import UICSMessageHandler -from cloudsync.busses.file_input_bus import FileInputBus -from cloudsync.utils.heartbeat import HeartBeatProvider -from cloudsync.handlers.error import Error +VERSION = "0.4.10" -VERSION = "0.4.9" - arguments = sys.argv -log_level = int(arguments[1]) -if not os.path.exists(CS_LOG_PATH): - os.makedirs(CS_LOG_PATH) - app = Flask(__name__) api = Api(app=app, version=VERSION, title="CloudSync Registration API", description="Interface with DIA Manufacturing Tool * DCS") -# Remove existing handlers -for handler in app.logger.handlers: - app.logger.removeHandler(handler) +logconf = LoggingConfig() +logconf.initiate(app=app) -# Create a TimedRotatingFileHandler that writes logs to a new file every midnight, in UTC time -handler = TimedRotatingFileHandler(CS_LOG_FILE, when="midnight", interval=1, utc=True) -handler.suffix = "%m-%d-%Y" - -# Set the log level -handler.setLevel(log_level) - -# Add a formatter -default_formatter = logging.Formatter('[%(asctime)s] %(levelname)s in %(module)s: %(message)s | {%(pathname)s:%(lineno)d}') -handler.setFormatter(default_formatter) - -# Add the handler to the logger -app.logger.addHandler(handler) -app.logger.setLevel(log_level) - -# Get the root logger -root_logger = logging.getLogger() - -# Add the handlers to the root logger -root_logger.addHandler(handler) -root_logger.setLevel(log_level) - -g_utils.add_logger(app.logger) - sleep(5) # wait for UI to prepare the configurations partition try: @@ -68,9 +40,19 @@ if g_config[CONFIG_DEVICE][CONFIG_DEVICE_MODE] == 'operation': g_config = helpers_read_config(OPERATION_CONFIG_FILE_PATH) CONFIG_PATH = OPERATION_CONFIG_FILE_PATH +except Exception as e: + g_utils.logger.error("Error reading config file - {0}".format(e)) + sys.exit(0) +try: reachability_provider = ReachabilityProvider(logger=app.logger, url_reachability=g_config[CONFIG_KEBORMED][CONFIG_KEBORMED_REACHABILITY_URL]) +except Exception as e: + g_utils.logger.error( + "Reachability URL missing from config file. Using Default URL - {0}".format(DEFAULT_REACHABILITY_URL)) + reachability_provider = ReachabilityProvider( + logger=app.logger, url_reachability=DEFAULT_REACHABILITY_URL) +try: g_utils.add_reachability_provider(reachability_provider=reachability_provider) output_channel = FileOutputBus(logger=app.logger, max_size=100, file_channels_path=UI2CS_FILE_CHANNELS_PATH) @@ -91,6 +73,11 @@ heartbeat_provider = HeartBeatProvider(logger=app.logger, network_request_handler=network_request_handler, output_channel=output_channel) + logconf.set_log_level(g_config[CONFIG_DEVICE][CONFIG_LOG_LEVEL]) + logconf.set_network_provider(network_request_handler=network_request_handler) + logconf.set_error_provider(error_handler=error_handler) + logconf.set_configuration(g_config=g_config) + if g_config[CONFIG_DEVICE][CONFIG_DEVICE_MODE] == 'operation': heartbeat_provider.send_heartbeat = True else: