新增定时任务

This commit is contained in:
2026-09-03 15:47:50 +08:00
parent a9752b01a9
commit 7cdfd76bfb
10 changed files with 516 additions and 25 deletions
+46 -18
View File
@@ -139,6 +139,17 @@ class AppState:
status, result = "done", "停止指令已下达"
else:
status, result = "done", "当前没有运行中的投递任务"
elif ttype == "start_activate":
# 定时调度器下发的 Boss 激活任务(与面板 /api/start-activate 同一入口)
res = start_activate_shared(task.get("params") or {})
status, result = ("done", res["msg"]) if res["ok"] else ("failed", res["msg"])
elif ttype == "stop_activate":
activator = self.ensure_activator()
if activator.is_running:
activator.stop()
status, result = "done", "停止指令已下达"
else:
status, result = "done", "当前没有运行中的激活任务"
else:
status, result = "failed", f"未知任务类型:{ttype}"
except Exception as e:
@@ -509,37 +520,54 @@ def stop_apply():
# =======================================================
# Boss 激活(单机功能,不依赖云端账号)
# Boss 激活(单机功能,不依赖云端账号;也支持定时调度器远程下发)
# =======================================================
@app.post("/api/start-activate")
def start_activate(req: StartActivateReq):
if not req.match_text.strip():
raise HTTPException(status_code=400, detail="请填写匹配条件文本")
if req.status_filter not in ("unread", "read", "all"):
raise HTTPException(status_code=400, detail="消息状态过滤取值非法")
if not req.reply_message.strip():
raise HTTPException(status_code=400, detail="请填写激活消息内容")
def start_activate_shared(params: dict) -> dict:
"""面板 / 远程定时任务共用的激活启动入口(返回 ok+msg,不抛业务异常)"""
match_text = (params.get("match_text") or "").strip()
if not match_text:
return {"ok": False, "msg": "缺少匹配条件文本 match_text"}
reply_message = (params.get("reply_message") or "").strip()
if not reply_message:
return {"ok": False, "msg": "缺少激活消息 reply_message"}
status_filter = params.get("status_filter") or "unread"
if status_filter not in ("unread", "read", "all"):
return {"ok": False, "msg": f"消息状态过滤取值非法:{status_filter}"}
try:
threshold = max(0, min(100, int(params.get("threshold") or 80)))
max_count = max(0, int(params.get("max_count") or 0))
except (TypeError, ValueError):
return {"ok": False, "msg": "threshold / max_count 参数非法"}
if state.applicant and state.applicant.is_running:
raise HTTPException(status_code=400, detail="投递任务运行中,请先停止")
return {"ok": False, "msg": "投递任务运行中,请先停止"}
activator = state.ensure_activator()
if activator.is_running:
raise HTTPException(status_code=400, detail="已有激活任务在运行")
return {"ok": False, "msg": "已有激活任务在运行"}
try:
# 复用登录态 profile,浏览器被手动关闭时自动拉起
state.browser.ensure_browser_started()
except RuntimeError as e:
raise HTTPException(status_code=400, detail=str(e))
return {"ok": False, "msg": str(e)}
activator.start(
match_text=req.match_text,
status_filter=req.status_filter,
threshold=req.threshold / 100.0,
reply_message=req.reply_message,
max_count=req.max_count,
dry_run=req.dry_run,
match_text=match_text,
status_filter=status_filter,
threshold=threshold / 100.0,
reply_message=reply_message,
max_count=max_count,
dry_run=bool(params.get("dry_run")),
)
return {"ok": True, "msg": "Boss 激活任务已启动"}
@app.post("/api/start-activate")
def start_activate(req: StartActivateReq):
# Pydantic 已校验字段类型,业务校验统一在 shared 入口(与远程定时任务同源)
res = start_activate_shared(req.model_dump())
if not res["ok"]:
raise HTTPException(status_code=400, detail=res["msg"])
return {"ok": True, "msg": res["msg"]}
@app.post("/api/stop-activate")
def stop_activate():
activator = state.ensure_activator()