From 0236b2bf31b893ab0b53374c0d3a0bc0ab9deda3 Mon Sep 17 00:00:00 2001 From: kaliko Date: Mon, 23 Dec 2013 18:07:49 +0100 Subject: [PATCH] Catches SIGHUP/SIGUSR1 to trigger conf reload --- launch | 40 ++++++++++++++++++++++++++-------------- setup.py | 2 +- sima/core.py | 7 +++++++ sima/lib/daemon.py | 8 +++++++- sima/lib/logger.py | 18 ++++++++++-------- sima/utils/utils.py | 3 +++ 6 files changed, 54 insertions(+), 24 deletions(-) diff --git a/launch b/launch index 39fac64..9d12fda 100755 --- a/launch +++ b/launch @@ -20,7 +20,7 @@ from sima.lib.logger import set_logger from sima.lib.simadb import SimaDB from sima.utils.config import ConfMan from sima.utils.startopt import StartOpt -from sima.utils.utils import exception_log +from sima.utils.utils import exception_log, SigHup # core plugins from sima.plugins.core.history import History from sima.plugins.core.mpdoptions import MpdOptions @@ -52,13 +52,9 @@ def load_plugins(sima, source): sima.register_plugin(plugin_obj) -def main(): - """Entry point, deal w/ CLI and starts application +def start(sopt, restart=False): + """starts application """ - info = dict({'version': core.__version__, - 'prog': 'sima'}) - # StartOpt gathers options from command line call (in StartOpt().options) - sopt = StartOpt(info) # set logger verbosity = sopt.options.get('verbosity', 'warning') logfile = sopt.options.get('logfile', None) @@ -95,11 +91,15 @@ def main(): # Loading contrib plugins load_plugins(sima, 'contrib') - # Run as a daemon if config.getboolean('daemon', 'daemon'): - sima.start() + if restart: + sima.run() + else: + logger.info('Daemonize process...') + sima.start() + logger.error('starting') try: sima.foreground() except KeyboardInterrupt: @@ -107,18 +107,30 @@ def main(): sys.exit(0) -def run(): - """Catching broad exception +def run(sopt, restart=False): + """ + Handles SigHup exception + Catches Unhandled exception """ # pylint: disable=broad-except try: - main() - except Exception: + start(sopt, restart) + except SigHup as err: # SigHup inherit from Exception + run(sopt, True) + except Exception: # Unhandled exception exception_log() # Script starts here +def main(): + info = dict({'version': core.__version__, + 'prog': 'sima'}) + # StartOpt gathers options from command line call (in StartOpt().options) + sopt = StartOpt(info) + run(sopt) + + if __name__ == '__main__': - run() + main() # VIM MODLINE # vim: ai ts=4 sw=4 sts=4 expandtab diff --git a/setup.py b/setup.py index da4773f..2768347 100755 --- a/setup.py +++ b/setup.py @@ -59,7 +59,7 @@ setup(name='sima', data_files=data_files, scripts=['launch'], entry_points={ - 'console_scripts': ['sima = launch:run',] + 'console_scripts': ['sima = launch:mainc',] }, ) diff --git a/sima/core.py b/sima/core.py index 694928f..bf098bb 100644 --- a/sima/core.py +++ b/sima/core.py @@ -16,6 +16,7 @@ from .client import PlayerClient from .client import PlayerError, PlayerUnHandledError from .lib.simadb import SimaDB from .lib.daemon import Daemon +from .utils.utils import SigHup class Sima(Daemon): """Main class, plugin and player management @@ -105,6 +106,12 @@ class Sima(Daemon): self.log.info('Got reconnected') break + def hup_handler(self, signum, frame): + self.log.warning('Caught a sighup!') + self.player.disconnect() + self.foreach_plugin('shutdown') + raise SigHup('SIGHUP caught!') + def shutdown(self): """General shutdown method """ diff --git a/sima/lib/daemon.py b/sima/lib/daemon.py index b0b1130..06f7bee 100644 --- a/sima/lib/daemon.py +++ b/sima/lib/daemon.py @@ -27,7 +27,7 @@ import atexit import os import sys import time -from signal import signal, SIGTERM +from signal import signal, SIGTERM, SIGHUP, SIGUSR1 class Daemon(object): @@ -124,10 +124,16 @@ class Daemon(object): """Declare signal handlers """ signal(SIGTERM, self.exit_handler) + signal(SIGHUP, self.hup_handler) + signal(SIGUSR1, self.hup_handler) def exit_handler(self, signum, frame): sys.exit(1) + def hup_handler(self, signum, frame): + """SIGHUP handler""" + pass + def delpid(self): """Remove PID file""" try: diff --git a/sima/lib/logger.py b/sima/lib/logger.py index ebe6c27..bde62d7 100644 --- a/sima/lib/logger.py +++ b/sima/lib/logger.py @@ -71,15 +71,17 @@ def set_logger(level='info', logfile=None, name='sima'): fileh = logging.FileHandler(logfile) #fileh.setLevel(user_log_level) fileh.setFormatter(formatter) - logg.addHandler(fileh) + if not logg.hasHandlers(): + logg.addHandler(fileh) else: - # create console handler with a specified log level (STDOUT) - couth = logging.StreamHandler(sys.stdout) - #couth.setLevel(user_log_level) - couth.addFilter(LevelFilter(logging.WARNING)) - - # create console handler with warning log level (STDERR) - cerrh = logging.StreamHandler(sys.stderr) + if not logg.hasHandlers(): + # create console handler with a specified log level (STDOUT) + couth = logging.StreamHandler(sys.stdout) + #couth.setLevel(user_log_level) + couth.addFilter(LevelFilter(logging.WARNING)) + + # create console handler with warning log level (STDERR) + cerrh = logging.StreamHandler(sys.stderr) #cerrh.setLevel(logging.WARNING) cerrh.setLevel(logging.ERROR) diff --git a/sima/utils/utils.py b/sima/utils/utils.py index e3927fc..07c317f 100644 --- a/sima/utils/utils.py +++ b/sima/utils/utils.py @@ -63,6 +63,9 @@ def exception_log(): log.info('Quiting now!') sys.exit(1) +class SigHup(Exception): + pass + # ArgParse Callbacks class Obsolete(Action): # pylint: disable=R0903 -- 2.39.5