优化启动阶段插件与调度初始化耗时

This commit is contained in:
liuwei
2026-05-07 14:46:52 +08:00
parent 3e56c90ab7
commit 19a4ab6e98
3 changed files with 40 additions and 11 deletions

View File

@@ -21,9 +21,9 @@ class PluginScheduleManager:
self._schedule_job_map: Dict[int, str] = {}
self._compensation_tolerance_seconds = 120
def init_and_load(self):
def init_and_load(self, *, run_startup_compensation: bool = False):
self.db.init_tables()
self.reload_from_db()
self.reload_from_db(run_startup_compensation=run_startup_compensation)
def migrate_from_system_jobs(self, system_job_db) -> Dict[str, int]:
"""把历史系统任务配置迁移到插件任务表(幂等)。"""
@@ -252,7 +252,7 @@ class PluginScheduleManager:
self.db.create_log(schedule_id, status, summary, detail)
return {"success": status == "success", "summary": summary, "detail": detail}
def reload_from_db(self):
def reload_from_db(self, *, run_startup_compensation: bool = True):
self.sync_defaults()
# 清理旧注册,避免重复
@@ -281,7 +281,7 @@ class PluginScheduleManager:
try:
trigger_type = row.get("trigger_type", "at_times")
trigger_config = row.get("trigger_config", {"time_list": ["09:00"]})
if self._should_compensate_once(schedule_id, trigger_type, trigger_config):
if run_startup_compensation and self._should_compensate_once(schedule_id, trigger_type, trigger_config):
logger.warning(
f"插件调度触发漏执行补偿: schedule_id={schedule_id}, "
f"plugin={row.get('plugin_name')}, action={row.get('action_key')}"

View File

@@ -191,10 +191,10 @@ class SystemJobLoader:
return False
return latest_log_at < (expected_at - timedelta(seconds=self._compensation_tolerance_seconds))
def init_and_load(self):
def init_and_load(self, *, run_startup_compensation: bool = False):
self.db.init_tables()
self._seed_defaults()
self.reload_from_db()
self.reload_from_db(run_startup_compensation=run_startup_compensation)
def _seed_defaults(self):
for item in self._job_defs.values():
@@ -212,7 +212,7 @@ class SystemJobLoader:
}
)
def reload_from_db(self):
def reload_from_db(self, *, run_startup_compensation: bool = True):
# 每次重载前先补齐默认任务,避免误删后无法恢复
self._seed_defaults()
@@ -275,7 +275,7 @@ class SystemJobLoader:
try:
trigger_type = row.get("trigger_type", definition["trigger_type"])
trigger_config = row.get("trigger_config", definition["trigger_config"])
if self._should_compensate_once(job_key, trigger_type, trigger_config):
if run_startup_compensation and self._should_compensate_once(job_key, trigger_type, trigger_config):
logger.warning(f"系统任务触发漏执行补偿: job_key={job_key}")
self._run_coro_blocking(_wrapped_handler())
except Exception as e: