diff --git a/CouchPotato.py b/CouchPotato.py index 478daf4..45f44cf 100755 --- a/CouchPotato.py +++ b/CouchPotato.py @@ -4,6 +4,7 @@ from signal import signal, SIGTERM import os import subprocess import sys +import time # Root path @@ -53,6 +54,10 @@ def main(): if not restart: break + from couchpotato.core.event import fireEvent + fireEvent('app.crappy_shutdown', single = True) + time.sleep(1) + sys.exit() if __name__ == '__main__': diff --git a/couchpotato/core/_base/_core/main.py b/couchpotato/core/_base/_core/main.py index d9c5e54..2bd7e6d 100644 --- a/couchpotato/core/_base/_core/main.py +++ b/couchpotato/core/_base/_core/main.py @@ -60,8 +60,6 @@ class Core(Plugin): }) def crappyShutdown(self): - if self.shutdown_started: return - try: self.urlopen('%sapp.shutdown' % self.createApiUrl(), show_error = False) return True @@ -70,8 +68,6 @@ class Core(Plugin): return False def crappyRestart(self): - if self.shutdown_started: return - try: self.urlopen('%sapp.restart' % self.createApiUrl(), show_error = False) return True @@ -88,26 +84,30 @@ class Core(Plugin): return 'restarting' def initShutdown(self, restart = False): + if self.shutdown_started: + log.info('Already shutting down') + log.info('Shutting down' if not restart else 'Restarting') + self.shutdown_started = True + fireEvent('app.shutdown') log.debug('Every plugin got shutdown event') loop = True while loop: log.debug('Asking who is running') - still_running = fireEvent('plugin.running') + still_running = fireEvent('plugin.running', merge = True) log.debug('Still running: %s' % still_running) if len(still_running) == 0: break - for running in still_running: - running = list(set(running) - set(self.ignore_restart)) - if len(running) > 0: - log.info('Waiting on plugins to finish: %s' % running) - else: - loop = False + running = list(set(still_running) - set(self.ignore_restart)) + if len(running) > 0: + log.info('Waiting on plugins to finish: %s' % running) + else: + loop = False time.sleep(1) diff --git a/couchpotato/core/plugins/base.py b/couchpotato/core/plugins/base.py index bf4a5f1..50948e8 100644 --- a/couchpotato/core/plugins/base.py +++ b/couchpotato/core/plugins/base.py @@ -25,8 +25,7 @@ class Plugin(object): enabled_option = 'enabled' auto_register_static = True - needs_shutdown = False - running = [] + _needs_shutdown = False http_last_use = {} http_time_between_calls = 0 @@ -143,7 +142,6 @@ class Plugin(object): time.sleep(last_use - now + self.http_time_between_calls) def beforeCall(self, handler): - #log.debug('Calling %s.%s' % (self.getName(), handler.__name__)) self.isRunning('%s.%s' % (self.getName(), handler.__name__)) def afterCall(self, handler): @@ -154,19 +152,23 @@ class Plugin(object): def shuttingDown(self, value = None): if value is None: - return self.needs_shutdown + return self._needs_shutdown - self.needs_shutdown = value + self._needs_shutdown = value def isRunning(self, value = None, boolean = True): + + if not hasattr(self, '_running'): + self._running = [] + if value is None: - return self.running + return self._running if boolean: - self.running.append(value) + self._running.append(value) else: try: - self.running.remove(value) + self._running.remove(value) except: log.error("Something went wrong when finishing the plugin function. Could not find the 'is_running' key")