Skip to content

Commit

Permalink
fix 数据库连接复用
Browse files Browse the repository at this point in the history
  • Loading branch information
jxxghp committed Aug 22, 2023
1 parent a202b5e commit 781de29
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 40 deletions.
35 changes: 21 additions & 14 deletions app/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
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
Expand All @@ -38,74 +39,76 @@ class Command(metaclass=Singleton):
_event = Event()

def __init__(self):
# 数据库连接
self._db = SessionLocal()
# 事件管理器
self.eventmanager = EventManager()
# 插件管理器
self.pluginmanager = PluginManager()
# 内置命令
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": {}
}
Expand All @@ -123,7 +126,7 @@ def __init__(self):
}
)
# 处理链
self.chain = CommandChian()
self.chain = CommandChian(self._db)
# 广播注册命令菜单
self.chain.register_commands(commands=self.get_commands())
# 消息处理线程
Expand Down Expand Up @@ -233,3 +236,7 @@ def command_event(self, event: ManagerEvent) -> None:
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()
7 changes: 5 additions & 2 deletions app/plugins/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
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
Expand Down Expand Up @@ -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()
# 系统消息
Expand Down
2 changes: 1 addition & 1 deletion app/plugins/bestfilmversion/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
6 changes: 3 additions & 3 deletions app/plugins/dirmonitor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand Down
4 changes: 2 additions & 2 deletions app/plugins/doubanrank/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
6 changes: 3 additions & 3 deletions app/plugins/doubansync/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion app/plugins/mediasyncdel/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
6 changes: 3 additions & 3 deletions app/plugins/nastoolsync/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
6 changes: 3 additions & 3 deletions app/plugins/rsssubscribe/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
23 changes: 15 additions & 8 deletions app/scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
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
Expand All @@ -38,48 +39,50 @@ 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),
name="同步CookieCloud站点")

# 媒体服务器同步
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())
Expand All @@ -99,3 +102,7 @@ def stop(self):
"""
if self._scheduler.running:
self._scheduler.shutdown()

def __del__(self):
if self._db:
self._db.close()

0 comments on commit 781de29

Please sign in to comment.