新增定时任务

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
+4
View File
@@ -14,6 +14,7 @@ from loguru import logger
from .config import settings
from .database import engine
from .scheduler import start_scheduler, stop_scheduler
from .routers import register, resumes, sessions, jobs, blacklist, rpa, templates, ai
@@ -25,7 +26,10 @@ async def lifespan(app: FastAPI):
logger.success("数据库连接成功")
except Exception as e:
logger.error(f"数据库连接失败,请检查配置及数据库是否已初始化:{e}")
# 定时任务调度器:后台循环扫描 rpa_schedule,到点自动下发 RpaTask
sched_task = start_scheduler()
yield
await stop_scheduler(sched_task)
app = FastAPI(
+27
View File
@@ -212,6 +212,33 @@ class RpaTask(Base):
user = relationship("User")
class RpaSchedule(Base):
"""
定时任务排程(远程控制页创建,服务端调度器每日到点自动下发 RpaTask)。
kind:apply=自动投递 / activate=Boss 激活;时间均为北京时间 HH:MM。
防重复:last_start_date / last_stop_date 记录当日已下发日期,每类动作每天只触发一次。
"""
__tablename__ = "rpa_schedule"
id = Column(Integer, primary_key=True, autoincrement=True)
user_id = Column(Integer, ForeignKey("users.id", ondelete="CASCADE"),
nullable=False, index=True)
# apply / activate
kind = Column(String(20), nullable=False)
# 任务参数 JSON:apply=投递条件;activate=激活参数(match_text/status_filter/threshold/reply_message/max_count/dry_run)
params = Column(Text, nullable=True)
# 每日开始/结束时间 HH:MM(北京时间),结束时间到点自动下发对应 stop 任务
start_time = Column(String(5), nullable=False)
end_time = Column(String(5), nullable=False)
enabled = Column(Boolean, default=True, nullable=False)
# 最近一次开始/结束下发日期(北京日期 YYYY-MM-DD,防当日重复触发)
last_start_date = Column(String(10), nullable=True)
last_stop_date = Column(String(10), nullable=True)
# 最近一次调度结果说明(已下发任务#ID / 跳过原因)
last_result = Column(String(255), nullable=True)
created_at = Column(DateTime, default=utcnow, nullable=False)
class ApplyTemplate(Base):
"""
投递条件模板
+117 -5
View File
@@ -1,11 +1,15 @@
"""
远程控制路由:云端下发开始/结束投递任务,客户端轮询执行并回调。
远程控制路由:云端下发开始/结束任务,客户端轮询执行并回调。
- 状态快照 rpa_status:心跳携带全量状态上报,每用户一行(单设备)
- 任务 rpa_task:pending → running → done/failed,pending 超时 → expired
(start_apply / stop_apply 可由控制端手动下发;start_activate / stop_activate
仅由定时调度器下发)
- 去重(服务端权威拦截):同类型任务未达终态时拒绝重复下发
- 定时排程 rpa_schedule:每日到点由 scheduler 自动下发对应任务
"""
import json
import re
from datetime import datetime, timedelta
from fastapi import APIRouter, Depends, HTTPException, Query
@@ -13,16 +17,17 @@ from sqlalchemy.orm import Session
from ..auth import get_current_user
from ..database import get_db
from ..models import User, RpaStatus, RpaTask
from ..models import User, RpaStatus, RpaTask, RpaSchedule
from ..online import is_online
from ..schemas import RpaTaskIn, RpaTaskFinishIn, Message
from ..scheduler import KIND_TASK_TYPES, PENDING_EXPIRE_MINUTES
from ..schemas import RpaTaskIn, RpaTaskFinishIn, RpaScheduleIn, Message
router = APIRouter(prefix="/api/rpa", tags=["远程控制"])
# 任务终态:达到终态后同类型任务才允许再次下发
TERMINAL_STATUS = ("done", "failed", "expired")
# pending 超过该时长未客户端认领则过期,防止离线恢复后执行过时任务
PENDING_EXPIRE_MINUTES = 10
# 定时排程时间格式:HH:MM(零填充 24 小时制)
_TIME_RE = re.compile(r"^([01]\d|2[0-3]):[0-5]\d$")
def _task_dict(t: RpaTask) -> dict:
@@ -165,3 +170,110 @@ def finish_task(
task.result = (req.result or "")[:500]
db.commit()
return Message(msg="ok")
# =======================================================
# 定时任务排程(每日到点由调度器自动下发 RpaTask)
# =======================================================
def _schedule_dict(s: RpaSchedule) -> dict:
return {
"id": s.id,
"kind": s.kind,
"params": json.loads(s.params) if s.params else None,
"start_time": s.start_time,
"end_time": s.end_time,
"enabled": bool(s.enabled),
"last_start_date": s.last_start_date,
"last_stop_date": s.last_stop_date,
"last_result": s.last_result,
"created_at": (s.created_at + timedelta(hours=8)).strftime("%Y-%m-%d %H:%M:%S"),
}
@router.get("/schedules", response_model=dict)
def list_schedules(
current_user: User = Depends(get_current_user),
db: Session = Depends(get_db),
):
"""当前用户的定时排程列表(倒序)"""
rows = db.query(RpaSchedule).filter(RpaSchedule.user_id == current_user.id) \
.order_by(RpaSchedule.id.desc()).all()
return {"items": [_schedule_dict(s) for s in rows]}
@router.post("/schedules", response_model=dict)
def create_schedule(
req: RpaScheduleIn,
current_user: User = Depends(get_current_user),
db: Session = Depends(get_db),
):
"""创建定时排程。不做客户端在线预检(定时本就是为无人值守场景,
下发时若离线由 pending 过期机制兑底)"""
if req.kind not in KIND_TASK_TYPES:
raise HTTPException(status_code=422, detail="任务类型只能为 apply(自动投递)或 activate(Boss激活)")
if not _TIME_RE.match(req.start_time or "") or not _TIME_RE.match(req.end_time or ""):
raise HTTPException(status_code=422, detail="时间格式应为 HH:MM(24 小时制)")
if req.end_time <= req.start_time:
raise HTTPException(status_code=422, detail="结束时间必须晚于开始时间")
params = req.params or {}
if req.kind == "apply":
if not params.get("keywords") or not params.get("city"):
raise HTTPException(status_code=422, detail="定时投递必须提供 keywords 与 city")
else: # activate
if not (params.get("match_text") or "").strip():
raise HTTPException(status_code=422, detail="定时激活必须提供匹配条件文本 match_text")
if not (params.get("reply_message") or "").strip():
raise HTTPException(status_code=422, detail="定时激活必须提供激活消息 reply_message")
if params.get("status_filter") not in (None, "unread", "read", "all"):
raise HTTPException(status_code=422, detail="消息状态过滤取值非法")
sched = RpaSchedule(
user_id=current_user.id,
kind=req.kind,
params=json.dumps(params, ensure_ascii=False),
start_time=req.start_time,
end_time=req.end_time,
enabled=True,
)
db.add(sched)
db.commit()
db.refresh(sched)
return _schedule_dict(sched)
@router.put("/schedules/{schedule_id}", response_model=dict)
def toggle_schedule(
schedule_id: int,
enabled: bool = Query(..., description="启用/停用"),
current_user: User = Depends(get_current_user),
db: Session = Depends(get_db),
):
"""启用/停用定时排程"""
sched = db.query(RpaSchedule).filter(
RpaSchedule.id == schedule_id,
RpaSchedule.user_id == current_user.id,
).first()
if not sched:
raise HTTPException(status_code=404, detail="排程不存在")
sched.enabled = enabled
db.commit()
return _schedule_dict(sched)
@router.delete("/schedules/{schedule_id}", response_model=Message)
def delete_schedule(
schedule_id: int,
current_user: User = Depends(get_current_user),
db: Session = Depends(get_db),
):
"""删除定时排程(仅停止后续调度,不影响已下发的任务)"""
sched = db.query(RpaSchedule).filter(
RpaSchedule.id == schedule_id,
RpaSchedule.user_id == current_user.id,
).first()
if not sched:
raise HTTPException(status_code=404, detail="排程不存在")
db.delete(sched)
db.commit()
return Message(msg="已删除")
+142
View File
@@ -0,0 +1,142 @@
"""
定时任务调度器:后台循环扫描 rpa_schedule 表,每日到点自动下发 RpaTask,
客户端通过现有 /api/rpa/tasks/pending 轮询认领执行(认领机制零改动)。
- 每 SCAN_INTERVAL 秒扫描一轮;排程时间均为北京时间 HH:MM
- 每个排程每天只下发一次开始动作、一次结束动作
(last_start_date / last_stop_date 记录北京日期防重复)
- 下发前复用 RpaTask 去重规则:同类型任务未达终态则跳过本轮,
不消耗当日次数,下轮重试
- 客户端离线时照常下发:任务 pending 超过 10 分钟无人认领,
由 fetch_pending_task 的现有过期机制自动 expired
"""
import asyncio
import json
from datetime import datetime, timedelta
from loguru import logger
from sqlalchemy.orm import Session
from .database import SessionLocal
from .models import RpaSchedule, RpaTask
SCAN_INTERVAL = 30 # 秒
# pending 超过该时长未客户端认领则过期(与 routers/rpa.py 共用同一常量)
PENDING_EXPIRE_MINUTES = 10
# kind → (开始任务类型, 结束任务类型)
KIND_TASK_TYPES = {
"apply": ("start_apply", "stop_apply"),
"activate": ("start_activate", "stop_activate"),
}
def _beijing_now() -> datetime:
return datetime.utcnow() + timedelta(hours=8)
def _due(last_date: str | None, hhmm: str, now_cn: datetime) -> bool:
"""到点判断:当日未触发过 且 当前 HH:MM 已到(零填充字符串可按字典序比较)"""
return last_date != now_cn.strftime("%Y-%m-%d") and now_cn.strftime("%H:%M") >= hhmm
def _dispatch(db: Session, sched: RpaSchedule, start: bool, now_cn: datetime) -> None:
"""下发一次开始/结束任务,并更新防重复日期"""
task_type = KIND_TASK_TYPES[sched.kind][0 if start else 1]
action = "开始" if start else "结束"
dup = db.query(RpaTask).filter(
RpaTask.user_id == sched.user_id,
RpaTask.task_type == task_type,
RpaTask.status.in_(("pending", "running")),
).first()
if dup:
# 同类型任务未终态:不消耗当日次数,下一轮重试
sched.last_result = f"定时{action}跳过:同类型任务 #{dup.id} 未结束"
db.commit()
return
task = RpaTask(
user_id=sched.user_id,
task_type=task_type,
# 只有开始任务携带参数;stop 任务无需 params
params=json.dumps(json.loads(sched.params) if sched.params else {},
ensure_ascii=False) if start else None,
)
db.add(task)
db.flush() # 先取 task.id 再写排程状态
today = now_cn.strftime("%Y-%m-%d")
if start:
sched.last_start_date = today
else:
sched.last_stop_date = today
sched.last_result = f"定时{action}已下发:任务 #{task.id}({task_type})"
db.commit()
logger.info(f"定时任务排程 #{sched.id}({sched.kind}) {action}已下发 -> RpaTask #{task.id} {task_type}")
def _expire_stale(db: Session) -> None:
"""pending 过期清理兜底:客户端长期离线不轮询时,fetch_pending_task 的清理
永远不会被触发,残留 pending 会永久阻塞后续调度——由调度器每轮代做"""
cutoff = datetime.utcnow() - timedelta(minutes=PENDING_EXPIRE_MINUTES)
stale = db.query(RpaTask).filter(
RpaTask.status == "pending",
RpaTask.created_at < cutoff,
).all()
for t in stale:
t.status = "expired"
t.result = f"超过 {PENDING_EXPIRE_MINUTES} 分钟未执行,自动过期"
if stale:
db.commit()
logger.info(f"调度器清理超时未认领任务 {len(stale)} 条")
def tick(db: Session | None = None) -> None:
"""单轮扫描。db 可注入(测试用),缺省自建会话;异常只记日志不中断循环"""
own = db is None
if own:
db = SessionLocal()
try:
_expire_stale(db)
now_cn = _beijing_now()
rows = db.query(RpaSchedule).filter(RpaSchedule.enabled.is_(True)).all()
for s in rows:
if s.kind not in KIND_TASK_TYPES:
logger.warning(f"定时任务排程 #{s.id} 类型未知({s.kind}),跳过")
continue
if _due(s.last_start_date, s.start_time, now_cn):
_dispatch(db, s, start=True, now_cn=now_cn)
if _due(s.last_stop_date, s.end_time, now_cn):
_dispatch(db, s, start=False, now_cn=now_cn)
except Exception as e:
db.rollback()
logger.error(f"定时任务扫描异常:{e}")
finally:
if own:
db.close()
async def _loop() -> None:
while True:
try:
# 同步 DB 操作丢线程池,避免阻塞事件循环
await asyncio.to_thread(tick)
except Exception as e:
logger.error(f"定时任务调度循环异常:{e}")
await asyncio.sleep(SCAN_INTERVAL)
def start_scheduler() -> asyncio.Task:
"""lifespan 内启动后台调度循环,返回 Task 供关闭时取消"""
logger.info(f"定时任务调度器已启动(每 {SCAN_INTERVAL} 秒扫描)")
return asyncio.create_task(_loop())
async def stop_scheduler(task: asyncio.Task | None) -> None:
if task is None:
return
task.cancel()
try:
await task
except asyncio.CancelledError:
pass
+8
View File
@@ -229,6 +229,14 @@ class RpaTaskIn(BaseModel):
params: Optional[dict] = None
class RpaScheduleIn(BaseModel):
"""定时任务排程:每日 start_time 下发开始任务、end_time 下发结束任务(北京时间)"""
kind: str = Field(..., description="apply=自动投递 / activate=Boss激活")
start_time: str = Field(..., description="每日开始时间 HH:MM")
end_time: str = Field(..., description="每日结束时间 HH:MM")
params: Optional[dict] = None
class RpaTaskFinishIn(BaseModel):
status: str = Field(..., description="done / failed")
result: Optional[str] = None
+124 -1
View File
@@ -243,7 +243,46 @@
</div>
<div class="bl-tip">任务下发后客户端 5 秒内认领执行;同类型任务未结束前不能重复下发。</div>
<!-- 定时任务(每日自动:到点下发开始,到结束时间自动停止) -->
<div style="font-size:14px;font-weight:600;color:#1890ff;margin:18px 4px 10px;">定时任务(每日自动)</div>
<div class="bl-add" style="flex-wrap:wrap;align-items:flex-end;">
<div style="min-width:130px;"><label style="font-size:12px;color:#666;">任务类型</label>
<select id="schKind" style="width:100%;padding:9px 10px;border:1px solid #d9d9d9;border-radius:6px;">
<option value="apply">自动投递</option>
<option value="activate">Boss 激活</option>
</select></div>
<div style="min-width:120px;"><label style="font-size:12px;color:#666;">每日开始时间</label>
<input type="time" id="schStart" value="09:00" style="width:100%;padding:8px 10px;border:1px solid #d9d9d9;border-radius:6px;"></div>
<div style="min-width:120px;"><label style="font-size:12px;color:#666;">每日结束时间</label>
<input type="time" id="schEnd" value="18:00" style="width:100%;padding:8px 10px;border:1px solid #d9d9d9;border-radius:6px;"></div>
<button id="btnSchAdd" style="background:#1890ff;">添加排程</button>
</div>
<!-- Boss 激活专属参数(任务类型=激活时显示;自动投递使用上方投递表单的条件) -->
<div class="bl-add hidden" id="schActParams" style="flex-wrap:wrap;align-items:flex-end;">
<div style="flex:2;min-width:220px;"><label style="font-size:12px;color:#666;">匹配条件文本</label>
<input type="text" id="schMatch" placeholder="与对方最后一条消息比对,如:Python开发" style="width:100%;padding:9px 10px;border:1px solid #d9d9d9;border-radius:6px;"></div>
<div style="min-width:110px;"><label style="font-size:12px;color:#666;">消息状态</label>
<select id="schStatus" style="width:100%;padding:9px 10px;border:1px solid #d9d9d9;border-radius:6px;">
<option value="unread" selected>仅未读</option>
<option value="read">仅已读</option>
<option value="all">不限</option>
</select></div>
<div style="width:100px;"><label style="font-size:12px;color:#666;">匹配阈值</label>
<input type="number" id="schThreshold" value="80" min="0" max="100" style="width:100%;padding:9px 10px;border:1px solid #d9d9d9;border-radius:6px;"></div>
<div style="flex:2;min-width:220px;"><label style="font-size:12px;color:#666;">激活消息</label>
<input type="text" id="schReply" placeholder="命中后发送的消息内容" style="width:100%;padding:9px 10px;border:1px solid #d9d9d9;border-radius:6px;"></div>
<div style="width:130px;"><label style="font-size:12px;color:#666;">发送上限(0=不限)</label>
<input type="number" id="schMax" value="20" min="0" style="width:100%;padding:9px 10px;border:1px solid #d9d9d9;border-radius:6px;"></div>
<label style="display:flex;align-items:center;gap:4px;font-size:13px;padding-bottom:10px;"><input type="checkbox" id="schDry"> 试跑(只识别不发送)</label>
</div>
<div class="bl-tip">自动投递使用上方投递表单的条件;结束时间到点自动下发停止任务;客户端离线时任务 10 分钟未认领自动过期;每条排程每天各触发一次开始/结束。</div>
<table>
<thead><tr><th style="width:50px">#</th><th style="width:90px">类型</th><th style="width:90px">每日开始</th><th style="width:90px">每日结束</th><th style="width:70px">状态</th><th>最近调度</th><th style="width:130px">操作</th></tr></thead>
<tbody id="schTbody"></tbody>
</table>
<div class="empty hidden" id="schEmpty">暂无定时任务</div>
<table style="margin-top:18px;">
<thead><tr><th style="width:150px">时间</th><th style="width:100px">类型</th><th style="width:90px">状态</th><th>结果</th></tr></thead>
<tbody id="taskTbody"></tbody>
</table>
@@ -434,7 +473,7 @@ async function removeBlacklist(id) {
}
/* ---------- 远程控制 ---------- */
const TASK_TXT = { start_apply: '开始投递', stop_apply: '结束投递' };
const TASK_TXT = { start_apply: '开始投递', stop_apply: '结束投递', start_activate: '开始激活', stop_activate: '停止激活' };
const TASK_STATUS_TXT = { pending: '待认领', running: '执行中', done: '已完成', failed: '失败', expired: '已过期' };
async function loadRemote() {
@@ -463,6 +502,67 @@ async function loadRemote() {
).join('');
document.getElementById('taskEmpty').classList.toggle('hidden', items.length > 0);
} catch (e) { /* ignore */ }
loadSchedules();
}
/* ---------- 定时任务排程 ---------- */
const SCH_KIND_TXT = { apply: '自动投递', activate: 'Boss 激活' };
async function loadSchedules() {
try {
const d = await api('/api/rpa/schedules');
const items = d.items || [];
document.getElementById('schTbody').innerHTML = items.map(s =>
'<tr><td>' + s.id + '</td><td>' + (SCH_KIND_TXT[s.kind] || esc(s.kind)) + '</td><td>' + esc(s.start_time) +
'</td><td>' + esc(s.end_time) + '</td><td>' +
(s.enabled ? '<span style="color:#52c41a;">启用</span>' : '<span style="color:#999;">停用</span>') +
'</td><td class="remark" title="' + esc(s.last_result || '') + '">' + esc(s.last_result || '-') +
'</td><td><button class="bl-remove sch-toggle" data-id="' + s.id + '" data-enabled="' + (s.enabled ? 0 : 1) + '">' +
(s.enabled ? '停用' : '启用') + '</button> <button class="bl-remove sch-del" data-id="' + s.id + '" style="color:#e64340;">删除</button></td></tr>'
).join('');
document.getElementById('schEmpty').classList.toggle('hidden', items.length > 0);
} catch (e) { /* 401 已在 api 内处理 */ }
}
async function addSchedule() {
const kind = document.getElementById('schKind').value;
const startTime = document.getElementById('schStart').value;
const endTime = document.getElementById('schEnd').value;
if (!startTime || !endTime) { alert('请选择开始/结束时间'); return; }
let params;
if (kind === 'apply') {
// 复用页面上方投递表单的条件(与手动开始投递同一套参数)
const keywords = (document.getElementById('rKeywords').value || '').split(/[,,\s]+/).filter(Boolean);
if (!keywords.length) { alert('自动投递需先在上方表单填写搜索关键词'); return; }
params = {
keywords,
city: document.getElementById('rCity').value,
min_score: parseInt(document.getElementById('rScore').value, 10) || 65,
salary_min: parseInt(document.getElementById('rSalaryMin').value, 10) || 0,
salary_max: parseInt(document.getElementById('rSalaryMax').value, 10) || 0,
daily_limit: parseInt(document.getElementById('rLimit').value, 10) || 0,
boss_online_only: document.getElementById('rOnlineOnly').checked,
allow_headhunter: document.getElementById('rHunter').checked,
prompt: document.getElementById('rPrompt').value.trim() || null,
};
} else {
params = {
match_text: document.getElementById('schMatch').value.trim(),
status_filter: document.getElementById('schStatus').value,
threshold: parseInt(document.getElementById('schThreshold').value, 10) || 80,
reply_message: document.getElementById('schReply').value.trim(),
max_count: parseInt(document.getElementById('schMax').value, 10) || 0,
dry_run: document.getElementById('schDry').checked,
};
if (!params.match_text) { alert('Boss 激活需填写匹配条件文本'); return; }
if (!params.reply_message) { alert('Boss 激活需填写激活消息'); return; }
}
try {
await api('/api/rpa/schedules', { method: 'POST', body: JSON.stringify({
kind, start_time: startTime, end_time: endTime, params,
})});
await loadSchedules();
} catch (e) { alert('添加失败:' + (e.message || '')); }
}
async function sendTask(taskType, params) {
@@ -612,6 +712,29 @@ document.getElementById('btnStopRemote').onclick = () => {
};
document.getElementById('btnAiSave').onclick = saveAiConfig;
document.getElementById('btnAiTest').onclick = testAiConfig;
// 定时任务:类型切换显隐激活参数区 + 添加/启停/删除
document.getElementById('schKind').addEventListener('change', () => {
document.getElementById('schActParams').classList.toggle('hidden',
document.getElementById('schKind').value !== 'activate');
});
document.getElementById('btnSchAdd').onclick = addSchedule;
document.getElementById('schTbody').addEventListener('click', async e => {
const tog = e.target.closest('.sch-toggle');
if (tog) {
try {
await api('/api/rpa/schedules/' + tog.dataset.id + '?enabled=' + tog.dataset.enabled, { method: 'PUT' });
await loadSchedules();
} catch (err) { alert('操作失败:' + (err.message || '')); }
return;
}
const del = e.target.closest('.sch-del');
if (del && confirm('确定删除该定时任务?删除后不再自动调度。')) {
try {
await api('/api/rpa/schedules/' + del.dataset.id, { method: 'DELETE' });
await loadSchedules();
} catch (err) { alert('删除失败:' + (err.message || '')); }
}
});
document.getElementById('blTbody').addEventListener('click', e => {
const btn = e.target.closest('.bl-remove');
if (btn) removeBlacklist(btn.dataset.id);