fix 异步启动

This commit is contained in:
thsrite
2024-04-18 11:07:41 +08:00
parent 3b9213789e
commit bf919c8e39
4 changed files with 48 additions and 36 deletions

View File

@@ -32,5 +32,5 @@ MoviePilot三方插件市场https://github.com/thsrite/MoviePilot-Plugins/
- [自定义命令 1.5](docs%2FCustomCommand.md)
- docker自定义任务 1.2
- 插件彻底卸载 1.0
- 实时软连接 1.1
- 实时软连接 1.2

View File

@@ -306,13 +306,12 @@
"FileSoftLink": {
"name": "实时软连接",
"description": "监控目录文件变化,媒体文件软连接,其他文件可选复制。",
"version": "1.1",
"version": "1.2",
"icon": "https://raw.githubusercontent.com/thsrite/MoviePilot-Plugins/main/icons/softlink.png",
"author": "thsrite",
"level": 1,
"history": {
"v1.1": "fix bug",
"v1.0": "init"
"v1.2": "异步启动"
}
}
}

View File

@@ -48,7 +48,6 @@ class CloudStrm(_PluginBase):
_rebuild = False
_https = False
_observer = []
_video_formats = ('.mp4', '.avi', '.rmvb', '.wmv', '.mov', '.mkv', '.flv', '.ts', '.webm', '.iso', '.mpg', '.m2ts')
__cloud_files_json = "cloud_files.json"
_dirconf = {}
@@ -231,7 +230,7 @@ class CloudStrm(_PluginBase):
continue
# 不复制非媒体文件时直接过滤掉非媒体文件
if not self._copy_files and not file.lower().endswith(self._video_formats):
if not self._copy_files and Path(file).suffix not in settings.RMT_MEDIAEXT:
continue
if source_file not in self.__cloud_files:
@@ -278,7 +277,7 @@ class CloudStrm(_PluginBase):
continue
# 不复制非媒体文件时直接过滤掉非媒体文件
if not self._copy_files and not file.lower().endswith(self._video_formats):
if not self._copy_files and Path(file).suffix not in settings.RMT_MEDIAEXT:
continue
logger.info(f"扫描到新文件 {source_file},正在开始处理")
@@ -341,7 +340,7 @@ class CloudStrm(_PluginBase):
os.makedirs(Path(dest_file).parent)
# 视频文件创建.strm文件
if dest_file.lower().endswith(self._video_formats):
if Path(dest_file).suffix in settings.RMT_MEDIAEXT:
# 创建.strm文件
self.__create_strm_file(scheme="https" if self._https else "http",
dest_file=dest_file,

View File

@@ -53,7 +53,7 @@ class FileSoftLink(_PluginBase):
# 插件图标
plugin_icon = "https://raw.githubusercontent.com/thsrite/MoviePilot-Plugins/main/icons/softlink.png"
# 插件版本
plugin_version = "1.1"
plugin_version = "1.2"
# 插件作者
plugin_author = "thsrite"
# 作者主页
@@ -145,31 +145,15 @@ class FileSoftLink(_PluginBase):
logger.debug(str(e))
pass
try:
if self._mode == "compatibility":
# 兼容模式目录同步性能降低且NAS不能休眠但可以兼容挂载的远程共享目录如SMB
observer = PollingObserver(timeout=10)
else:
# 内部处理系统操作类型选择最优解
observer = Observer(timeout=10)
self._observer.append(observer)
observer.schedule(FileMonitorHandler(mon_path, self), path=mon_path, recursive=True)
observer.daemon = True
observer.start()
logger.info(f"{mon_path} 的目录监控服务启动")
except Exception as e:
err_msg = str(e)
if "inotify" in err_msg and "reached" in err_msg:
logger.warn(
f"目录监控服务启动出现异常:{err_msg}请在宿主机上不是docker容器内执行以下命令并重启"
+ """
echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf
echo fs.inotify.max_user_instances=524288 | sudo tee -a /etc/sysctl.conf
sudo sysctl -p
""")
else:
logger.error(f"{mon_path} 启动目录监控失败:{err_msg}")
self.systemmessage.put(f"{mon_path} 启动目录监控失败:{err_msg}")
# 异步开启云盘监控
logger.info(f"异步开启实时硬链接 {mon_path} {self._mode}延迟5s启动")
self._scheduler.add_job(func=self.start_monitor, trigger='date',
run_date=datetime.datetime.now(
tz=pytz.timezone(settings.TZ)) + datetime.timedelta(seconds=5),
name=f"实时硬链接 {mon_path}",
kwargs={
"source_dir": mon_path
})
# 运行一次定时服务
if self._onlyonce:
@@ -188,6 +172,36 @@ class FileSoftLink(_PluginBase):
self._scheduler.print_jobs()
self._scheduler.start()
def start_monitor(self, source_dir: str):
"""
异步开启实时软链接
"""
try:
if str(self._mode) == "compatibility":
# 兼容模式目录同步性能降低且NAS不能休眠但可以兼容挂载的远程共享目录如SMB
observer = PollingObserver(timeout=10)
else:
# 内部处理系统操作类型选择最优解
observer = Observer(timeout=10)
self._observer.append(observer)
observer.schedule(FileMonitorHandler(source_dir, self), path=source_dir, recursive=True)
observer.daemon = True
observer.start()
logger.info(f"{source_dir} 的实时软链接服务启动")
except Exception as e:
err_msg = str(e)
if "inotify" in err_msg and "reached" in err_msg:
logger.warn(
f"云盘监控服务启动出现异常:{err_msg}请在宿主机上不是docker容器内执行以下命令并重启"
+ """
echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf
echo fs.inotify.max_user_instances=524288 | sudo tee -a /etc/sysctl.conf
sudo sysctl -p
""")
else:
logger.error(f"{source_dir} 启动云盘监控失败:{err_msg}")
self.systemmessage.put(f"{source_dir} 启动云盘监控失败:{err_msg}")
def __update_config(self):
"""
更新配置
@@ -228,7 +242,7 @@ class FileSoftLink(_PluginBase):
# 遍历所有监控目录
for mon_path in self._dirconf.keys():
# 遍历目录下所有文件
for file_path in SystemUtils.list_files(Path(mon_path), settings.RMT_MEDIAEXT):
for file_path in SystemUtils.list_files(Path(mon_path), ['.*']):
self.__handle_file(event_path=str(file_path), mon_path=mon_path)
logger.info("全量同步监控目录完成!")
@@ -321,7 +335,7 @@ class FileSoftLink(_PluginBase):
os.makedirs(Path(target_file).parent)
# 媒体文件软连接
if target_file.lower().endswith(self._video_formats):
if Path(target_file).suffix in settings.RMT_MEDIAEXT:
SystemUtils.softlink(str(file_path), target_file)
else:
if self._copy_files: