Browse Source

Proper restarting of server

pull/381/merge
Ruud 13 years ago
parent
commit
a5c8747fee
  1. 25
      couchpotato/core/_base/_core/main.py
  2. 1
      couchpotato/environment.py
  3. 12
      couchpotato/runner.py

25
couchpotato/core/_base/_core/main.py

@ -5,7 +5,7 @@ from couchpotato.core.helpers.variable import cleanHost, md5
from couchpotato.core.logger import CPLog from couchpotato.core.logger import CPLog
from couchpotato.core.plugins.base import Plugin from couchpotato.core.plugins.base import Plugin
from couchpotato.environment import Env from couchpotato.environment import Env
from flask import request from tornado.ioloop import IOLoop
from uuid import uuid4 from uuid import uuid4
import os import os
import platform import platform
@ -18,7 +18,7 @@ log = CPLog(__name__)
class Core(Plugin): class Core(Plugin):
ignore_restart = ['Core.crappyRestart', 'Core.crappyShutdown'] ignore_restart = ['Core.crappyRestart', 'Core.crappyShutdown', 'Updater.check']
shutdown_started = False shutdown_started = False
def __init__(self): def __init__(self):
@ -63,30 +63,28 @@ class Core(Plugin):
if self.shutdown_started: if self.shutdown_started:
return return
try:
self.urlopen('%s/app.shutdown' % self.createApiUrl(), show_error = False)
return True
except:
self.initShutdown() self.initShutdown()
return False return True
def crappyRestart(self): def crappyRestart(self):
if self.shutdown_started: if self.shutdown_started:
return return
try:
self.urlopen('%s/app.restart' % self.createApiUrl(), show_error = False)
return True
except:
self.initShutdown(restart = True) self.initShutdown(restart = True)
return False return True
def shutdown(self): def shutdown(self):
def shutdown():
self.initShutdown() self.initShutdown()
IOLoop.instance().add_callback(shutdown)
return 'shutdown' return 'shutdown'
def restart(self): def restart(self):
def restart():
self.initShutdown(restart = True) self.initShutdown(restart = True)
IOLoop.instance().add_callback(restart)
return 'restarting' return 'restarting'
def initShutdown(self, restart = False): def initShutdown(self, restart = False):
@ -121,7 +119,8 @@ class Core(Plugin):
log.debug('Save to shutdown/restart') log.debug('Save to shutdown/restart')
try: try:
request.environ.get('werkzeug.server.shutdown')() Env.get('httpserver').stop()
IOLoop.instance().stop()
except RuntimeError: except RuntimeError:
pass pass
except: except:

1
couchpotato/environment.py

@ -23,6 +23,7 @@ class Env(object):
_deamonize = False _deamonize = False
_desktop = None _desktop = None
_session = None _session = None
_httpserver = None
''' Data paths and directories ''' ''' Data paths and directories '''
_app_dir = "" _app_dir = ""

12
couchpotato/runner.py

@ -233,16 +233,18 @@ def runCouchPotato(options, base_path, args, data_dir = None, log_dir = None, En
fireEventAsync('app.load') fireEventAsync('app.load')
# Go go go! # Go go go!
web_container = WSGIContainer(app)
web_container._log = _log
http_server = HTTPServer(web_container, no_keep_alive = True)
Env.set('httpserver', http_server)
loop = IOLoop.instance()
try_restart = True try_restart = True
restart_tries = 5 restart_tries = 5
while try_restart: while try_restart:
try: try:
web_container = WSGIContainer(app)
web_container._log = _log
http_server = HTTPServer(web_container)
http_server.listen(config['port'], config['host']) http_server.listen(config['port'], config['host'])
loop = IOLoop.instance()
if config['use_reloader']: if config['use_reloader']:
autoreload.start(loop) autoreload.start(loop)

Loading…
Cancel
Save