fix: 启动时可以剥离 qBittorrent。

This commit is contained in:
estrella
2023-09-17 21:45:52 +08:00
parent 46f643fb40
commit ee5a6669e7
4 changed files with 58 additions and 18 deletions

View File

@@ -2,6 +2,8 @@ import logging
import threading
import time
from .timeout import timeout
logger = logging.getLogger(__name__)
lock = threading.Lock()

View File

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

View File

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

View File

@@ -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."}