]> kaliko git repositories - mpd-sima.git/commitdiff
Catches SIGHUP/SIGUSR1 to trigger conf reload
authorkaliko <efrim@azylum.org>
Mon, 23 Dec 2013 17:07:49 +0000 (18:07 +0100)
committerkaliko <efrim@azylum.org>
Mon, 23 Dec 2013 17:07:49 +0000 (18:07 +0100)
launch
setup.py
sima/core.py
sima/lib/daemon.py
sima/lib/logger.py
sima/utils/utils.py

diff --git a/launch b/launch
index 39fac649ba25ebdba7e8fc3e5b1a311ffd39a941..9d12fda1ebdc9f014ca34fbf2ae1bcb0c4345828 100755 (executable)
--- 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
index da4773f8e44deee9d831a64656f5512b5f801e24..2768347e7feb5cdd9a7dbb2b00931ffdbb5e20b4 100755 (executable)
--- 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',]
           },
 )
 
index 694928fa77815ddd017aab71c4b2c9aadd2a4b78..bf098bbb58ececa7d2442e0927d1009a7ef08e8b 100644 (file)
@@ -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
         """
index b0b1130172450ac21c0196a4ec05a801f58d4d7e..06f7beeae9a36e33459f3207a25f6fb75715f8af 100644 (file)
@@ -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:
index ebe6c27142946932dfa23fd4826634eb61e37c8b..bde62d7f83b01cd1ca07e2a70112284808712191 100644 (file)
@@ -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)
 
index e3927fcd7cbff823dbda081cb2b5f561b56f25e2..07c317fcba587e584ba1afdf5a12f3a6e2f20628 100644 (file)
@@ -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