2018-07-09 18:47:36 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
from socket import error as SocketError
|
|
|
|
import sys
|
|
|
|
import os
|
2018-09-23 19:15:50 +02:00
|
|
|
import signal
|
2018-09-30 09:43:20 +02:00
|
|
|
import web
|
2018-09-23 19:15:50 +02:00
|
|
|
|
2018-07-09 18:47:36 +02:00
|
|
|
try:
|
|
|
|
from gevent.pywsgi import WSGIServer
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
class server:
|
|
|
|
|
|
|
|
wsgiserver = None
|
|
|
|
restart= False
|
|
|
|
|
|
|
|
def __init__(self):
|
2018-09-23 19:15:50 +02:00
|
|
|
signal.signal(signal.SIGINT, self.killServer)
|
2018-09-30 09:43:20 +02:00
|
|
|
signal.signal(signal.SIGTERM, self.killServer)
|
2018-07-09 18:47:36 +02:00
|
|
|
|
|
|
|
def start_gevent(self):
|
|
|
|
try:
|
|
|
|
ssl_args = dict()
|
2018-11-17 00:32:21 +01:00
|
|
|
certfile_path = web.ub.config.get_config_certfile()
|
|
|
|
keyfile_path = web.ub.config.get_config_keyfile()
|
|
|
|
if certfile_path and keyfile_path:
|
|
|
|
if os.path.isfile(certfile_path) and os.path.isfile(keyfile_path):
|
|
|
|
ssl_args = {"certfile": certfile_path,
|
|
|
|
"keyfile": keyfile_path}
|
|
|
|
else:
|
|
|
|
web.app.logger.info('The specified paths for the ssl certificate file and/or key file seem to be broken. Ignoring ssl. Cert path: %s | Key path: %s' % (certfile_path, keyfile_path))
|
2018-07-09 18:47:36 +02:00
|
|
|
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:
|
2018-10-30 19:29:33 +01:00
|
|
|
try:
|
|
|
|
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 (OSError, SocketError) as e:
|
|
|
|
web.app.logger.info("Error starting server: %s" % e.strerror)
|
|
|
|
print("Error starting server: %s" % e.strerror)
|
|
|
|
web.helper.global_WorkerThread.stop()
|
|
|
|
sys.exit(1)
|
2018-08-12 09:29:57 +02:00
|
|
|
except Exception:
|
|
|
|
web.app.logger.info("Unknown error while starting gevent")
|
2018-07-09 18:47:36 +02:00
|
|
|
|
|
|
|
def startServer(self):
|
|
|
|
if gevent_present:
|
|
|
|
web.app.logger.info('Starting Gevent server')
|
|
|
|
# leave subprocess out to allow forking for fetchers and processors
|
|
|
|
self.start_gevent()
|
|
|
|
else:
|
2018-10-30 19:29:33 +01:00
|
|
|
try:
|
2018-11-17 16:58:05 +01:00
|
|
|
ssl = None
|
2018-10-30 19:29:33 +01:00
|
|
|
web.app.logger.info('Starting Tornado server')
|
2018-11-17 00:32:21 +01:00
|
|
|
certfile_path = web.ub.config.get_config_certfile()
|
|
|
|
keyfile_path = web.ub.config.get_config_keyfile()
|
|
|
|
if certfile_path and keyfile_path:
|
|
|
|
if os.path.isfile(certfile_path) and os.path.isfile(keyfile_path):
|
2018-11-17 01:28:34 +01:00
|
|
|
ssl = {"certfile": certfile_path,
|
|
|
|
"keyfile": keyfile_path}
|
2018-11-17 00:32:21 +01:00
|
|
|
else:
|
|
|
|
web.app.logger.info('The specified paths for the ssl certificate file and/or key file seem to be broken. Ignoring ssl. Cert path: %s | Key path: %s' % (certfile_path, keyfile_path))
|
2018-11-17 16:58:05 +01:00
|
|
|
|
2018-10-30 19:29:33 +01:00
|
|
|
# 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)
|
|
|
|
except SocketError as e:
|
|
|
|
web.app.logger.info("Error starting server: %s" % e.strerror)
|
|
|
|
print("Error starting server: %s" % e.strerror)
|
|
|
|
web.helper.global_WorkerThread.stop()
|
|
|
|
sys.exit(1)
|
2018-07-09 18:47:36 +02:00
|
|
|
|
|
|
|
if self.restart == True:
|
2018-08-24 15:48:09 +02:00
|
|
|
web.app.logger.info("Performing restart of Calibre-Web")
|
2018-08-12 09:29:57 +02:00
|
|
|
web.helper.global_WorkerThread.stop()
|
2018-07-09 18:47:36 +02:00
|
|
|
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:
|
2018-08-24 15:48:09 +02:00
|
|
|
web.app.logger.info("Performing shutdown of Calibre-Web")
|
2018-08-12 09:29:57 +02:00
|
|
|
web.helper.global_WorkerThread.stop()
|
2018-07-09 18:47:36 +02:00
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
def setRestartTyp(self,starttyp):
|
|
|
|
self.restart=starttyp
|
|
|
|
|
2018-09-23 19:15:50 +02:00
|
|
|
def killServer(self, signum, frame):
|
|
|
|
self.stopServer()
|
|
|
|
|
2018-07-09 18:47:36 +02:00
|
|
|
def stopServer(self):
|
2018-11-24 20:47:11 +01:00
|
|
|
if self.wsgiserver:
|
|
|
|
if gevent_present:
|
|
|
|
self.wsgiserver.close()
|
|
|
|
else:
|
|
|
|
self.wsgiserver.add_callback(self.wsgiserver.stop)
|
2018-07-09 18:47:36 +02:00
|
|
|
|
2018-08-12 09:29:57 +02:00
|
|
|
@staticmethod
|
|
|
|
def getNameVersion():
|
2018-07-09 18:47:36 +02:00
|
|
|
if gevent_present:
|
2018-07-18 20:21:44 +02:00
|
|
|
return {'Gevent':'v'+geventVersion}
|
2018-07-09 18:47:36 +02:00
|
|
|
else:
|
2018-07-18 20:21:44 +02:00
|
|
|
return {'Tornado':'v'+tornadoVersion}
|
2018-07-09 18:47:36 +02:00
|
|
|
|
|
|
|
|
|
|
|
# Start Instance of Server
|
|
|
|
Server=server()
|