fix 下载器任务同步插件支持周期运行
This commit is contained in:
@ -1,13 +1,18 @@
|
|||||||
import os
|
import os
|
||||||
import time
|
import time
|
||||||
|
from datetime import datetime
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
|
from apscheduler.schedulers.background import BackgroundScheduler
|
||||||
|
from apscheduler.triggers.cron import CronTrigger
|
||||||
|
|
||||||
|
from app.core.config import settings
|
||||||
from app.db.downloadhistory_oper import DownloadHistoryOper
|
from app.db.downloadhistory_oper import DownloadHistoryOper
|
||||||
from app.db.transferhistory_oper import TransferHistoryOper
|
from app.db.transferhistory_oper import TransferHistoryOper
|
||||||
from app.modules.qbittorrent import Qbittorrent
|
from app.modules.qbittorrent import Qbittorrent
|
||||||
from app.modules.transmission import Transmission
|
from app.modules.transmission import Transmission
|
||||||
from app.plugins import _PluginBase
|
from app.plugins import _PluginBase
|
||||||
from typing import Any, List, Dict, Tuple
|
from typing import Any, List, Dict, Tuple, Optional
|
||||||
from app.log import logger
|
from app.log import logger
|
||||||
|
|
||||||
|
|
||||||
@ -34,6 +39,9 @@ class SyncDownloadFiles(_PluginBase):
|
|||||||
auth_level = 2
|
auth_level = 2
|
||||||
|
|
||||||
# 私有属性
|
# 私有属性
|
||||||
|
_enabled = False
|
||||||
|
# 任务执行间隔
|
||||||
|
_time = None
|
||||||
qb = None
|
qb = None
|
||||||
tr = None
|
tr = None
|
||||||
_onlyonce = False
|
_onlyonce = False
|
||||||
@ -43,8 +51,16 @@ class SyncDownloadFiles(_PluginBase):
|
|||||||
downloadhis = None
|
downloadhis = None
|
||||||
transferhis = None
|
transferhis = None
|
||||||
|
|
||||||
|
# 定时器
|
||||||
|
_scheduler: Optional[BackgroundScheduler] = None
|
||||||
|
|
||||||
def init_plugin(self, config: dict = None):
|
def init_plugin(self, config: dict = None):
|
||||||
|
# 停止现有任务
|
||||||
|
self.stop_service()
|
||||||
|
|
||||||
if config:
|
if config:
|
||||||
|
self._enabled = config.get('enabled')
|
||||||
|
self._time = config.get('time') or 6
|
||||||
self._history = config.get('history')
|
self._history = config.get('history')
|
||||||
self._onlyonce = config.get("onlyonce")
|
self._onlyonce = config.get("onlyonce")
|
||||||
self._downloaders = config.get('downloaders') or []
|
self._downloaders = config.get('downloaders') or []
|
||||||
@ -59,19 +75,38 @@ class SyncDownloadFiles(_PluginBase):
|
|||||||
|
|
||||||
# 关闭onlyonce
|
# 关闭onlyonce
|
||||||
self._onlyonce = False
|
self._onlyonce = False
|
||||||
self.update_config({
|
self.__update_config()
|
||||||
"history": self._history,
|
|
||||||
"onlyonce": self._onlyonce,
|
|
||||||
"downloaders": self._downloaders,
|
|
||||||
"dirs": self._dirs
|
|
||||||
})
|
|
||||||
|
|
||||||
self.sync()
|
self.sync()
|
||||||
|
|
||||||
|
if self._enabled:
|
||||||
|
# 定时服务
|
||||||
|
self._scheduler = BackgroundScheduler(timezone=settings.TZ)
|
||||||
|
if self._time:
|
||||||
|
try:
|
||||||
|
self._scheduler.add_job(func=self.sync,
|
||||||
|
trigger="interval",
|
||||||
|
hours=float(self._time.strip()),
|
||||||
|
name="自动同步下载器文件记录")
|
||||||
|
logger.info(f"自动同步下载器文件记录服务启动,时间间隔 {self._time} 小时")
|
||||||
|
except Exception as err:
|
||||||
|
logger.error(f"定时任务配置错误:{err}")
|
||||||
|
|
||||||
|
# 启动任务
|
||||||
|
if self._scheduler.get_jobs():
|
||||||
|
self._scheduler.print_jobs()
|
||||||
|
self._scheduler.start()
|
||||||
|
else:
|
||||||
|
self._enabled = False
|
||||||
|
self.__update_config()
|
||||||
|
|
||||||
def sync(self):
|
def sync(self):
|
||||||
"""
|
"""
|
||||||
同步所选下载器种子记录
|
同步所选下载器种子记录
|
||||||
"""
|
"""
|
||||||
|
start_time = datetime.now()
|
||||||
|
logger.info("开始同步下载器任务文件记录")
|
||||||
|
|
||||||
if not self._downloaders:
|
if not self._downloaders:
|
||||||
logger.error("未选择同步下载器,停止运行")
|
logger.error("未选择同步下载器,停止运行")
|
||||||
return
|
return
|
||||||
@ -161,6 +196,21 @@ class SyncDownloadFiles(_PluginBase):
|
|||||||
self.save_data(f"last_sync_time_{downloader}",
|
self.save_data(f"last_sync_time_{downloader}",
|
||||||
time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))
|
time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))
|
||||||
|
|
||||||
|
# 计算耗时
|
||||||
|
end_time = datetime.now()
|
||||||
|
|
||||||
|
logger.info(f"下载器任务文件记录已同步完成。总耗时 {(end_time - start_time).seconds} 秒")
|
||||||
|
|
||||||
|
def __update_config(self):
|
||||||
|
self.update_config({
|
||||||
|
"enabled": self._enabled,
|
||||||
|
"time": self._time,
|
||||||
|
"history": self._history,
|
||||||
|
"onlyonce": self._onlyonce,
|
||||||
|
"downloaders": self._downloaders,
|
||||||
|
"dirs": self._dirs
|
||||||
|
})
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def __get_origin_torrents(torrents: Any, dl_tpe: str):
|
def __get_origin_torrents(torrents: Any, dl_tpe: str):
|
||||||
# 把种子按照名称和种子大小分组,获取添加时间最早的一个,认定为是源种子,其余为辅种
|
# 把种子按照名称和种子大小分组,获取添加时间最早的一个,认定为是源种子,其余为辅种
|
||||||
@ -284,7 +334,7 @@ class SyncDownloadFiles(_PluginBase):
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
def get_state(self) -> bool:
|
def get_state(self) -> bool:
|
||||||
return False
|
return self._enabled
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_command() -> List[Dict[str, Any]]:
|
def get_command() -> List[Dict[str, Any]]:
|
||||||
@ -308,7 +358,23 @@ class SyncDownloadFiles(_PluginBase):
|
|||||||
'component': 'VCol',
|
'component': 'VCol',
|
||||||
'props': {
|
'props': {
|
||||||
'cols': 12,
|
'cols': 12,
|
||||||
'md': 6
|
'md': 4
|
||||||
|
},
|
||||||
|
'content': [
|
||||||
|
{
|
||||||
|
'component': 'VSwitch',
|
||||||
|
'props': {
|
||||||
|
'model': 'enabled',
|
||||||
|
'label': '开启插件',
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
'component': 'VCol',
|
||||||
|
'props': {
|
||||||
|
'cols': 12,
|
||||||
|
'md': 4
|
||||||
},
|
},
|
||||||
'content': [
|
'content': [
|
||||||
{
|
{
|
||||||
@ -324,7 +390,7 @@ class SyncDownloadFiles(_PluginBase):
|
|||||||
'component': 'VCol',
|
'component': 'VCol',
|
||||||
'props': {
|
'props': {
|
||||||
'cols': 12,
|
'cols': 12,
|
||||||
'md': 6
|
'md': 4
|
||||||
},
|
},
|
||||||
'content': [
|
'content': [
|
||||||
{
|
{
|
||||||
@ -338,6 +404,26 @@ class SyncDownloadFiles(_PluginBase):
|
|||||||
},
|
},
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
'component': 'VRow',
|
||||||
|
'content': [
|
||||||
|
{
|
||||||
|
'component': 'VCol',
|
||||||
|
'props': {
|
||||||
|
'cols': 12,
|
||||||
|
},
|
||||||
|
'content': [
|
||||||
|
{
|
||||||
|
'component': 'VTextField',
|
||||||
|
'props': {
|
||||||
|
'model': 'time',
|
||||||
|
'label': '时间间隔'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
]
|
||||||
|
},
|
||||||
{
|
{
|
||||||
'component': 'VRow',
|
'component': 'VRow',
|
||||||
'content': [
|
'content': [
|
||||||
@ -399,6 +485,7 @@ class SyncDownloadFiles(_PluginBase):
|
|||||||
'component': 'VAlert',
|
'component': 'VAlert',
|
||||||
'props': {
|
'props': {
|
||||||
'text': '如果所选下载器种子很多的话,时间会有点久,请耐心等候,可查看日志。'
|
'text': '如果所选下载器种子很多的话,时间会有点久,请耐心等候,可查看日志。'
|
||||||
|
'时间间隔建议最少每6小时执行一次,防止上次任务没处理完。'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
@ -408,8 +495,10 @@ class SyncDownloadFiles(_PluginBase):
|
|||||||
]
|
]
|
||||||
}
|
}
|
||||||
], {
|
], {
|
||||||
|
"enabled": False,
|
||||||
"onlyonce": False,
|
"onlyonce": False,
|
||||||
"history": False,
|
"history": False,
|
||||||
|
"time": 6,
|
||||||
"dirs": "",
|
"dirs": "",
|
||||||
"downloaders": []
|
"downloaders": []
|
||||||
}
|
}
|
||||||
@ -421,4 +510,11 @@ class SyncDownloadFiles(_PluginBase):
|
|||||||
"""
|
"""
|
||||||
退出插件
|
退出插件
|
||||||
"""
|
"""
|
||||||
pass
|
try:
|
||||||
|
if self._scheduler:
|
||||||
|
self._scheduler.remove_all_jobs()
|
||||||
|
if self._scheduler.running:
|
||||||
|
self._scheduler.shutdown()
|
||||||
|
self._scheduler = None
|
||||||
|
except Exception as e:
|
||||||
|
logger.error("退出插件失败:%s" % str(e))
|
||||||
|
Reference in New Issue
Block a user