S-13 清理策略
这个提交存在于:
父节点
55850dc45b
当前提交
a28ef3e971
共有 6 个文件被更改,包括 66 次插入 和 6 次删除
|
|
@ -7,6 +7,7 @@
|
||||||
- `/jm download <id>` 或 `/jm 下载 <id>`:下载、归档,并按配置触发 Komga 扫描。
|
- `/jm download <id>` 或 `/jm 下载 <id>`:下载、归档,并按配置触发 Komga 扫描。
|
||||||
- `/jm status`:查看当前归档与 Komga 配置状态。
|
- `/jm status`:查看当前归档与 Komga 配置状态。
|
||||||
- `/jm scan`:手动触发一次 Komga 扫描。
|
- `/jm scan`:手动触发一次 Komga 扫描。
|
||||||
|
- `/jm cleanup`:清理超过 `cleanup_retention_days` 的临时工作目录。
|
||||||
|
|
||||||
## 归档结构
|
## 归档结构
|
||||||
|
|
||||||
|
|
@ -27,6 +28,8 @@ CBZ 内部会包含:
|
||||||
|
|
||||||
当 `archive_format` 设置为 `pdf` 时,插件会按 `pdf_chunk_size` 分块生成临时 PDF,再合并为最终文件,避免大本子转换时一次性占用过多内存。
|
当 `archive_format` 设置为 `pdf` 时,插件会按 `pdf_chunk_size` 分块生成临时 PDF,再合并为最终文件,避免大本子转换时一次性占用过多内存。
|
||||||
|
|
||||||
|
归档成功后,插件默认删除本次临时下载目录;归档库中的 CBZ/PDF 不会被自动删除。历史临时目录可通过 `/jm cleanup` 清理。
|
||||||
|
|
||||||
## Komga 联动
|
## Komga 联动
|
||||||
|
|
||||||
在 AstrBot 插件配置里填写:
|
在 AstrBot 插件配置里填写:
|
||||||
|
|
|
||||||
|
|
@ -35,6 +35,11 @@
|
||||||
"type": "bool",
|
"type": "bool",
|
||||||
"default": true
|
"default": true
|
||||||
},
|
},
|
||||||
|
"cleanup_retention_days": {
|
||||||
|
"description": "手动清理临时目录时保留最近多少天的工作目录;设为 0 会清理全部历史工作目录。",
|
||||||
|
"type": "int",
|
||||||
|
"default": 7
|
||||||
|
},
|
||||||
"download_timeout_seconds": {
|
"download_timeout_seconds": {
|
||||||
"description": "单个下载任务的超时时间,单位秒。",
|
"description": "单个下载任务的超时时间,单位秒。",
|
||||||
"type": "int",
|
"type": "int",
|
||||||
|
|
|
||||||
18
main.py
18
main.py
|
|
@ -98,6 +98,7 @@ class JMDownloadPlugin(Star):
|
||||||
f"下载重试次数:{self.settings.download_retry_count}",
|
f"下载重试次数:{self.settings.download_retry_count}",
|
||||||
f"进度提示间隔:{self.settings.progress_interval_seconds} 秒",
|
f"进度提示间隔:{self.settings.progress_interval_seconds} 秒",
|
||||||
f"发送归档文件:{self.settings.send_archive_file}",
|
f"发送归档文件:{self.settings.send_archive_file}",
|
||||||
|
f"临时目录保留:{self.settings.cleanup_retention_days} 天",
|
||||||
f"Komga 自动扫描:{self.settings.komga.enabled}",
|
f"Komga 自动扫描:{self.settings.komga.enabled}",
|
||||||
f"Komga 配置完整:{self.settings.komga.can_scan}",
|
f"Komga 配置完整:{self.settings.komga.can_scan}",
|
||||||
f"Komga 地址:{self.settings.komga.base_url or '未配置'}",
|
f"Komga 地址:{self.settings.komga.base_url or '未配置'}",
|
||||||
|
|
@ -119,6 +120,23 @@ class JMDownloadPlugin(Star):
|
||||||
yield event.plain_result(result.message)
|
yield event.plain_result(result.message)
|
||||||
event.stop_event()
|
event.stop_event()
|
||||||
|
|
||||||
|
@jm.command("cleanup")
|
||||||
|
async def cleanup(self, event: AstrMessageEvent):
|
||||||
|
"""Cleanup old JMDownload work directories."""
|
||||||
|
if self._deny_non_admin(event):
|
||||||
|
yield event.plain_result("JMDownload 当前配置仅允许管理员执行清理命令。")
|
||||||
|
event.stop_event()
|
||||||
|
return
|
||||||
|
|
||||||
|
try:
|
||||||
|
removed = await asyncio.to_thread(self.downloader.cleanup_old_work_dirs)
|
||||||
|
except JMDownloadError as exc:
|
||||||
|
yield event.plain_result(f"清理失败:{exc.user_message}")
|
||||||
|
event.stop_event()
|
||||||
|
return
|
||||||
|
yield event.plain_result(f"清理完成:删除 {removed} 个临时工作目录。")
|
||||||
|
event.stop_event()
|
||||||
|
|
||||||
def _deny_non_admin(self, event: AstrMessageEvent) -> bool:
|
def _deny_non_admin(self, event: AstrMessageEvent) -> bool:
|
||||||
return bool(self.settings.admin_only and not event.is_admin())
|
return bool(self.settings.admin_only and not event.is_admin())
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -58,10 +58,15 @@ class ArchiveService:
|
||||||
if tmp_path.exists():
|
if tmp_path.exists():
|
||||||
tmp_path.unlink()
|
tmp_path.unlink()
|
||||||
|
|
||||||
|
try:
|
||||||
if self.settings.archive_format == "cbz":
|
if self.settings.archive_format == "cbz":
|
||||||
self._create_cbz(tmp_path, downloaded, series_name, book_name)
|
self._create_cbz(tmp_path, downloaded, series_name, book_name)
|
||||||
else:
|
else:
|
||||||
self._create_pdf(tmp_path, downloaded)
|
self._create_pdf(tmp_path, downloaded)
|
||||||
|
except Exception:
|
||||||
|
if tmp_path.exists():
|
||||||
|
tmp_path.unlink()
|
||||||
|
raise
|
||||||
|
|
||||||
os.replace(tmp_path, target_path)
|
os.replace(tmp_path, target_path)
|
||||||
return ArchiveResult(
|
return ArchiveResult(
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ from __future__ import annotations
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
import shutil
|
import shutil
|
||||||
|
import time
|
||||||
from collections.abc import Awaitable, Callable
|
from collections.abc import Awaitable, Callable
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
@ -124,8 +125,31 @@ class JMDownloadService:
|
||||||
def cleanup(self, downloaded: DownloadedComic) -> None:
|
def cleanup(self, downloaded: DownloadedComic) -> None:
|
||||||
if not self.settings.delete_work_dir_after_archive:
|
if not self.settings.delete_work_dir_after_archive:
|
||||||
return
|
return
|
||||||
if downloaded.work_dir.exists():
|
self._remove_work_dir(downloaded.work_dir)
|
||||||
shutil.rmtree(downloaded.work_dir)
|
|
||||||
|
def cleanup_old_work_dirs(self, retention_days: int | None = None) -> int:
|
||||||
|
retention_days = self.settings.cleanup_retention_days if retention_days is None else retention_days
|
||||||
|
if not self.settings.work_root.exists():
|
||||||
|
return 0
|
||||||
|
|
||||||
|
threshold = time.time() - retention_days * 86400
|
||||||
|
removed = 0
|
||||||
|
for child in self.settings.work_root.iterdir():
|
||||||
|
if not child.is_dir():
|
||||||
|
continue
|
||||||
|
if retention_days > 0 and child.stat().st_mtime >= threshold:
|
||||||
|
continue
|
||||||
|
self._remove_work_dir(child)
|
||||||
|
removed += 1
|
||||||
|
return removed
|
||||||
|
|
||||||
|
def _remove_work_dir(self, path: Path) -> None:
|
||||||
|
work_root = self.settings.work_root.resolve()
|
||||||
|
target = path.resolve()
|
||||||
|
if target == work_root or work_root not in target.parents:
|
||||||
|
raise DownloadError(f"拒绝清理工作目录之外的路径:{target}")
|
||||||
|
if target.exists():
|
||||||
|
shutil.rmtree(target)
|
||||||
|
|
||||||
def _download_sync(self, comic_id: str, config_path: Path) -> str:
|
def _download_sync(self, comic_id: str, config_path: Path) -> str:
|
||||||
try:
|
try:
|
||||||
|
|
|
||||||
|
|
@ -56,6 +56,7 @@ class PluginSettings:
|
||||||
send_archive_file: bool = False
|
send_archive_file: bool = False
|
||||||
overwrite_existing: bool = False
|
overwrite_existing: bool = False
|
||||||
delete_work_dir_after_archive: bool = True
|
delete_work_dir_after_archive: bool = True
|
||||||
|
cleanup_retention_days: int = 7
|
||||||
download_timeout_seconds: int = 3600
|
download_timeout_seconds: int = 3600
|
||||||
download_retry_count: int = 2
|
download_retry_count: int = 2
|
||||||
progress_interval_seconds: int = 30
|
progress_interval_seconds: int = 30
|
||||||
|
|
@ -97,6 +98,10 @@ class PluginSettings:
|
||||||
delete_work_dir_after_archive=bool(
|
delete_work_dir_after_archive=bool(
|
||||||
config.get("delete_work_dir_after_archive", True),
|
config.get("delete_work_dir_after_archive", True),
|
||||||
),
|
),
|
||||||
|
cleanup_retention_days=max(
|
||||||
|
0,
|
||||||
|
_int_value(config.get("cleanup_retention_days"), 7),
|
||||||
|
),
|
||||||
download_timeout_seconds=max(
|
download_timeout_seconds=max(
|
||||||
60,
|
60,
|
||||||
_int_value(config.get("download_timeout_seconds"), 3600),
|
_int_value(config.get("download_timeout_seconds"), 3600),
|
||||||
|
|
|
||||||
正在加载…
添加表格
在新工单中引用