插件配置持久化:
- plugin_settings 表新增 config_json 字段,支持存储每个插件的自定义配置
- BaseCrawlerPlugin 新增 default_config 属性和 update_config 方法
- PluginSettingsRepository 新增 get_config / set_config 方法
- PluginService 新增 get_plugin_config 和 update_plugin_config
- api/routes/plugins.py 新增 GET /{id}/config 和 POST /{id}/config 接口
- 前端 Plugins.vue 增加配置编辑对话框,支持动态渲染数字/布尔/字符串类型配置
- ip3366 插件示例化:增加 max_pages 配置项,验证配置生效后会动态更新爬取 URL
任务队列持久化:
- 新建 validation_tasks 表:id, ip, port, protocol, status, result, response_time_ms, created_at, updated_at
- 新建 ValidationTaskRepository,提供 insert_batch / acquire_pending / complete_task / reset_processing 等方法
- ValidationQueue 重构:
- submit() 时把任务写入数据库并唤醒 Worker
- Worker 通过 acquire_pending 原子取任务并验证
- 验证完成后更新任务状态并入库有效代理
- 启动时自动恢复之前中断的 processing 任务为 pending
- 支持 drain() 等待所有 pending 完成
- 调度器验证流程同样自动持久化到任务表
其他适配:
- 更新 api/deps.py 和 api/lifespan.py,移除对已删除 settings_service 的残留引用
- 更新前端 pluginService.js 和 api/index.js 增加配置相关 API
141 lines
4.9 KiB
Python
141 lines
4.9 KiB
Python
"""设置数据访问层"""
|
|
import json
|
|
import aiosqlite
|
|
from typing import Optional, Dict, Any
|
|
from core.log import logger
|
|
|
|
|
|
DEFAULT_SETTINGS = {
|
|
"crawl_timeout": 30,
|
|
"validation_timeout": 10,
|
|
"max_retries": 3,
|
|
"default_concurrency": 50,
|
|
"min_proxy_score": 0,
|
|
"proxy_expiry_days": 7,
|
|
"auto_validate": True,
|
|
"validate_interval_minutes": 30,
|
|
}
|
|
|
|
|
|
class SettingsRepository:
|
|
"""系统设置 Repository"""
|
|
|
|
@staticmethod
|
|
async def get_all(db: aiosqlite.Connection) -> Dict[str, Any]:
|
|
settings = DEFAULT_SETTINGS.copy()
|
|
try:
|
|
async with db.execute("SELECT key, value FROM settings") as cursor:
|
|
rows = await cursor.fetchall()
|
|
for key, value in rows:
|
|
# 类型转换
|
|
default = DEFAULT_SETTINGS.get(key)
|
|
if isinstance(default, bool):
|
|
settings[key] = value.lower() == "true"
|
|
elif isinstance(default, int):
|
|
settings[key] = int(value)
|
|
else:
|
|
settings[key] = value
|
|
except Exception as e:
|
|
logger.error(f"get_all settings failed: {e}")
|
|
return settings
|
|
|
|
@staticmethod
|
|
async def save(db: aiosqlite.Connection, settings: Dict[str, Any]) -> bool:
|
|
try:
|
|
for key, value in settings.items():
|
|
await db.execute(
|
|
"""
|
|
INSERT INTO settings (key, value, updated_at)
|
|
VALUES (?, ?, CURRENT_TIMESTAMP)
|
|
ON CONFLICT(key) DO UPDATE SET
|
|
value = excluded.value,
|
|
updated_at = CURRENT_TIMESTAMP
|
|
""",
|
|
(key, str(value)),
|
|
)
|
|
await db.commit()
|
|
return True
|
|
except Exception as e:
|
|
logger.error(f"save settings failed: {e}")
|
|
return False
|
|
|
|
|
|
class PluginSettingsRepository:
|
|
"""插件设置 Repository"""
|
|
|
|
@staticmethod
|
|
async def get_enabled(db: aiosqlite.Connection, plugin_id: str) -> Optional[bool]:
|
|
async with db.execute(
|
|
"SELECT enabled FROM plugin_settings WHERE plugin_id = ?", (plugin_id,)
|
|
) as cursor:
|
|
row = await cursor.fetchone()
|
|
if row:
|
|
return bool(row[0])
|
|
return None
|
|
|
|
@staticmethod
|
|
async def set_enabled(db: aiosqlite.Connection, plugin_id: str, enabled: bool) -> bool:
|
|
try:
|
|
await db.execute(
|
|
"""
|
|
INSERT INTO plugin_settings (plugin_id, enabled, created_at, updated_at)
|
|
VALUES (?, ?, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
|
|
ON CONFLICT(plugin_id) DO UPDATE SET
|
|
enabled = excluded.enabled,
|
|
updated_at = CURRENT_TIMESTAMP
|
|
""",
|
|
(plugin_id, int(enabled)),
|
|
)
|
|
await db.commit()
|
|
return True
|
|
except Exception as e:
|
|
logger.error(f"set_enabled failed for {plugin_id}: {e}")
|
|
return False
|
|
|
|
@staticmethod
|
|
async def get_config(db: aiosqlite.Connection, plugin_id: str) -> Optional[Dict[str, Any]]:
|
|
async with db.execute(
|
|
"SELECT config_json FROM plugin_settings WHERE plugin_id = ?", (plugin_id,)
|
|
) as cursor:
|
|
row = await cursor.fetchone()
|
|
if row and row[0]:
|
|
try:
|
|
return json.loads(row[0])
|
|
except json.JSONDecodeError:
|
|
return None
|
|
return None
|
|
|
|
@staticmethod
|
|
async def set_config(db: aiosqlite.Connection, plugin_id: str, config: Dict[str, Any]) -> bool:
|
|
try:
|
|
await db.execute(
|
|
"""
|
|
INSERT INTO plugin_settings (plugin_id, config_json, updated_at)
|
|
VALUES (?, ?, CURRENT_TIMESTAMP)
|
|
ON CONFLICT(plugin_id) DO UPDATE SET
|
|
config_json = excluded.config_json,
|
|
updated_at = CURRENT_TIMESTAMP
|
|
""",
|
|
(plugin_id, json.dumps(config, ensure_ascii=False)),
|
|
)
|
|
await db.commit()
|
|
return True
|
|
except Exception as e:
|
|
logger.error(f"set_config failed for {plugin_id}: {e}")
|
|
return False
|
|
|
|
@staticmethod
|
|
async def list_all(db: aiosqlite.Connection) -> Dict[str, Dict[str, Any]]:
|
|
result = {}
|
|
async with db.execute("SELECT plugin_id, enabled, config_json FROM plugin_settings") as cursor:
|
|
rows = await cursor.fetchall()
|
|
for plugin_id, enabled, config_json in rows:
|
|
config = {}
|
|
if config_json:
|
|
try:
|
|
config = json.loads(config_json)
|
|
except json.JSONDecodeError:
|
|
pass
|
|
result[plugin_id] = {"enabled": bool(enabled), "config": config}
|
|
return result
|