From f15ccadc2d8b370aa12db516d6977d2345faca84 Mon Sep 17 00:00:00 2001 From: jxxghp Date: Tue, 17 Oct 2023 19:39:19 +0800 Subject: [PATCH] =?UTF-8?q?fix=20=E4=BC=98=E5=8C=96=E8=BF=9E=E6=8E=A5?= =?UTF-8?q?=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/endpoints/dashboard.py | 10 ++++------ app/api/endpoints/webhook.py | 17 +++++++---------- app/chain/dashboard.py | 3 ++- app/chain/system.py | 3 ++- app/chain/webhook.py | 3 ++- app/command.py | 15 ++++++++------- app/db/__init__.py | 4 ++-- app/plugins/moviepilotupdatenotify/__init__.py | 4 ++-- app/scheduler.py | 4 ++++ 9 files changed, 33 insertions(+), 30 deletions(-) diff --git a/app/api/endpoints/dashboard.py b/app/api/endpoints/dashboard.py index d7e8d127..80af9c01 100644 --- a/app/api/endpoints/dashboard.py +++ b/app/api/endpoints/dashboard.py @@ -17,12 +17,11 @@ router = APIRouter() @router.get("/statistic", summary="媒体数量统计", response_model=schemas.Statistic) -def statistic(db: Session = Depends(get_db), - _: schemas.TokenPayload = Depends(verify_token)) -> Any: +def statistic(_: schemas.TokenPayload = Depends(verify_token)) -> Any: """ 查询媒体数量统计信息 """ - media_statistics: Optional[List[schemas.Statistic]] = DashboardChain(db).media_statistic() + media_statistics: Optional[List[schemas.Statistic]] = DashboardChain().media_statistic() if media_statistics: # 汇总各媒体库统计信息 ret_statistic = schemas.Statistic() @@ -57,12 +56,11 @@ def processes(_: schemas.TokenPayload = Depends(verify_token)) -> Any: @router.get("/downloader", summary="下载器信息", response_model=schemas.DownloaderInfo) -def downloader(db: Session = Depends(get_db), - _: schemas.TokenPayload = Depends(verify_token)) -> Any: +def downloader(_: schemas.TokenPayload = Depends(verify_token)) -> Any: """ 查询下载器信息 """ - transfer_info = DashboardChain(db).downloader_info() + transfer_info = DashboardChain().downloader_info() free_space = SystemUtils.free_space(Path(settings.DOWNLOAD_PATH)) if transfer_info: return schemas.DownloaderInfo( diff --git a/app/api/endpoints/webhook.py b/app/api/endpoints/webhook.py index cd6daf42..493c46c6 100644 --- a/app/api/endpoints/webhook.py +++ b/app/api/endpoints/webhook.py @@ -1,27 +1,25 @@ from typing import Any -from fastapi import APIRouter, BackgroundTasks, Request, Depends -from sqlalchemy.orm import Session +from fastapi import APIRouter, BackgroundTasks, Request from app import schemas from app.chain.webhook import WebhookChain from app.core.config import settings -from app.db import get_db router = APIRouter() -def start_webhook_chain(db: Session, body: Any, form: Any, args: Any): +def start_webhook_chain(body: Any, form: Any, args: Any): """ 启动链式任务 """ - WebhookChain(db).message(body=body, form=form, args=args) + WebhookChain().message(body=body, form=form, args=args) @router.post("/", summary="Webhook消息响应", response_model=schemas.Response) async def webhook_message(background_tasks: BackgroundTasks, token: str, request: Request, - db: Session = Depends(get_db),) -> Any: + ) -> Any: """ Webhook响应 """ @@ -30,19 +28,18 @@ async def webhook_message(background_tasks: BackgroundTasks, body = await request.body() form = await request.form() args = request.query_params - background_tasks.add_task(start_webhook_chain, db, body, form, args) + background_tasks.add_task(start_webhook_chain, body, form, args) return schemas.Response(success=True) @router.get("/", summary="Webhook消息响应", response_model=schemas.Response) async def webhook_message(background_tasks: BackgroundTasks, - token: str, request: Request, - db: Session = Depends(get_db)) -> Any: + token: str, request: Request) -> Any: """ Webhook响应 """ if token != settings.API_TOKEN: return schemas.Response(success=False, message="token认证不通过") args = request.query_params - background_tasks.add_task(start_webhook_chain, db, None, None, args) + background_tasks.add_task(start_webhook_chain, None, None, args) return schemas.Response(success=True) diff --git a/app/chain/dashboard.py b/app/chain/dashboard.py index e32c30f7..35258a90 100644 --- a/app/chain/dashboard.py +++ b/app/chain/dashboard.py @@ -2,9 +2,10 @@ from typing import Optional, List from app import schemas from app.chain import ChainBase +from app.utils.singleton import Singleton -class DashboardChain(ChainBase): +class DashboardChain(ChainBase, metaclass=Singleton): """ 各类仪表板统计处理链 """ diff --git a/app/chain/system.py b/app/chain/system.py index d18dd402..88df3326 100644 --- a/app/chain/system.py +++ b/app/chain/system.py @@ -8,10 +8,11 @@ from app.core.config import settings from app.log import logger from app.schemas import Notification, MessageChannel from app.utils.http import RequestUtils +from app.utils.singleton import Singleton from app.utils.system import SystemUtils -class SystemChain(ChainBase): +class SystemChain(ChainBase, metaclass=Singleton): """ 系统级处理链 """ diff --git a/app/chain/webhook.py b/app/chain/webhook.py index 5ee77d12..fc01f788 100644 --- a/app/chain/webhook.py +++ b/app/chain/webhook.py @@ -4,10 +4,11 @@ from typing import Any from app.chain import ChainBase from app.schemas import Notification from app.schemas.types import EventType, MediaImageType, MediaType, NotificationType +from app.utils.singleton import Singleton from app.utils.web import WebUtils -class WebhookChain(ChainBase): +class WebhookChain(ChainBase, metaclass=Singleton): """ Webhook处理链 """ diff --git a/app/command.py b/app/command.py index ccfa1ae7..e3a81e02 100644 --- a/app/command.py +++ b/app/command.py @@ -1,6 +1,7 @@ import importlib +import threading import traceback -from threading import Thread, Event +from threading import Thread from typing import Any, Union, Dict from app.chain import ChainBase @@ -39,7 +40,7 @@ class Command(metaclass=Singleton): _commands = {} # 退出事件 - _event = Event() + _event = threading.Event() def __init__(self): # 数据库连接 @@ -135,25 +136,25 @@ class Command(metaclass=Singleton): "data": {} }, "/clear_cache": { - "func": SystemChain(self._db).remote_clear_cache, + "func": SystemChain().remote_clear_cache, "description": "清理缓存", "category": "管理", "data": {} }, "/restart": { - "func": SystemChain(self._db).restart, + "func": SystemChain().restart, "description": "重启系统", "category": "管理", "data": {} }, "/version": { - "func": SystemChain(self._db).version, + "func": SystemChain().version, "description": "当前版本", "category": "管理", "data": {} }, "/update": { - "func": SystemChain(self._db).update, + "func": SystemChain().update, "description": "更新系统", "category": "管理", "data": {} @@ -179,7 +180,7 @@ class Command(metaclass=Singleton): # 启动事件处理线程 self._thread.start() # 重启msg - SystemChain(self._db).restart_finish() + SystemChain().restart_finish() def __run(self): """ diff --git a/app/db/__init__.py b/app/db/__init__.py index da335492..646d16ae 100644 --- a/app/db/__init__.py +++ b/app/db/__init__.py @@ -9,9 +9,9 @@ Engine = create_engine(f"sqlite:///{settings.CONFIG_PATH}/user.db", echo=False, poolclass=QueuePool, pool_size=1024, - pool_recycle=600, + pool_recycle=3600, pool_timeout=180, - max_overflow=0, + max_overflow=10, connect_args={"timeout": 60}) # 会话工厂 SessionFactory = sessionmaker(autocommit=False, autoflush=False, bind=Engine) diff --git a/app/plugins/moviepilotupdatenotify/__init__.py b/app/plugins/moviepilotupdatenotify/__init__.py index 68f43956..b2222ec1 100644 --- a/app/plugins/moviepilotupdatenotify/__init__.py +++ b/app/plugins/moviepilotupdatenotify/__init__.py @@ -80,7 +80,7 @@ class MoviePilotUpdateNotify(_PluginBase): return # 本地版本 - local_version = SystemChain(self.db).get_local_version() + local_version = SystemChain().get_local_version() if release_version == local_version: logger.info(f"当前版本:{local_version} 远程版本:{release_version} 停止运行") return @@ -98,7 +98,7 @@ class MoviePilotUpdateNotify(_PluginBase): # 自动更新 if self._update: logger.info("开始执行自动更新…") - SystemChain(self.db).update() + SystemChain().update() @staticmethod def __get_release_version(): diff --git a/app/scheduler.py b/app/scheduler.py index 5c0a6380..6c01e914 100644 --- a/app/scheduler.py +++ b/app/scheduler.py @@ -1,4 +1,5 @@ import logging +import threading from datetime import datetime, timedelta from typing import List @@ -39,6 +40,8 @@ class Scheduler(metaclass=Singleton): executors={ 'default': ThreadPoolExecutor(20) }) + # 退出事件 + _event = threading.Event() def __init__(self): # 数据库连接 @@ -258,6 +261,7 @@ class Scheduler(metaclass=Singleton): """ 关闭定时服务 """ + self._event.set() if self._scheduler.running: self._scheduler.shutdown() if self._db: