Add restart in api

This commit is contained in:
EstrellaXD
2023-04-24 12:22:44 +08:00
parent c47d81e801
commit ce5736c284
4 changed files with 35 additions and 20 deletions

View File

@@ -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)

View File

@@ -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:

View File

@@ -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()

View File

@@ -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):