diff --git a/Headphones.py b/Headphones.py index ccef9ec1..4487df68 100644 --- a/Headphones.py +++ b/Headphones.py @@ -1,5 +1,6 @@ #!/usr/bin/env python import os, sys +import time from lib.configobj import ConfigObj @@ -99,6 +100,20 @@ def main(): # Start the background threads headphones.start() + while True: + if not headphones.SIGNAL: + time.sleep(1) + else: + logger.info('Received signal: ' + headphones.SIGNAL) + if headphones.SIGNAL == 'shutdown': + headphones.shutdown() + elif headphones.SIGNAL == 'restart': + headphones.shutdown(restart=True) + else: + headphones.shutdown(restart=True, update=True) + + headphones.SIGNAL = None + return if __name__ == "__main__": diff --git a/headphones/__init__.py b/headphones/__init__.py index 7f4d7e6b..7b1fc2fb 100644 --- a/headphones/__init__.py +++ b/headphones/__init__.py @@ -18,7 +18,7 @@ FULL_PATH = None PROG_DIR = None ARGS = None -INVOKED_COMMAND = None +SIGNAL = None QUIET = False DAEMON = False @@ -496,20 +496,23 @@ def dbcheck(): def shutdown(restart=False, update=False): - + cherrypy.engine.exit() SCHED.shutdown(wait=False) config_write() + if not restart and not update: + logger.info('Headphones is shutting down...') if update: + logger.info('Headphones is updating...') try: versioncheck.update() except Exception, e: logger.warn('Headphones failed to update: %s. Restarting.' % e) if restart: - + logger.info('Headphones is restarting...') popen_list = [sys.executable, FULL_PATH] popen_list += ARGS if '--nolaunch' not in popen_list: diff --git a/headphones/webserve.py b/headphones/webserve.py index c03c2790..399d22ee 100644 --- a/headphones/webserve.py +++ b/headphones/webserve.py @@ -397,8 +397,7 @@ class WebInterface(object): configUpdate.exposed = True def shutdown(self): - logger.info(u"Headphones is shutting down...") - threading.Timer(2, headphones.shutdown).start() + headphones.SIGNAL = 'shutdown' message = 'Shutting Down...' return serve_template(templatename="shutdown.html", title="Shutting Down", message=message, timer=15) return page @@ -406,19 +405,16 @@ class WebInterface(object): shutdown.exposed = True def restart(self): - logger.info(u"Headphones is restarting...") - threading.Timer(2, headphones.shutdown, [True]).start() + headphones.SIGNAL = 'restart' message = 'Restarting...' return serve_template(templatename="shutdown.html", title="Restarting", message=message, timer=30) restart.exposed = True def update(self): - logger.info('Headphones is updating...') - threading.Timer(2, headphones.shutdown, [True, True]).start() + headphones.SIGNAL = 'update' message = 'Updating...' return serve_template(templatename="shutdown.html", title="Updating", message=message, timer=120) return page - update.exposed = True def extras(self):