From 781de2959123314ea74e5aa9c1acec03110dcc4e Mon Sep 17 00:00:00 2001 From: jxxghp Date: Tue, 22 Aug 2023 08:13:44 +0800 Subject: [PATCH] =?UTF-8?q?fix=20=E6=95=B0=E6=8D=AE=E5=BA=93=E8=BF=9E?= =?UTF-8?q?=E6=8E=A5=E5=A4=8D=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/command.py | 35 +++++++++++++++---------- app/plugins/__init__.py | 7 +++-- app/plugins/bestfilmversion/__init__.py | 2 +- app/plugins/dirmonitor/__init__.py | 6 ++--- app/plugins/doubanrank/__init__.py | 4 +-- app/plugins/doubansync/__init__.py | 6 ++--- app/plugins/mediasyncdel/__init__.py | 2 +- app/plugins/nastoolsync/__init__.py | 6 ++--- app/plugins/rsssubscribe/__init__.py | 6 ++--- app/scheduler.py | 23 ++++++++++------ 10 files changed, 57 insertions(+), 40 deletions(-) diff --git a/app/command.py b/app/command.py index fa4e4586..95d45ed2 100644 --- a/app/command.py +++ b/app/command.py @@ -12,6 +12,7 @@ from app.chain.transfer import TransferChain from app.core.event import Event as ManagerEvent from app.core.event import eventmanager, EventManager from app.core.plugin import PluginManager +from app.db import SessionLocal from app.log import logger from app.schemas.types import EventType, MessageChannel from app.utils.object import ObjectUtils @@ -38,6 +39,8 @@ class Command(metaclass=Singleton): _event = Event() def __init__(self): + # 数据库连接 + self._db = SessionLocal() # 事件管理器 self.eventmanager = EventManager() # 插件管理器 @@ -45,67 +48,67 @@ class Command(metaclass=Singleton): # 内置命令 self._commands = { "/cookiecloud": { - "func": CookieCloudChain().remote_sync, + "func": CookieCloudChain(self._db).remote_sync, "description": "同步站点", "data": {} }, "/sites": { - "func": SiteChain().remote_list, + "func": SiteChain(self._db).remote_list, "description": "查询站点", "data": {} }, "/site_cookie": { - "func": SiteChain().remote_cookie, + "func": SiteChain(self._db).remote_cookie, "description": "更新站点Cookie", "data": {} }, "/site_enable": { - "func": SiteChain().remote_enable, + "func": SiteChain(self._db).remote_enable, "description": "启用站点", "data": {} }, "/site_disable": { - "func": SiteChain().remote_disable, + "func": SiteChain(self._db).remote_disable, "description": "禁用站点", "data": {} }, "/mediaserver_sync": { - "func": MediaServerChain().remote_sync, + "func": MediaServerChain(self._db).remote_sync, "description": "同步媒体服务器", "data": {} }, "/subscribes": { - "func": SubscribeChain().remote_list, + "func": SubscribeChain(self._db).remote_list, "description": "查询订阅", "data": {} }, "/subscribe_refresh": { - "func": SubscribeChain().remote_refresh, + "func": SubscribeChain(self._db).remote_refresh, "description": "刷新订阅", "data": {} }, "/subscribe_search": { - "func": SubscribeChain().remote_search, + "func": SubscribeChain(self._db).remote_search, "description": "搜索订阅", "data": {} }, "/subscribe_delete": { - "func": SubscribeChain().remote_delete, + "func": SubscribeChain(self._db).remote_delete, "description": "删除订阅", "data": {} }, "/downloading": { - "func": DownloadChain().remote_downloading, + "func": DownloadChain(self._db).remote_downloading, "description": "正在下载", "data": {} }, "/transfer": { - "func": TransferChain().process, + "func": TransferChain(self._db).process, "description": "下载文件整理", "data": {} }, "/redo": { - "func": TransferChain().remote_transfer, + "func": TransferChain(self._db).remote_transfer, "description": "手动整理", "data": {} } @@ -123,7 +126,7 @@ class Command(metaclass=Singleton): } ) # 处理链 - self.chain = CommandChian() + self.chain = CommandChian(self._db) # 广播注册命令菜单 self.chain.register_commands(commands=self.get_commands()) # 消息处理线程 @@ -233,3 +236,7 @@ class Command(metaclass=Singleton): args = " ".join(event_str.split()[1:]) if self.get(cmd): self.execute(cmd, args, event_channel, event_user) + + def __del__(self): + if self._db: + self._db.close() diff --git a/app/plugins/__init__.py b/app/plugins/__init__.py index afc4e0c8..7399ece4 100644 --- a/app/plugins/__init__.py +++ b/app/plugins/__init__.py @@ -5,6 +5,7 @@ from typing import Any, List, Dict, Tuple from app.chain import ChainBase from app.core.config import settings from app.core.event import EventManager +from app.db import SessionLocal from app.db.models import Base from app.db.plugindata_oper import PluginDataOper from app.db.systemconfig_oper import SystemConfigOper @@ -37,10 +38,12 @@ class _PluginBase(metaclass=ABCMeta): plugin_desc: str = "" def __init__(self): + # 数据库连接 + self.db = SessionLocal() # 插件数据 - self.plugindata = PluginDataOper() + self.plugindata = PluginDataOper(self.db) # 处理链 - self.chain = PluginChian() + self.chain = PluginChian(self.db) # 系统配置 self.systemconfig = SystemConfigOper() # 系统消息 diff --git a/app/plugins/bestfilmversion/__init__.py b/app/plugins/bestfilmversion/__init__.py index 279aa347..1e516e6a 100644 --- a/app/plugins/bestfilmversion/__init__.py +++ b/app/plugins/bestfilmversion/__init__.py @@ -62,7 +62,7 @@ class BestFilmVersion(_PluginBase): def init_plugin(self, config: dict = None): self._cache_path = settings.TEMP_PATH / "__best_film_version_cache__" - self.subscribechain = SubscribeChain() + self.subscribechain = SubscribeChain(self.db) # 停止现有任务 self.stop_service() diff --git a/app/plugins/dirmonitor/__init__.py b/app/plugins/dirmonitor/__init__.py index 4f27ecf2..1bb81f45 100644 --- a/app/plugins/dirmonitor/__init__.py +++ b/app/plugins/dirmonitor/__init__.py @@ -90,9 +90,9 @@ class DirMonitor(_PluginBase): tr = None def init_plugin(self, config: dict = None): - self.transferhis = TransferHistoryOper() - self.downloadhis = DownloadHistoryOper() - self.transferchian = TransferChain() + self.transferhis = TransferHistoryOper(self.db) + self.downloadhis = DownloadHistoryOper(self.db) + self.transferchian = TransferChain(self.db) # 清空配置 self._dirconf = {} diff --git a/app/plugins/doubanrank/__init__.py b/app/plugins/doubanrank/__init__.py index d9d8a1ac..39a6fa7b 100644 --- a/app/plugins/doubanrank/__init__.py +++ b/app/plugins/doubanrank/__init__.py @@ -65,8 +65,8 @@ class DoubanRank(_PluginBase): _clearflag = False def init_plugin(self, config: dict = None): - self.downloadchain = DownloadChain() - self.subscribechain = SubscribeChain() + self.downloadchain = DownloadChain(self.db) + self.subscribechain = SubscribeChain(self.db) if config: self._enabled = config.get("enabled") diff --git a/app/plugins/doubansync/__init__.py b/app/plugins/doubansync/__init__.py index 4d5667ec..a5684a0f 100644 --- a/app/plugins/doubansync/__init__.py +++ b/app/plugins/doubansync/__init__.py @@ -66,9 +66,9 @@ class DoubanSync(_PluginBase): def init_plugin(self, config: dict = None): self.rsshelper = RssHelper() - self.downloadchain = DownloadChain() - self.searchchain = SearchChain() - self.subscribechain = SubscribeChain() + self.downloadchain = DownloadChain(self.db) + self.searchchain = SearchChain(self.db) + self.subscribechain = SubscribeChain(self.db) # 停止现有任务 self.stop_service() diff --git a/app/plugins/mediasyncdel/__init__.py b/app/plugins/mediasyncdel/__init__.py index 339721cc..56a4bf3e 100644 --- a/app/plugins/mediasyncdel/__init__.py +++ b/app/plugins/mediasyncdel/__init__.py @@ -61,7 +61,7 @@ class MediaSyncDel(_PluginBase): tr = None def init_plugin(self, config: dict = None): - self._transferhis = TransferHistoryOper() + self._transferhis = TransferHistoryOper(self.db) self.episode = Episode() self.qb = Qbittorrent() self.tr = Transmission() diff --git a/app/plugins/nastoolsync/__init__.py b/app/plugins/nastoolsync/__init__.py index 9ed82215..e9ea8edc 100644 --- a/app/plugins/nastoolsync/__init__.py +++ b/app/plugins/nastoolsync/__init__.py @@ -51,9 +51,9 @@ class NAStoolSync(_PluginBase): tr = None def init_plugin(self, config: dict = None): - self._transferhistory = TransferHistoryOper() - self._plugindata = PluginDataOper() - self._downloadhistory = DownloadHistoryOper() + self._transferhistory = TransferHistoryOper(self.db) + self._plugindata = PluginDataOper(self.db) + self._downloadhistory = DownloadHistoryOper(self.db) if config: self._clear = config.get("clear") diff --git a/app/plugins/rsssubscribe/__init__.py b/app/plugins/rsssubscribe/__init__.py index 0d855384..3d8956ec 100644 --- a/app/plugins/rsssubscribe/__init__.py +++ b/app/plugins/rsssubscribe/__init__.py @@ -70,9 +70,9 @@ class RssSubscribe(_PluginBase): def init_plugin(self, config: dict = None): self.rsshelper = RssHelper() - self.downloadchain = DownloadChain() - self.searchchain = SearchChain() - self.subscribechain = SubscribeChain() + self.downloadchain = DownloadChain(self.db) + self.searchchain = SearchChain(self.db) + self.subscribechain = SubscribeChain(self.db) # 停止现有任务 self.stop_service() diff --git a/app/scheduler.py b/app/scheduler.py index 88f7af7d..31c9e714 100644 --- a/app/scheduler.py +++ b/app/scheduler.py @@ -12,6 +12,7 @@ from app.chain.rss import RssChain from app.chain.subscribe import SubscribeChain from app.chain.transfer import TransferChain from app.core.config import settings +from app.db import SessionLocal from app.log import logger from app.utils.singleton import Singleton from app.utils.timer import TimerUtils @@ -38,12 +39,14 @@ class Scheduler(metaclass=Singleton): }) def __init__(self): + # 数据库连接 + self._db = SessionLocal() # 调试模式不启动定时服务 if settings.DEV: return # CookieCloud定时同步 if settings.COOKIECLOUD_INTERVAL: - self._scheduler.add_job(CookieCloudChain().process, + self._scheduler.add_job(CookieCloudChain(self._db).process, "interval", minutes=settings.COOKIECLOUD_INTERVAL, next_run_time=datetime.now(pytz.timezone(settings.TZ)) + timedelta(minutes=1), @@ -51,35 +54,35 @@ class Scheduler(metaclass=Singleton): # 媒体服务器同步 if settings.MEDIASERVER_SYNC_INTERVAL: - self._scheduler.add_job(MediaServerChain().sync, "interval", + self._scheduler.add_job(MediaServerChain(self._db).sync, "interval", hours=settings.MEDIASERVER_SYNC_INTERVAL, next_run_time=datetime.now(pytz.timezone(settings.TZ)) + timedelta(minutes=5), name="同步媒体服务器") # 新增订阅时搜索(5分钟检查一次) - self._scheduler.add_job(SubscribeChain().search, "interval", + self._scheduler.add_job(SubscribeChain(self._db).search, "interval", minutes=5, kwargs={'state': 'N'}) # 订阅状态每隔12小时搜索一次 - self._scheduler.add_job(SubscribeChain().search, "interval", + self._scheduler.add_job(SubscribeChain(self._db).search, "interval", hours=12, kwargs={'state': 'R'}, name="订阅搜索") # 站点首页种子定时刷新缓存并匹配订阅 triggers = TimerUtils.random_scheduler(num_executions=30) for trigger in triggers: - self._scheduler.add_job(SubscribeChain().refresh, "cron", + self._scheduler.add_job(SubscribeChain(self._db).refresh, "cron", hour=trigger.hour, minute=trigger.minute, name="订阅刷新") # 自定义订阅 - self._scheduler.add_job(RssChain().refresh, "interval", + self._scheduler.add_job(RssChain(self._db).refresh, "interval", minutes=30, name="自定义订阅刷新") # 下载器文件转移(每5分钟) if settings.DOWNLOADER_MONITOR: - self._scheduler.add_job(TransferChain().process, "interval", minutes=5, name="下载文件整理") + self._scheduler.add_job(TransferChain(self._db).process, "interval", minutes=5, name="下载文件整理") # 公共定时服务 - self._scheduler.add_job(SchedulerChain().scheduler_job, "interval", minutes=10) + self._scheduler.add_job(SchedulerChain(self._db).scheduler_job, "interval", minutes=10) # 打印服务 logger.debug(self._scheduler.print_jobs()) @@ -99,3 +102,7 @@ class Scheduler(metaclass=Singleton): """ if self._scheduler.running: self._scheduler.shutdown() + + def __del__(self): + if self._db: + self._db.close()