Improved logging mechanism (and a few minor goodies) (#54)
* Improved logging mechanism * Allow for suppression of any errors regarding failed device updates
This commit is contained in:
+30
-19
@@ -1,31 +1,42 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import logging
|
||||
import sys
|
||||
|
||||
if sys.version_info < (3, 5):
|
||||
print("To use this script you need python 3.5 or newer! got %s" % sys.version_info)
|
||||
sys.exit(1)
|
||||
|
||||
from logger import _LOGGER
|
||||
import logger
|
||||
logger.setup()
|
||||
|
||||
import logging
|
||||
import argparse
|
||||
import queue
|
||||
|
||||
from workers_queue import _WORKERS_QUEUE
|
||||
from config import settings
|
||||
from mqtt import MqttClient
|
||||
from workers_manager import WorkersManager
|
||||
|
||||
|
||||
parser = argparse.ArgumentParser()
|
||||
group = parser.add_mutually_exclusive_group()
|
||||
group.add_argument('-d', '--debug', action='store_true', default=False)
|
||||
group.add_argument('-q', '--quiet', action='store_true', default=False)
|
||||
group.add_argument('-d', '--debug', action='store_true', default=False, help='Set logging to output debug information')
|
||||
group.add_argument('-q', '--quiet', action='store_true', default=False, help='Set logging to just output warnings and errors')
|
||||
parser.add_argument('-s', '--suppress-update-failures', dest='suppress', action='store_true', default=False, help='Suppress any errors regarding failed device updates')
|
||||
parsed = parser.parse_args()
|
||||
|
||||
if parsed.debug:
|
||||
_LOGGER = logger.get()
|
||||
if parsed.quiet:
|
||||
_LOGGER.setLevel(logging.WARNING)
|
||||
elif parsed.debug:
|
||||
_LOGGER.setLevel(logging.DEBUG)
|
||||
logger.enable_debug_formatter()
|
||||
else:
|
||||
_LOGGER.setLevel(logging.INFO)
|
||||
logger.suppress_update_failures(parsed.suppress)
|
||||
|
||||
_LOGGER.debug('Starting')
|
||||
_LOGGER.info('Starting')
|
||||
|
||||
mqtt = MqttClient(settings['mqtt'])
|
||||
manager = WorkersManager()
|
||||
@@ -33,16 +44,16 @@ manager.register_workers(settings['manager']).start(mqtt)
|
||||
|
||||
running = True
|
||||
|
||||
try:
|
||||
while running:
|
||||
try:
|
||||
mqtt.publish(_WORKERS_QUEUE.get(block=True).execute())
|
||||
except (KeyboardInterrupt, SystemExit):
|
||||
raise
|
||||
except Exception as e:
|
||||
if not parsed.quiet:
|
||||
_LOGGER.exception(e)
|
||||
except KeyboardInterrupt:
|
||||
running = False
|
||||
_LOGGER.info('Exiting allowing jobs to finish. If you need force exit use kill')
|
||||
|
||||
while running:
|
||||
try:
|
||||
mqtt.publish(_WORKERS_QUEUE.get(timeout=10).execute())
|
||||
except queue.Empty: # Allow for SIGINT processing
|
||||
pass
|
||||
except TimeoutError:
|
||||
logger.log_exception(_LOGGER, "Timeout while executing worker command", suppress=True)
|
||||
except (KeyboardInterrupt, SystemExit):
|
||||
running = False
|
||||
_LOGGER.info('Finish current jobs and shut down. If you need force exit use kill')
|
||||
except Exception as e:
|
||||
logger.log_exception(_LOGGER, "Fatal error while executing worker command: %s", type(e).__name__)
|
||||
raise e
|
||||
|
||||
Reference in New Issue
Block a user