feat(maizone): 新增定时任务随机间隔功能
为了避免定时任务在完全相同的时间点触发,引入了随机延迟机制。 现在,定时任务的执行间隔将在设定的最小和最大分钟数之间随机波动,使行为模式更难被预测。 此功能可通过配置项进行调整,默认间隔为 5 到 15 分钟。 Co-Authored-By: tt-P607 <68868379+tt-P607@users.noreply.github.com>
This commit is contained in:
@@ -68,6 +68,8 @@ class MaiZoneRefactoredPlugin(BasePlugin):
|
||||
},
|
||||
"schedule": {
|
||||
"enable_schedule": ConfigField(type=bool, default=False, description="是否启用定时发送"),
|
||||
"random_interval_min_minutes": ConfigField(type=int, default=5, description="随机间隔分钟数下限"),
|
||||
"random_interval_max_minutes": ConfigField(type=int, default=15, description="随机间隔分钟数上限"),
|
||||
},
|
||||
"cookie": {
|
||||
"http_fallback_host": ConfigField(type=str, default="127.0.0.1", description="备用Cookie获取服务的主机地址"),
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
"""
|
||||
import asyncio
|
||||
import datetime
|
||||
import random
|
||||
import traceback
|
||||
from typing import Callable
|
||||
|
||||
@@ -91,8 +92,12 @@ class SchedulerService:
|
||||
result.get("message", "")
|
||||
)
|
||||
|
||||
# 6. 等待5分钟后进行下一次检查
|
||||
await asyncio.sleep(300)
|
||||
# 6. 计算并等待一个随机的时间间隔
|
||||
min_minutes = self.get_config("schedule.random_interval_min_minutes", 5)
|
||||
max_minutes = self.get_config("schedule.random_interval_max_minutes", 15)
|
||||
wait_seconds = random.randint(min_minutes * 60, max_minutes * 60)
|
||||
logger.info(f"下一次检查将在 {wait_seconds / 60:.2f} 分钟后进行。")
|
||||
await asyncio.sleep(wait_seconds)
|
||||
|
||||
except asyncio.CancelledError:
|
||||
logger.info("定时任务循环被取消。")
|
||||
|
||||
Reference in New Issue
Block a user