Compare commits

..

1 Commits

Author SHA1 Message Date
pre-commit-ci[bot]
e6f6ad0559 🚨 auto fix by pre-commit hooks 2025-10-22 10:22:51 +00:00
9 changed files with 18 additions and 83 deletions

View File

@ -111,14 +111,6 @@ __plugin_meta__ = PluginMetadata(
default_value=300,
type=int,
),
RegisterConfig(
module="SchedulerManager",
key="DEFAULT_INTERVAL_SECONDS",
value=0,
help="为多目标定时任务设置的默认串行执行间隔秒数(大于0时生效),用于控制任务间的固定时间间隔。",
default_value=0,
type=int,
),
],
).to_dict(),
)

View File

@ -128,11 +128,6 @@ schedule_cmd = on_alconna(
Args["spread_seconds", int],
help_text="设置多目标执行的分散延迟(秒)",
),
Option(
"--fixed-interval",
Args["interval_seconds", int],
help_text="设置任务间的固定执行间隔(秒),将强制串行",
),
Option(
"--permission",
Args["perm_level", int],
@ -147,8 +142,8 @@ schedule_cmd = on_alconna(
Subcommand(
"删除",
Args[
"schedule_ids?",
MultiVar(int),
"schedule_id?",
int,
Field(unmatch_tips=lambda text: f"任务ID '{text}' 必须是数字!"),
],
*create_targeting_options(),
@ -158,8 +153,8 @@ schedule_cmd = on_alconna(
Subcommand(
"暂停",
Args[
"schedule_ids?",
MultiVar(int),
"schedule_id?",
int,
Field(unmatch_tips=lambda text: f"任务ID '{text}' 必须是数字!"),
],
*create_targeting_options(),
@ -169,8 +164,8 @@ schedule_cmd = on_alconna(
Subcommand(
"恢复",
Args[
"schedule_ids?",
MultiVar(int),
"schedule_id?",
int,
Field(unmatch_tips=lambda text: f"任务ID '{text}' 必须是数字!"),
],
*create_targeting_options(),

View File

@ -65,7 +65,6 @@ class SchedulerAdminService:
job_name: str | None,
jitter: int | None,
spread: int | None,
interval: int | None,
created_by: str,
) -> str:
"""创建或更新一个定时任务"""
@ -78,8 +77,6 @@ class SchedulerAdminService:
execution_options["jitter"] = jitter
if spread is not None:
execution_options["spread"] = spread
if interval is not None:
execution_options["interval"] = interval
for target_desc in targets:
target_type, target_id = self._resolve_target_descriptor(target_desc)

View File

@ -158,7 +158,7 @@ async def GetTargeter(
event: Event,
bot: Bot,
arp: Arparma = AlconnaMatches(),
schedule_ids: Match[list[int]] = AlconnaMatch("schedule_ids"),
schedule_id: Match[int] = AlconnaMatch("schedule_id"),
plugin_name: Match[str] = AlconnaMatch("plugin_name"),
group_ids: Match[list[str]] = AlconnaMatch("group_ids"),
user_id: Match[str] = AlconnaMatch("user_id"),
@ -172,8 +172,8 @@ async def GetTargeter(
if not subcommand:
await matcher.finish("内部错误:无法解析子命令。")
if schedule_ids.available:
return scheduler_manager.target(id__in=schedule_ids.result)
if schedule_id.available:
return scheduler_manager.target(id=schedule_id.result)
all_enabled = arp.query(f"{subcommand}.all.value", False)
global_flag = arp.query(f"{subcommand}.global.value", False)

View File

@ -63,7 +63,6 @@ async def handle_set(
tag_name: Match[str] = AlconnaMatch("tag_name"),
jitter: Match[int] = AlconnaMatch("jitter_seconds"),
spread: Match[int] = AlconnaMatch("spread_seconds"),
interval: Match[int] = AlconnaMatch("interval_seconds"),
job_name: Match[str] = AlconnaMatch("job_name"),
bot_id_to_operate: str = Depends(GetBotId),
trigger_info: tuple[str, dict] = Depends(GetTriggerInfo),
@ -75,7 +74,6 @@ async def handle_set(
p_name = plugin_name.result
jitter_val: int | None = jitter.result if jitter.available else None
spread_val: int | None = spread.result if spread.available else None
interval_val: int | None = interval.result if interval.available else None
is_multi_target = (
len(target_groups) > 1
@ -102,14 +100,6 @@ async def handle_set(
"SchedulerManager", "DEFAULT_SPREAD_SECONDS"
)
if interval_val is None:
if task_meta and task_meta.get("default_interval") is not None:
interval_val = cast(int | None, task_meta["default_interval"])
else:
interval_val = Config.get_config(
"SchedulerManager", "DEFAULT_INTERVAL_SECONDS"
)
result_message = await scheduler_admin_service.set_schedule(
targets=target_groups,
creator_permission_level=creator_permission_level,
@ -121,7 +111,6 @@ async def handle_set(
job_name=job_name.result if job_name.available else None,
jitter=jitter_val,
spread=spread_val,
interval=interval_val,
created_by=session.user.id,
)
await MessageUtils.build_message(result_message).send()

View File

@ -46,8 +46,7 @@ class ScheduledJob(Model):
consecutive_failures = fields.IntField(default=0, description="连续失败次数")
execution_options = fields.JSONField(
null=True,
description="任务执行的额外选项 (例如: jitter, spread, "
"interval, concurrency_policy)",
description="任务执行的额外选项 (例如: jitter, spread, concurrency_policy)",
)
create_time = fields.DatetimeField(auto_now_add=True)

View File

@ -412,44 +412,16 @@ async def _execute_job(
if isinstance(schedule.execution_options, dict)
else {}
)
interval_seconds = spread_config.get("interval")
spread_seconds = spread_config.get("spread", 1.0)
if interval_seconds is not None and interval_seconds > 0:
logger.debug(
f"任务 {schedule.id}: 使用串行模式执行 {len(resolved_targets)} "
f"个目标,固定间隔 {interval_seconds} 秒。"
)
for i, target_id in enumerate(resolved_targets):
if i > 0:
logger.debug(
f"任务 {schedule.id} 目标 [{target_id or '全局'}]: "
f"等待 {interval_seconds} 秒后执行。"
)
await asyncio.sleep(interval_seconds)
async def worker(target_id: str | None):
await asyncio.sleep(random.uniform(0.1, spread_seconds))
async with semaphore:
await _execute_single_job_instance(schedule, bot, group_id=target_id)
else:
spread_seconds = spread_config.get("spread", 1.0)
logger.debug(
f"任务 {schedule.id}: 将在 {spread_seconds:.2f} 秒内分散执行 "
f"{len(resolved_targets)} 个目标。"
)
async def worker(target_id: str | None):
delay = random.uniform(0.1, spread_seconds)
logger.debug(
f"任务 {schedule.id} 目标 [{target_id or '全局'}]: "
f"随机延迟 {delay:.2f} 秒后执行。"
)
await asyncio.sleep(delay)
async with semaphore:
await _execute_single_job_instance(
schedule, bot, group_id=target_id
)
tasks_to_run = [worker(target_id) for target_id in resolved_targets]
if tasks_to_run:
await asyncio.gather(*tasks_to_run, return_exceptions=True)
tasks_to_run = [worker(target_id) for target_id in resolved_targets]
if tasks_to_run:
await asyncio.gather(*tasks_to_run, return_exceptions=True)
schedule.last_run_at = datetime.now()
schedule.last_run_status = "SUCCESS"

View File

@ -109,7 +109,6 @@ class SchedulerManager:
policy: ExecutionPolicy | None = None,
default_jitter: int | None = None,
default_spread: int | None = None,
default_interval: int | None = None,
):
"""
声明式定时任务的统一装饰器
@ -141,7 +140,6 @@ class SchedulerManager:
"model": params_model,
"default_jitter": default_jitter,
"default_spread": default_spread,
"default_interval": default_interval,
}
job_kwargs = model_dump(default_params) if default_params else {}
@ -207,7 +205,6 @@ class SchedulerManager:
default_permission: int = 5,
default_jitter: int | None = None,
default_spread: int | None = None,
default_interval: int | None = None,
) -> Callable:
"""
注册可调度的任务函数
@ -223,7 +220,6 @@ class SchedulerManager:
"default_permission": default_permission,
"default_jitter": default_jitter,
"default_spread": default_spread,
"default_interval": default_interval,
}
model_name = params_model.__name__ if params_model else ""
logger.debug(

View File

@ -84,12 +84,7 @@ class ExecutionOptions(BaseModel):
"""
jitter: int | None = Field(None, description="触发时间抖动(秒)")
spread: int | None = Field(
None, description="(并发模式)多目标执行的最大分散延迟(秒)"
)
interval: int | None = Field(
None, description="多目标执行的固定间隔(秒),设置后将强制串行执行"
)
spread: int | None = Field(None, description="多目标执行的分散延迟(秒)")
concurrency_policy: Literal["ALLOW", "SKIP", "QUEUE"] = Field(
"ALLOW", description="并发策略"
)