2021-01-19 04:49:51 +00:00
|
|
|
# coding=utf-8
|
|
|
|
|
2020-05-18 02:22:36 +00:00
|
|
|
import warnings
|
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
import io
|
2022-09-17 03:57:58 +00:00
|
|
|
import errno
|
2022-05-01 12:00:20 +00:00
|
|
|
|
2021-10-21 00:46:22 +00:00
|
|
|
from waitress.server import create_server
|
2022-09-17 03:57:58 +00:00
|
|
|
from time import sleep
|
2020-05-18 02:22:36 +00:00
|
|
|
|
2022-09-22 03:51:34 +00:00
|
|
|
from api import api_bp
|
2022-06-10 01:02:40 +00:00
|
|
|
from .ui import ui_bp
|
2022-05-01 12:00:20 +00:00
|
|
|
from .get_args import args
|
|
|
|
from .config import settings, base_url
|
2024-01-11 04:04:23 +00:00
|
|
|
from .database import close_database
|
2022-05-01 12:00:20 +00:00
|
|
|
from .app import create_app
|
2020-05-18 02:22:36 +00:00
|
|
|
|
|
|
|
app = create_app()
|
2022-09-22 03:51:34 +00:00
|
|
|
ui_bp.register_blueprint(api_bp, url_prefix='/api')
|
2022-11-07 18:06:49 +00:00
|
|
|
# Mute UserWarning with flask-restx and Flask >= 2.2.0. Will be raised as an exception in 2.3.0
|
|
|
|
# https://github.com/python-restx/flask-restx/issues/485
|
|
|
|
warnings.filterwarnings('ignore', message='The setup method ')
|
2022-06-10 01:02:40 +00:00
|
|
|
app.register_blueprint(ui_bp, url_prefix=base_url.rstrip('/'))
|
2020-05-18 02:22:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Server:
|
|
|
|
def __init__(self):
|
|
|
|
# Mute DeprecationWarning
|
|
|
|
warnings.simplefilter("ignore", DeprecationWarning)
|
|
|
|
# Mute Insecure HTTPS requests made to Sonarr and Radarr
|
|
|
|
warnings.filterwarnings('ignore', message='Unverified HTTPS request')
|
|
|
|
# Mute Python3 BrokenPipeError
|
|
|
|
warnings.simplefilter("ignore", BrokenPipeError)
|
|
|
|
|
2022-09-17 03:57:58 +00:00
|
|
|
self.server = None
|
|
|
|
self.connected = False
|
|
|
|
self.address = str(settings.general.ip)
|
|
|
|
self.port = int(args.port) if args.port else int(settings.general.port)
|
|
|
|
|
|
|
|
while not self.connected:
|
|
|
|
sleep(0.1)
|
|
|
|
self.configure_server()
|
|
|
|
|
|
|
|
def configure_server(self):
|
|
|
|
try:
|
|
|
|
self.server = create_server(app,
|
|
|
|
host=self.address,
|
|
|
|
port=self.port,
|
|
|
|
threads=100)
|
|
|
|
self.connected = True
|
|
|
|
except OSError as error:
|
|
|
|
if error.errno == errno.EADDRNOTAVAIL:
|
|
|
|
logging.exception("BAZARR cannot bind to specified IP, trying with default (0.0.0.0)")
|
|
|
|
self.address = '0.0.0.0'
|
|
|
|
self.connected = False
|
|
|
|
elif error.errno == errno.EADDRINUSE:
|
|
|
|
logging.exception("BAZARR cannot bind to specified TCP port, trying with default (6767)")
|
|
|
|
self.port = '6767'
|
|
|
|
self.connected = False
|
|
|
|
else:
|
|
|
|
logging.exception("BAZARR cannot start because of unhandled exception.")
|
|
|
|
self.shutdown()
|
2020-05-18 02:22:36 +00:00
|
|
|
|
|
|
|
def start(self):
|
2024-01-11 04:04:23 +00:00
|
|
|
logging.info(f'BAZARR is started and waiting for request on http://{self.server.effective_host}:'
|
|
|
|
f'{self.server.effective_port}')
|
2020-05-18 02:22:36 +00:00
|
|
|
try:
|
2024-01-11 04:04:23 +00:00
|
|
|
self.server.run()
|
|
|
|
except (KeyboardInterrupt, SystemExit):
|
2020-05-18 02:22:36 +00:00
|
|
|
self.shutdown()
|
2024-01-11 04:04:23 +00:00
|
|
|
except Exception:
|
|
|
|
pass
|
2020-05-18 02:22:36 +00:00
|
|
|
|
|
|
|
def shutdown(self):
|
|
|
|
try:
|
2024-01-11 04:04:23 +00:00
|
|
|
stop_file = io.open(os.path.join(args.config_dir, "bazarr.stop"), "w", encoding='UTF-8')
|
2020-05-18 02:22:36 +00:00
|
|
|
except Exception as e:
|
2024-01-11 04:04:23 +00:00
|
|
|
logging.error(f'BAZARR Cannot create stop file: {repr(e)}')
|
2020-05-18 02:22:36 +00:00
|
|
|
else:
|
2024-01-11 04:04:23 +00:00
|
|
|
logging.info('Bazarr is being shutdown...')
|
|
|
|
stop_file.write(str(''))
|
|
|
|
stop_file.close()
|
|
|
|
close_database()
|
|
|
|
self.server.close()
|
|
|
|
os._exit(0)
|
2020-05-18 02:22:36 +00:00
|
|
|
|
|
|
|
def restart(self):
|
|
|
|
try:
|
2024-01-11 04:04:23 +00:00
|
|
|
restart_file = io.open(os.path.join(args.config_dir, "bazarr.restart"), "w", encoding='UTF-8')
|
2020-05-18 02:22:36 +00:00
|
|
|
except Exception as e:
|
2024-01-11 04:04:23 +00:00
|
|
|
logging.error(f'BAZARR Cannot create restart file: {repr(e)}')
|
2020-05-18 02:22:36 +00:00
|
|
|
else:
|
2024-01-11 04:04:23 +00:00
|
|
|
logging.info('Bazarr is being restarted...')
|
|
|
|
restart_file.write(str(''))
|
|
|
|
restart_file.close()
|
|
|
|
close_database()
|
|
|
|
self.server.close()
|
|
|
|
os._exit(0)
|
2020-05-18 02:22:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
webserver = Server()
|