From 088dc5386c9351a044b5150c6bff1674bd7ffb14 Mon Sep 17 00:00:00 2001 From: Ruud Date: Sat, 10 Mar 2012 17:07:19 +0100 Subject: [PATCH] Better shutdown of process --- CouchPotato.py | 3 ++- couchpotato/core/_base/_core/main.py | 7 +++++++ couchpotato/runner.py | 24 +++++++++++++++++++++++- 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/CouchPotato.py b/CouchPotato.py index 9a0d9fb..3bc7916 100755 --- a/CouchPotato.py +++ b/CouchPotato.py @@ -85,7 +85,8 @@ class Loader(object): # remove old pidfile first try: if self.runAsDaemon(): - self.daemon.stop() + try: self.daemon.stop() + except: pass self.daemon.delpid() except: self.log.critical(traceback.format_exc()) diff --git a/couchpotato/core/_base/_core/main.py b/couchpotato/core/_base/_core/main.py index d334553..b22f9ba 100644 --- a/couchpotato/core/_base/_core/main.py +++ b/couchpotato/core/_base/_core/main.py @@ -56,6 +56,9 @@ class Core(Plugin): }) def crappyShutdown(self): + if self.shutdown_started: + return + try: self.urlopen('%s/app.shutdown' % self.createApiUrl(), show_error = False) return True @@ -64,6 +67,9 @@ class Core(Plugin): return False def crappyRestart(self): + if self.shutdown_started: + return + try: self.urlopen('%s/app.restart' % self.createApiUrl(), show_error = False) return True @@ -82,6 +88,7 @@ class Core(Plugin): def initShutdown(self, restart = False): if self.shutdown_started: log.info('Already shutting down') + return log.info('Shutting down' if not restart else 'Restarting') diff --git a/couchpotato/runner.py b/couchpotato/runner.py index 11236c1..217f52b 100644 --- a/couchpotato/runner.py +++ b/couchpotato/runner.py @@ -186,4 +186,26 @@ def runCouchPotato(options, base_path, args, data_dir = None, log_dir = None, En if fire_load: fireEventAsync('app.load') # Go go go! - app.run(**config) + try_restart = True + restart_tries = 5 + while try_restart: + try: + app.run(**config) + except Exception, e: + try: + nr, msg = e + if nr == 48: + log.info('Already in use, try %s more time after few seconds' % restart_tries) + time.sleep(1) + restart_tries -= 1 + + if restart_tries > 0: + continue + else: + return + except: + pass + + raise + + try_restart = False