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
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)
# 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:
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
data_files=data_files,
scripts=['launch'],
entry_points={
- 'console_scripts': ['sima = launch:run',]
+ 'console_scripts': ['sima = launch:mainc',]
},
)
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
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
"""
import os
import sys
import time
-from signal import signal, SIGTERM
+from signal import signal, SIGTERM, SIGHUP, SIGUSR1
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:
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)
log.info('Quiting now!')
sys.exit(1)
+class SigHup(Exception):
+ pass
+
# ArgParse Callbacks
class Obsolete(Action):
# pylint: disable=R0903