From ee5a6669e73d93027a0cb1b10e0a7d65bdab377f Mon Sep 17 00:00:00 2001 From: estrella Date: Sun, 17 Sep 2023 21:45:52 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=90=AF=E5=8A=A8=E6=97=B6=E5=8F=AF?= =?UTF-8?q?=E4=BB=A5=E5=89=A5=E7=A6=BB=20qBittorrent=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/src/module/ab_decorator/__init__.py | 2 ++ backend/src/module/ab_decorator/timeout.py | 23 +++++++++++++ backend/src/module/checker/checker.py | 38 +++++++++++++++------ backend/src/module/core/program.py | 13 ++++--- 4 files changed, 58 insertions(+), 18 deletions(-) create mode 100644 backend/src/module/ab_decorator/timeout.py diff --git a/backend/src/module/ab_decorator/__init__.py b/backend/src/module/ab_decorator/__init__.py index 00d2bef5..85b84082 100644 --- a/backend/src/module/ab_decorator/__init__.py +++ b/backend/src/module/ab_decorator/__init__.py @@ -2,6 +2,8 @@ import logging import threading import time +from .timeout import timeout + logger = logging.getLogger(__name__) lock = threading.Lock() diff --git a/backend/src/module/ab_decorator/timeout.py b/backend/src/module/ab_decorator/timeout.py new file mode 100644 index 00000000..32f3a985 --- /dev/null +++ b/backend/src/module/ab_decorator/timeout.py @@ -0,0 +1,23 @@ +import signal + + +def timeout(seconds): + def decorator(func): + def handler(signum, frame): + raise TimeoutError("Function timed out.") + + def wrapper(*args, **kwargs): + # 设置信号处理程序,当超时时触发TimeoutError异常 + signal.signal(signal.SIGALRM, handler) + signal.alarm(seconds) # 设置alarm定时器 + + try: + result = func(*args, **kwargs) + finally: + signal.alarm(0) # 取消alarm定时器 + + return result + + return wrapper + + return decorator diff --git a/backend/src/module/checker/checker.py b/backend/src/module/checker/checker.py index c67ed0c5..48237b63 100644 --- a/backend/src/module/checker/checker.py +++ b/backend/src/module/checker/checker.py @@ -1,12 +1,14 @@ -import os +import logging +import requests from pathlib import Path from module.conf import settings, VERSION from module.downloader import DownloadClient from module.models import Config -from module.network import RequestContent from module.update import version_check +logger = logging.getLogger(__name__) + class Checker: def __init__(self): @@ -26,14 +28,6 @@ class Checker: else: return False - @staticmethod - def check_downloader() -> bool: - with DownloadClient() as client: - if client.authed: - return True - else: - return False - @staticmethod def check_first_run() -> bool: if settings.dict() == Config().dict(): @@ -53,6 +47,28 @@ class Checker: else: return True + @staticmethod + def check_downloader() -> bool: + try: + response = requests.get(settings.downloader.host, timeout=2) + if settings.downloader.type in response.text.lower(): + with DownloadClient() as client: + if client.authed: + return True + else: + return False + else: + return False + except requests.exceptions.Timeout: + logger.error("[Checker] Downloader connect timeout.") + return False + except requests.exceptions.ConnectionError: + logger.error("[Checker] Downloader connect failed.") + return False + except requests.exceptions.InvalidSchema: + logger.error("[Checker] No route to downloader.") + return False + if __name__ == "__main__": - print(Checker().check_version()) + print(Checker().check_downloader()) diff --git a/backend/src/module/core/program.py b/backend/src/module/core/program.py index e17401ce..6785e2fb 100644 --- a/backend/src/module/core/program.py +++ b/backend/src/module/core/program.py @@ -32,24 +32,22 @@ class Program(RenameThread, RSSThread): def startup(self): self.__start_info() - if self.first_run or not self.database: + if not self.database: first_run() - logger.info("First run detected, please configure the program in webui.") + logger.info("[Core] No db file exists, create database file.") return {"status": "First run detected."} if self.legacy_data: logger.info( - "Legacy data detected, starting data migration, please wait patiently." + "[Core] Legacy data detected, starting data migration, please wait patiently." ) data_migration() elif self.version_update: # Update database from_30_to_31() - logger.info("Database updated.") + logger.info("[Core] Database updated.") self.start() def start(self): - if self.first_run: - return {"status": "Not ready to start."} self.stop_event.clear() settings.load() if self.downloader_status: @@ -60,6 +58,7 @@ class Program(RenameThread, RSSThread): logger.info("Program running.") return {"status": "Program started."} else: + self.stop_event.set() return {"status": "Can't connect to downloader. Program not paused."} def stop(self): @@ -80,5 +79,5 @@ class Program(RenameThread, RSSThread): if not self.version_update: return {"status": "No update found."} else: - start_up(True) + start_up() return {"status": "Database updated."}