pull/997/head
Louis Vézina 5 years ago
parent 5a93456dcd
commit 1cb0799d23

@ -64,15 +64,15 @@ def authenticate(actual_method):
class Shutdown(Resource): class Shutdown(Resource):
@authenticate @authenticate
def get(self): def get(self):
from main import Server from main import webserver
Server.shutdown() webserver.shutdown()
class Restart(Resource): class Restart(Resource):
@authenticate @authenticate
def get(self): def get(self):
from main import Server from main import webserver
Server.restart() webserver.restart()
class Badges(Resource): class Badges(Resource):

@ -464,79 +464,84 @@ def test_notification(protocol, provider):
return '', 200 return '', 200
class Server: class Server(object):
# Mute DeprecationWarning class __Server:
warnings.simplefilter("ignore", DeprecationWarning) def __init__(self):
# Mute Insecure HTTPS requests made to Sonarr and Radarr # Mute DeprecationWarning
warnings.filterwarnings('ignore', message='Unverified HTTPS request') warnings.simplefilter("ignore", DeprecationWarning)
# Mute Python3 BrokenPipeError # Mute Insecure HTTPS requests made to Sonarr and Radarr
warnings.simplefilter("ignore", BrokenPipeError) warnings.filterwarnings('ignore', message='Unverified HTTPS request')
# Mute Python3 BrokenPipeError
if args.dev: warnings.simplefilter("ignore", BrokenPipeError)
server = app.run(host=str(settings.general.ip),
port=(int(args.port) if args.port else int(settings.general.port))) if args.dev:
else: self.server = app.run(host=str(settings.general.ip),
server = create_server(app, port=(int(args.port) if args.port else int(settings.general.port)))
host=str(settings.general.ip), else:
port=int(args.port) if args.port else int(settings.general.port), self.server = create_server(app,
threads=24) host=str(settings.general.ip),
port=int(args.port) if args.port else int(settings.general.port),
# Make sure that server class instance isn't getting instantiated again but reused instead. threads=24)
_instance = None
def start(self):
def __new__(cls, *args, **kwargs): try:
if not cls._instance: logging.info(
cls._instance = super(Server, cls).__new__( 'BAZARR is started and waiting for request on http://' + str(settings.general.ip) + ':' + (str(
cls, *args, **kwargs) args.port) if args.port else str(settings.general.port)) + str(base_url))
return cls._instance if not args.dev:
self.server.run()
@classmethod except KeyboardInterrupt:
def start(cls): self.shutdown()
try:
logging.info( def shutdown(self):
'BAZARR is started and waiting for request on http://' + str(settings.general.ip) + ':' + (str(
args.port) if args.port else str(settings.general.port)) + str(base_url))
if not args.dev:
cls.server.run()
except KeyboardInterrupt:
cls.shutdown()
@classmethod
def shutdown(cls):
try:
cls.server.close()
except Exception as e:
logging.error('BAZARR Cannot stop Waitress: ' + repr(e))
else:
database.close()
try: try:
stop_file = io.open(os.path.join(args.config_dir, "bazarr.stop"), "w", encoding='UTF-8') self.server.close()
except Exception as e: except Exception as e:
logging.error('BAZARR Cannot create bazarr.stop file: ' + repr(e)) logging.error('BAZARR Cannot stop Waitress: ' + repr(e))
else: else:
logging.info('Bazarr is being shutdown...') database.close()
stop_file.write(str('')) try:
stop_file.close() stop_file = io.open(os.path.join(args.config_dir, "bazarr.stop"), "w", encoding='UTF-8')
os._exit(0) except Exception as e:
logging.error('BAZARR Cannot create bazarr.stop file: ' + repr(e))
@classmethod else:
def restart(cls): logging.info('Bazarr is being shutdown...')
try: stop_file.write(str(''))
cls.server.close() stop_file.close()
except Exception as e: os._exit(0)
logging.error('BAZARR Cannot stop Waitress: ' + repr(e))
else: def restart(self):
database.close()
try: try:
restart_file = io.open(os.path.join(args.config_dir, "bazarr.restart"), "w", encoding='UTF-8') self.server.close()
except Exception as e: except Exception as e:
logging.error('BAZARR Cannot create bazarr.restart file: ' + repr(e)) logging.error('BAZARR Cannot stop Waitress: ' + repr(e))
else: else:
logging.info('Bazarr is being restarted...') database.close()
restart_file.write(str('')) try:
restart_file.close() restart_file = io.open(os.path.join(args.config_dir, "bazarr.restart"), "w", encoding='UTF-8')
os._exit(0) except Exception as e:
logging.error('BAZARR Cannot create bazarr.restart file: ' + repr(e))
else:
logging.info('Bazarr is being restarted...')
restart_file.write(str(''))
restart_file.close()
os._exit(0)
instance = None
def __new__(cls): # __new__ always a classmethod
if not Server.instance:
Server.instance = Server.__Server()
return Server.instance
def __getattr__(self, name):
return getattr(self.instance, name)
def __setattr__(self, name):
return setattr(self.instance, name)
webserver = Server()
if __name__ == "__main__": if __name__ == "__main__":
Server.start() webserver.start()

Loading…
Cancel
Save