From 1079cc8ff295ca11e65c2a864e41789ddc6ed205 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Fri, 13 Oct 2017 14:35:08 +0300 Subject: Add: wrap main program of controller in a try/except to log errors --- ick_controller.py | 62 ++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 39 insertions(+), 23 deletions(-) (limited to 'ick_controller.py') diff --git a/ick_controller.py b/ick_controller.py index 855b6ea..59ac86a 100644 --- a/ick_controller.py +++ b/ick_controller.py @@ -18,6 +18,7 @@ import logging import logging.handlers import os +import sys import apifw @@ -55,37 +56,52 @@ def load_config(filename, defconf): return actual_config -logger = logging.getLogger() -logger.setLevel(logging.DEBUG) -handler = logging.FileHandler('/tmp/ick.log') -logger.addHandler(handler) -logging.info('Starting ick controller main program') +def main(): + logger = logging.getLogger() + logger.setLevel(logging.DEBUG) + handler = logging.FileHandler('/tmp/ick.log') + logger.addHandler(handler) + logging.info('Starting ick controller main program') -config_filename = os.environ.get('ICK_CONTROLLER_CONFIG') -logging.info('config filename %r', config_filename) -if not config_filename: - logging.error('no ICK_CONTROLLER_CONFIG in environment') - raise Exception('No ICK_CONTROLLER_CONFIG defined in environment') + try: -logging.info('reading config from %r', config_filename) -config = load_config(config_filename, default_config) + config_filename = os.environ.get('ICK_CONTROLLER_CONFIG') + logging.info('config filename %r', config_filename) + if not config_filename: + logging.error('no ICK_CONTROLLER_CONFIG in environment') + raise Exception('No ICK_CONTROLLER_CONFIG defined in environment') -logging.info('config is %r', config) -ick2.setup_logging(config) + logging.info('reading config from %r', config_filename) + config = load_config(config_filename, default_config) -ick2.log.log('info', msg_text='Ick2 controller starts', config=config) + logging.info('config is %r', config) + ick2.setup_logging(config) -api = ick2.ControllerAPI() -ick2.log.log('info', msg_text='created ControllerAPI') + ick2.log.log('info', msg_text='Ick2 controller starts', config=config) -api.set_state_directory(config['statedir']) -ick2.log.log('info', msg_text='called ControllerAPI.set_state_directory') + api = ick2.ControllerAPI() + ick2.log.log('info', msg_text='created ControllerAPI') -api.load_projects() -ick2.log.log('info', msg_text='called ControllerAPI.load_projects') + api.set_state_directory(config['statedir']) + ick2.log.log( + 'info', msg_text='called ControllerAPI.set_state_directory') -app = apifw.create_bottle_application(api, counter, dict_logger, config) -ick2.log.log('info', msg_text='called apifw.create_bottle_application') + api.load_projects() + ick2.log.log('info', msg_text='called ControllerAPI.load_projects') + + application = apifw.create_bottle_application( + api, counter, dict_logger, config) + ick2.log.log('info', msg_text='called apifw.create_bottle_application') + + return application + except SystemExit: + pass + except BaseException as e: + logging.error(str(e)) + sys.exit(1) + + +app = main() # If we are running this program directly with Python, and not via # gunicorn, we can use the Bottle built-in debug server, which can -- cgit v1.2.1