diff --git a/src/main.py b/src/main.py index dd1b9d16..e2bc8cb9 100644 --- a/src/main.py +++ b/src/main.py @@ -1,13 +1,29 @@ +import os +import signal import logging import multiprocessing +import uvicorn from module import app -from module import api -from module.conf import VERSION, setup_logger +from module.api import router +from module.conf import VERSION, setup_logger, settings logger = logging.getLogger(__name__) +main_process = multiprocessing.Process(target=app.run) + + +@router.get("/api/v1/restart") +async def restart(): + global main_process + logger.info("Restarting...") + os.kill(main_process.pid, signal.SIGTERM) + main_process = multiprocessing.Process(target=app.run) + main_process.start() + logger.info("Restarted") + return {"status": "success"} + def show_info(): with open("icon", "r") as f: @@ -21,14 +37,6 @@ def show_info(): if __name__ == "__main__": setup_logger() show_info() - num_processes = 2 - processes = [] - p1 = multiprocessing.Process(target=app.run) - p2 = multiprocessing.Process(target=api.run) - process_list = [p1, p2] - for p in process_list: - p.start() - processes.append(p) - for p in processes: - p.join() + main_process.start() + uvicorn.run(router, host="0.0.0.0", port=settings.program.webui_port) diff --git a/src/module/app.py b/src/module/app.py index 248ac32b..668f2f18 100644 --- a/src/module/app.py +++ b/src/module/app.py @@ -65,6 +65,7 @@ def run(): setup_logger() # 初始化 reset_log() + settings.reload() download_client = DownloadClient() download_client.init_downloader() if settings.rss_parser.token is None: diff --git a/src/module/conf/config.py b/src/module/conf/config.py index 69c06975..a1b6cfb7 100644 --- a/src/module/conf/config.py +++ b/src/module/conf/config.py @@ -14,6 +14,12 @@ except ImportError: VERSION = "DEV_VERSION" +class Setting(Config): + @staticmethod + def reload(): + load_config_from_file(CONFIG_PATH) + + def save_config_to_file(config: Config, path: str): with open(path, "w", encoding="utf-8") as f: json.dump(config, f, indent=4) @@ -23,7 +29,7 @@ def save_config_to_file(config: Config, path: str): def load_config_from_file(path: str) -> Config: with open(path, "r", encoding="utf-8") as f: config = json.load(f) - return Config(**config) + return Setting(**config) def _val_from_env(env: str, attr: tuple): @@ -40,8 +46,8 @@ def _val_from_env(env: str, attr: tuple): return os.environ[env] -def env_to_config() -> Config: - _settings = Config() +def env_to_config() -> Setting: + _settings = Setting() for key, section in ENV_TO_ATTR.items(): for env, attr in section.items(): if env in os.environ: @@ -65,7 +71,7 @@ elif os.path.isdir("config") and VERSION != "DEV_VERSION": settings = env_to_config() save_config_to_file(settings, CONFIG_PATH) else: - settings = Config() + settings = Setting() diff --git a/src/module/manager/repath.py b/src/module/manager/repath.py index 99e32cd3..b53fd4fd 100644 --- a/src/module/manager/repath.py +++ b/src/module/manager/repath.py @@ -20,12 +20,12 @@ class RuleInfo: @dataclass -class RePathInfo: +class RepathInfo: path: str hashes: list -class RePath: +class RepathTorrents: def __init__(self, download_client: DownloadClient): self._client = download_client self.re_season = re.compile(r"S\d{1,2}") @@ -73,10 +73,10 @@ class RePath: hashes.append(info.hash) infos.remove(info) if hashes: - repath_list.append(RePathInfo(rule.new_path, hashes)) + repath_list.append(RepathInfo(rule.new_path, hashes)) return repath_list - def re_path(self, repath_info: RePathInfo): + def re_path(self, repath_info: RepathInfo): self._client.move_torrent(repath_info.hashes, repath_info.path) def run(self):