fix db session

This commit is contained in:
jxxghp
2023-10-18 08:35:16 +08:00
parent 84f5ce8a0b
commit fb78a07662
49 changed files with 170 additions and 224 deletions

View File

@ -5,7 +5,6 @@ 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 SessionFactory
from app.db.models import Base
from app.db.plugindata_oper import PluginDataOper
from app.db.systemconfig_oper import SystemConfigOper
@ -36,12 +35,10 @@ class _PluginBase(metaclass=ABCMeta):
plugin_desc: str = ""
def __init__(self):
# 数据库连接
self.db = SessionFactory()
# 插件数据
self.plugindata = PluginDataOper(self.db)
self.plugindata = PluginDataOper()
# 处理链
self.chain = PluginChian(self.db)
self.chain = PluginChian()
# 系统配置
self.systemconfig = SystemConfigOper()
# 系统消息
@ -186,8 +183,4 @@ class _PluginBase(metaclass=ABCMeta):
))
def close(self):
"""
关闭数据库连接
"""
if self.db:
self.db.close()
pass

View File

@ -72,8 +72,8 @@ class AutoClean(_PluginBase):
# 加载模块
if self._enabled:
self._downloadhis = DownloadHistoryOper(self.db)
self._transferhis = TransferHistoryOper(self.db)
self._downloadhis = DownloadHistoryOper()
self._transferhis = TransferHistoryOper()
# 定时服务
self._scheduler = BackgroundScheduler(timezone=settings.TZ)
@ -181,12 +181,12 @@ class AutoClean(_PluginBase):
for history in transferhis_list:
# 册除媒体库文件
if str(self._cleantype == "dest") or str(self._cleantype == "all"):
TransferChain(self.db).delete_files(Path(history.dest))
TransferChain().delete_files(Path(history.dest))
# 删除记录
self._transferhis.delete(history.id)
# 删除源文件
if str(self._cleantype == "src") or str(self._cleantype == "all"):
TransferChain(self.db).delete_files(Path(history.src))
TransferChain().delete_files(Path(history.src))
# 发送事件
eventmanager.send_event(
EventType.DownloadFileDeleted,

View File

@ -15,6 +15,7 @@ from app import schemas
from app.core.config import settings
from app.core.event import EventManager, eventmanager, Event
from app.db.models.site import Site
from app.db.site_oper import SiteOper
from app.helper.browser import PlaywrightHelper
from app.helper.cloudflare import under_challenge
from app.helper.module import ModuleHelper
@ -52,6 +53,7 @@ class AutoSignIn(_PluginBase):
# 私有属性
sites: SitesHelper = None
siteoper: SiteOper = None
# 事件管理器
event: EventManager = None
# 定时器
@ -74,6 +76,7 @@ class AutoSignIn(_PluginBase):
def init_plugin(self, config: dict = None):
self.sites = SitesHelper()
self.siteoper = SiteOper()
self.event = EventManager()
# 停止现有任务
@ -248,7 +251,7 @@ class AutoSignIn(_PluginBase):
customSites = self.__custom_sites()
site_options = ([{"title": site.name, "value": site.id}
for site in Site.list_order_by_pri(self.db)]
for site in self.siteoper.list_order_by_pri()]
+ [{"title": site.get("name"), "value": site.get("id")}
for site in customSites])
return [
@ -456,7 +459,7 @@ class AutoSignIn(_PluginBase):
"retry_keyword": "错误|失败"
}
def __custom_sites(self) -> List[dict]:
def __custom_sites(self) -> List[Any]:
custom_sites = []
custom_sites_config = self.get_config("CustomSites")
if custom_sites_config and custom_sites_config.get("enabled"):

View File

@ -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.db)
self.subscribechain = SubscribeChain()
# 停止现有任务
self.stop_service()

View File

@ -96,9 +96,9 @@ class DirMonitor(_PluginBase):
_event = threading.Event()
def init_plugin(self, config: dict = None):
self.transferhis = TransferHistoryOper(self.db)
self.downloadhis = DownloadHistoryOper(self.db)
self.transferchian = TransferChain(self.db)
self.transferhis = TransferHistoryOper()
self.downloadhis = DownloadHistoryOper()
self.transferchian = TransferChain()
self.tmdbchain = TmdbChain()
# 清空配置
self._dirconf = {}

View File

@ -66,8 +66,8 @@ class DoubanRank(_PluginBase):
_clearflag = False
def init_plugin(self, config: dict = None):
self.downloadchain = DownloadChain(self.db)
self.subscribechain = SubscribeChain(self.db)
self.downloadchain = DownloadChain()
self.subscribechain = SubscribeChain()
if config:
self._enabled = config.get("enabled")

View File

@ -66,9 +66,9 @@ class DoubanSync(_PluginBase):
def init_plugin(self, config: dict = None):
self.rsshelper = RssHelper()
self.downloadchain = DownloadChain(self.db)
self.searchchain = SearchChain(self.db)
self.subscribechain = SubscribeChain(self.db)
self.downloadchain = DownloadChain()
self.searchchain = SearchChain()
self.subscribechain = SubscribeChain()
# 停止现有任务
self.stop_service()

View File

@ -57,7 +57,7 @@ class DownloadingMsg(_PluginBase):
# 加载模块
if self._enabled:
self._downloadhis = DownloadHistoryOper(self.db)
self._downloadhis = DownloadHistoryOper()
# 定时服务
self._scheduler = BackgroundScheduler(timezone=settings.TZ)
@ -80,7 +80,7 @@ class DownloadingMsg(_PluginBase):
定时推送正在下载进度
"""
# 正在下载种子
torrents = DownloadChain(self.db).list_torrents(status=TorrentStatus.DOWNLOADING)
torrents = DownloadChain().list_torrents(status=TorrentStatus.DOWNLOADING)
if not torrents:
logger.info("当前没有正在下载的任务!")
return

View File

@ -11,6 +11,7 @@ from lxml import etree
from ruamel.yaml import CommentedMap
from app.core.config import settings
from app.db.site_oper import SiteOper
from app.helper.sites import SitesHelper
from app.core.event import eventmanager
@ -55,6 +56,7 @@ class IYUUAutoSeed(_PluginBase):
qb = None
tr = None
sites = None
siteoper = None
torrent = None
# 开关
_enabled = False
@ -96,6 +98,7 @@ class IYUUAutoSeed(_PluginBase):
def init_plugin(self, config: dict = None):
self.sites = SitesHelper()
self.siteoper = SiteOper()
self.torrent = TorrentHelper()
# 读取配置
if config:
@ -176,7 +179,7 @@ class IYUUAutoSeed(_PluginBase):
"""
# 站点的可选项
site_options = [{"title": site.name, "value": site.id}
for site in Site.list_order_by_pri(self.db)]
for site in self.siteoper.list_order_by_pri()]
return [
{
'component': 'VForm',

View File

@ -70,7 +70,7 @@ class LibraryScraper(_PluginBase):
# 启动定时任务 & 立即运行一次
if self._enabled or self._onlyonce:
self.transferhis = TransferHistoryOper(self.db)
self.transferhis = TransferHistoryOper()
self._scheduler = BackgroundScheduler(timezone=settings.TZ)
if self._cron:
logger.info(f"媒体库刮削服务启动,周期:{self._cron}")

View File

@ -62,7 +62,7 @@ class MediaSyncDel(_PluginBase):
tr = None
def init_plugin(self, config: dict = None):
self._transferchain = TransferChain(self.db)
self._transferchain = TransferChain()
self._transferhis = self._transferchain.transferhis
self._downloadhis = self._transferchain.downloadhis
self.episode = Episode()

View File

@ -46,9 +46,9 @@ class NAStoolSync(_PluginBase):
_transfer = False
def init_plugin(self, config: dict = None):
self._transferhistory = TransferHistoryOper(self.db)
self._plugindata = PluginDataOper(self.db)
self._downloadhistory = DownloadHistoryOper(self.db)
self._transferhistory = TransferHistoryOper()
self._plugindata = PluginDataOper()
self._downloadhistory = DownloadHistoryOper()
if config:
self._clear = config.get("clear")

View File

@ -69,7 +69,7 @@ class PersonMeta(_PluginBase):
def init_plugin(self, config: dict = None):
self.tmdbchain = TmdbChain()
self.mschain = MediaServerChain(self.db)
self.mschain = MediaServerChain()
if config:
self._enabled = config.get("enabled")
self._onlyonce = config.get("onlyonce")

View File

@ -69,9 +69,9 @@ class RssSubscribe(_PluginBase):
def init_plugin(self, config: dict = None):
self.rsshelper = RssHelper()
self.downloadchain = DownloadChain(self.db)
self.searchchain = SearchChain(self.db)
self.subscribechain = SubscribeChain(self.db)
self.downloadchain = DownloadChain()
self.searchchain = SearchChain()
self.subscribechain = SubscribeChain()
# 停止现有任务
self.stop_service()

View File

@ -16,6 +16,7 @@ from app.core.config import settings
from app.core.event import Event
from app.core.event import eventmanager
from app.db.models.site import Site
from app.db.site_oper import SiteOper
from app.helper.browser import PlaywrightHelper
from app.helper.module import ModuleHelper
from app.helper.sites import SitesHelper
@ -56,6 +57,7 @@ class SiteStatistic(_PluginBase):
# 私有属性
sites = None
siteoper = None
_scheduler: Optional[BackgroundScheduler] = None
_last_update_time: Optional[datetime] = None
_sites_data: dict = {}
@ -72,6 +74,7 @@ class SiteStatistic(_PluginBase):
def init_plugin(self, config: dict = None):
self.sites = SitesHelper()
self.siteoper = SiteOper()
# 停止现有任务
self.stop_service()
@ -187,7 +190,7 @@ class SiteStatistic(_PluginBase):
customSites = self.__custom_sites()
site_options = ([{"title": site.name, "value": site.id}
for site in Site.list_order_by_pri(self.db)]
for site in self.siteoper.list_order_by_pri()]
+ [{"title": site.get("name"), "value": site.get("id")}
for site in customSites])
@ -1122,7 +1125,7 @@ class SiteStatistic(_PluginBase):
self.save_data("last_update_time", key)
logger.info("站点数据刷新完成")
def __custom_sites(self) -> List[dict]:
def __custom_sites(self) -> List[Any]:
custom_sites = []
custom_sites_config = self.get_config("CustomSites")
if custom_sites_config and custom_sites_config.get("enabled"):

View File

@ -59,8 +59,8 @@ class SyncDownloadFiles(_PluginBase):
self.qb = Qbittorrent()
self.tr = Transmission()
self.downloadhis = DownloadHistoryOper(self.db)
self.transferhis = TransferHistoryOper(self.db)
self.downloadhis = DownloadHistoryOper()
self.transferhis = TransferHistoryOper()
if config:
self._enabled = config.get('enabled')