diff --git a/cps.py b/cps.py index 5bc1e854..055c0ffe 100755 --- a/cps.py +++ b/cps.py @@ -10,44 +10,12 @@ sys.path.append(base_path) sys.path.append(os.path.join(base_path, 'cps')) sys.path.append(os.path.join(base_path, 'vendor')) -from cps import web -try: - from gevent.pywsgi import WSGIServer - gevent_present = True -except ImportError: - from tornado.wsgi import WSGIContainer - from tornado.httpserver import HTTPServer - from tornado.ioloop import IOLoop - gevent_present = False +from cps.server import Server if __name__ == '__main__': - if gevent_present: - web.app.logger.info('Attempting to start gevent') - web.start_gevent() - else: - web.app.logger.info('Starting Tornado webserver') - # Max Buffersize set to 200MB - if web.ub.config.get_config_certfile() and web.ub.config.get_config_keyfile(): - ssl={"certfile": web.ub.config.get_config_certfile(), - "keyfile": web.ub.config.get_config_keyfile()} - else: - ssl=None - http_server = HTTPServer(WSGIContainer(web.app), - max_buffer_size = 209700000, - ssl_options=ssl) - http_server.listen(web.ub.config.config_port) - IOLoop.instance().start() - IOLoop.instance().close(True) + Server.startServer() + + + + - if web.helper.global_task == 0: - web.app.logger.info("Performing restart of Calibre-web") - if os.name == 'nt': - arguments = ["\"" + sys.executable + "\""] - for e in sys.argv: - arguments.append("\"" + e + "\"") - os.execv(sys.executable, arguments) - else: - os.execl(sys.executable, sys.executable, *sys.argv) - else: - web.app.logger.info("Performing shutdown of Calibre-web") - sys.exit(0) diff --git a/cps/helper.py b/cps/helper.py index 3cc93262..cdc836d5 100755 --- a/cps/helper.py +++ b/cps/helper.py @@ -36,12 +36,12 @@ import threading import shutil import requests import zipfile -from tornado.ioloop import IOLoop try: import gdriveutils as gd except ImportError: pass import web +import server try: import unidecode @@ -50,7 +50,6 @@ except ImportError: use_unidecode = False # Global variables -global_task = None updater_thread = None RET_SUCCESS = 1 @@ -388,7 +387,6 @@ class Updater(threading.Thread): self.status = 0 def run(self): - global global_task self.status = 1 r = requests.get('https://api.github.com/repos/janeczku/calibre-web/zipball/master', stream=True) fname = re.findall("filename=(.+)", r.headers['content-disposition'])[0] @@ -400,19 +398,13 @@ class Updater(threading.Thread): self.status = 4 self.update_source(os.path.join(tmp_dir, os.path.splitext(fname)[0]), ub.config.get_main_dir) self.status = 5 - global_task = 0 db.session.close() db.engine.dispose() ub.session.close() ub.engine.dispose() self.status = 6 - - if web.gevent_server: - web.gevent_server.stop() - else: - # stop tornado server - server = IOLoop.instance() - server.add_callback(server.stop) + server.Server.setRestartTyp(True) + server.Server.stopServer() self.status = 7 def get_update_status(self): diff --git a/cps/server.py b/cps/server.py new file mode 100644 index 00000000..b84c71dc --- /dev/null +++ b/cps/server.py @@ -0,0 +1,103 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + + +from socket import error as SocketError +import sys +import os +try: + from gevent.pywsgi import WSGIServer + from gevent import monkey + from gevent.pool import Pool + from gevent import __version__ as geventVersion + gevent_present = True +except ImportError: + from tornado.wsgi import WSGIContainer + from tornado.httpserver import HTTPServer + from tornado.ioloop import IOLoop + from tornado import version as tornadoVersion + gevent_present = False + +import web + + +class server: + + wsgiserver = None + restart= False + + def __init__(self): + pass + + def start_gevent(self): + try: + ssl_args = dict() + if web.ub.config.get_config_certfile() and web.ub.config.get_config_keyfile(): + ssl_args = {"certfile": web.ub.config.get_config_certfile(), + "keyfile": web.ub.config.get_config_keyfile()} + if os.name == 'nt': + self.wsgiserver= WSGIServer(('0.0.0.0', web.ub.config.config_port), web.app, spawn=Pool(), **ssl_args) + else: + self.wsgiserver = WSGIServer(('', web.ub.config.config_port), web.app, spawn=Pool(), **ssl_args) + self.wsgiserver.serve_forever() + + except SocketError: + web.app.logger.info('Unable to listen on \'\', trying on IPv4 only...') + self.wsgiserver = WSGIServer(('0.0.0.0', web.ub.config.config_port), web.app, spawn=Pool(), **ssl_args) + self.wsgiserver.serve_forever() + except: + pass + + def startServer(self): + if gevent_present: + web.app.logger.info('Starting Gevent server') + # leave subprocess out to allow forking for fetchers and processors + monkey.patch_all(subprocess=False) + self.start_gevent() + else: + web.app.logger.info('Starting Tornado server') + if web.ub.config.get_config_certfile() and web.ub.config.get_config_keyfile(): + ssl={"certfile": web.ub.config.get_config_certfile(), + "keyfile": web.ub.config.get_config_keyfile()} + else: + ssl=None + # Max Buffersize set to 200MB + http_server = HTTPServer(WSGIContainer(web.app), + max_buffer_size = 209700000, + ssl_options=ssl) + http_server.listen(web.ub.config.config_port) + self.wsgiserver=IOLoop.instance() + self.wsgiserver.start() # wait for stop signal + self.wsgiserver.close(True) + + if self.restart == True: + web.app.logger.info("Performing restart of Calibre-web") + if os.name == 'nt': + arguments = ["\"" + sys.executable + "\""] + for e in sys.argv: + arguments.append("\"" + e + "\"") + os.execv(sys.executable, arguments) + else: + os.execl(sys.executable, sys.executable, *sys.argv) + else: + web.app.logger.info("Performing shutdown of Calibre-web") + sys.exit(0) + + def setRestartTyp(self,starttyp): + self.restart=starttyp + + def stopServer(self): + if gevent_present: + self.wsgiserver.close() + else: + self.wsgiserver.add_callback(self.wsgiserver.stop) + + def getNameVersion(self): + if gevent_present: + return {'gevent':geventVersion} + else: + return {'tornado':tornadoVersion} + + +# Start Instance of Server +Server=server() diff --git a/cps/templates/stats.html b/cps/templates/stats.html index 7be8f613..4db0f3f2 100644 --- a/cps/templates/stats.html +++ b/cps/templates/stats.html @@ -38,6 +38,18 @@