合入 W1-c 数据事件触发:定时任务加「事件触发」(上传表格出新一期、上游工作流正式运行成功结束、数据源有要处理的变化),事件在发生处写进待派发事件、调度循环去抖派发;保存时与运行时防环;{{ event.* }} 模板变量;途经驳回、应用补算的缺省不认(可勾选);积压一拍处理完、过期的合成一条跳过;按期次分窗口;定时页可选「定时 / 事件触发」

This commit is contained in:
ThearchyHelios
2026-10-18 18:23:33 +08:00
29 changed files with 3924 additions and 119 deletions
+304 -26
View File
@@ -8,6 +8,12 @@ eval_suite(跑一次评测题集)或 source_maintenance(维护一个数据
结束、运行失败、等待审批)说的是这一次发起的那次运行,非工作流目标没有那一次运行,配上了也收不到,所以照实拒收。接口返回带
target_kind、target_id、target_name(目标现在的名字,不在了用保存时记下的那一份)。
**触发方式**(docs/design/orchestration-round/00-总方案.md §4,一套定时机制):trigger_kind 是 cron(缺省,字段和行为一字不变)
或 event(由数据事件触发,规则在 scheduler/triggers.py、派发在 scheduler/outbox.py)。event 时请求体写 trigger(事件种类、对象、
过滤、去抖),目标只能是工作流,cron 存空串、下一次触发为空;核对对象、输入模板里的 {{ event.* }}、workflow.finished 串成的环
(A→B→A 拒收,说清是哪几个)都在保存时做。触发方式建好之后不能改。Webhook 照旧可配。接口返回另带 trigger_kind、trigger、
trigger_text(给人看的触发说明,事件触发时也是 description_text)、trigger_state(几条事件在等、去抖到什么时候、调度开没开)。
形状按前后端约定(Schedule、Fire 两种对象,见 _schedule_out、_fire_outs)。Webhook 地址和签名密钥加密存放,接口
只给主机和末 4 位(url_hint)、有没有密钥(has_secret)。请求体自己解析、自己校验:错误一律回中文 detail,表单
据 field 把报错落到那一格(422 {detail, code, field},同 api/coded.py)。后台的调度循环在 app/scheduler 里。
@@ -27,12 +33,12 @@ from app.api.runs import actor_of
from app.core.crypto import decrypt, encrypt
from app.db.base import get_session, utcnow
from app.db.models import Run, Schedule, ScheduleFire, WebhookDelivery, Workflow, WorkflowVersion
from app.scheduler import auto_compare, cron, service, webhook
from app.scheduler import auto_compare, cron, outbox, service, triggers, webhook
router = APIRouter(prefix="/api/schedules", tags=["schedules"])
#: 请求不合规(422)的机器码。field 是出错的那一项:name、target_kind、target_id、workflow_id、cron、timezone、run_class、
#: input_template、enabled、overlap、misfire_grace_s、webhook、version、limit、before
#: input_template、enabled、overlap、misfire_grace_s、webhook、version、limit、before、trigger_kind、trigger、debounce_s
SCHEDULE_INVALID = "schedule_invalid"
#: 非工作流目标配了 Webhook(见模块说明)
NO_WEBHOOK = ("目标是「{kind}」的定时任务不推 Webhook:Webhook 的事件说的是定时发起的那次运行。要收这类定时的通知,"
@@ -155,13 +161,45 @@ def _run_class(value: Any) -> str:
return value
def _template(value: Any) -> dict[str, str]:
def _template(value: Any, event: str | None = None) -> dict[str, str]:
try:
return service.check_template(value)
return service.check_template(value, event=event)
except service.TemplateError as e:
raise _invalid(str(e), "input_template") from e
#: 触发方式:按 cron 到点、由数据事件触发
TRIGGER_KINDS = ("cron", "event")
def _trigger_kind(value: Any) -> str:
if value not in TRIGGER_KINDS:
raise _invalid(f"触发方式需要是定时或事件触发{_current(value)}", "trigger_kind")
return value
def _trigger(value: Any) -> dict[str, Any]:
try:
return triggers.normalize(value)
except triggers.TriggerError as e:
raise _invalid(str(e), e.field) from e
async def _check_trigger(session: AsyncSession, trigger: dict[str, Any], *, schedule_id: str | None,
target_workflow_id: str) -> triggers.Checked:
"""核对触发条件里的对象,再查 workflow.finished 串没串成环。不对抛 422(field=trigger)。"""
try:
checked = await triggers.check(session, trigger)
except triggers.TriggerError as e:
raise _invalid(str(e), e.field) from e
cycle = await triggers.find_cycle(session, schedule_id=schedule_id, trigger=trigger,
target_workflow_id=target_workflow_id)
if cycle:
names = dict((await session.execute(select(Workflow.id, Workflow.name).where(Workflow.id.in_(set(cycle))))).all())
raise _invalid(triggers.cycle_text([names.get(w, w) for w in cycle]), "trigger")
return checked
def _current(value: Any) -> str:
return service.current_value(value)
@@ -328,6 +366,37 @@ def _description(row: Schedule) -> str:
return f"按 cron 表达式 {row.cron}({row.timezone})"
def _event_kind(row: Schedule) -> bool:
return (getattr(row, "trigger_kind", None) or "cron") == "event"
async def _trigger_names(session: AsyncSession, rows: list[Schedule]) -> dict[str, str]:
"""事件触发任务的对象、过滤里的应用现在叫什么:{id: 名字}。不在了的不在里面(说法里用保存时的名字)。"""
from app.db.models import App, DataSource
wanted: dict[str, set[str]] = {"source": set(), "workflow": set(), "app": set()}
for r in rows:
trigger = r.trigger if _event_kind(r) and isinstance(r.trigger, dict) else None
if not trigger:
continue
subject = triggers.subject_of(trigger)
wanted["workflow" if trigger.get("event") == "workflow.finished" else "source"].add(subject)
if trigger.get("app_id"):
wanted["app"].add(trigger["app_id"])
out: dict[str, str] = {}
for model, ids in ((DataSource, wanted["source"]), (Workflow, wanted["workflow"]), (App, wanted["app"])):
if ids:
out |= dict((await session.execute(select(model.id, model.name).where(model.id.in_(ids)))).all())
return out
def _trigger_text(row: Schedule, names: dict[str, str]) -> str:
trigger = row.trigger if isinstance(row.trigger, dict) else {}
subject = triggers.subject_of(trigger)
stored = (trigger.get("subject_name") if isinstance(trigger.get("subject_name"), str) else None)
return triggers.describe(trigger, names.get(subject) or stored, app_name=names.get(trigger.get("app_id") or ""))
def _webhook_out(row: Schedule) -> dict[str, Any] | None:
if not row.webhook_url:
return None
@@ -365,6 +434,15 @@ async def _fire_outs(session: AsyncSession, fires: list[ScheduleFire]) -> list[d
raw = f.inputs if isinstance(f.inputs, dict) else {}
return {key: raw[key] for key in ("steps", "jobs", "skipped") if key in raw}
def event_of(f: ScheduleFire) -> dict[str, Any] | None:
"""事件触发的那几次(派发的、事件触发任务的立即运行):事件的说明(scheduler/outbox.EventFire.brief),没有为 null。"""
raw = f.inputs.get("event") if isinstance(f.inputs, dict) and f.schedule_id not in maintenance else None
if not isinstance(raw, dict):
return None
# windows / periods 只在几个过了宽限的窗口合成一条跳过时有:合了几个窗口、合掉的期次(上传表格)
return {key: raw.get(key) for key in ("event", "subject_id", "subject_name", "text", "merged", "first_at",
"last_at", "depth", "sample", "real", "windows", "periods")}
compared = {f.id: f.compare_artifact for f in fires if f.compare_artifact and f.run_id}
compares = await auto_compare.briefs(list(compared.values()))
stale = await auto_compare.outdated(session, compares)
@@ -405,6 +483,8 @@ async def _fire_outs(session: AsyncSession, fires: list[ScheduleFire]) -> list[d
# 目标是评测题集时这一次触发建的那次评测(没有运行 id)
"eval_run_id": f.eval_run_id,
"inputs": inputs_of(f),
# 事件触发的那几次:事件的说明(种类、对象、给人看的一句、合并了几次、触发深度、是不是立即运行的示例)
"event": event_of(f),
"reason": None if f.id in launching else f.reason,
"notified": notified.get(f.id, []),
"compare": compare_of(f),
@@ -430,6 +510,16 @@ async def _schedule_outs(session: AsyncSession, rows: list[Schedule]) -> list[di
ScheduleFire.schedule_id == newest.c.schedule_id, ScheduleFire.created_at == newest.c.at)))).scalars())
for out, fire in zip(await _fire_outs(session, fires), fires):
last.setdefault(fire.schedule_id, out)
trigger_names = await _trigger_names(session, rows) if rows else {}
states: dict[str, dict[str, Any]] = {}
if any(_event_kind(r) for r in rows):
from app import scheduler
on = scheduler.scheduler_enabled()
for r in rows:
if _event_kind(r):
states[r.id] = {**await outbox.pending_for(session, r), "scheduler_on": on}
def target(r: Schedule) -> dict[str, Any]:
kind, target_id = service.target_of(r)
name = names.get(r.workflow_id) if kind == "workflow" else targets.get(r.id)
@@ -437,10 +527,20 @@ async def _schedule_outs(session: AsyncSession, rows: list[Schedule]) -> list[di
return {"target_kind": kind, "target_id": target_id, "target_label": service.TARGET_LABEL.get(kind, kind),
"target_name": name or r.workflow_name, "target_exists": name is not None}
def trigger(r: Schedule) -> dict[str, Any]:
"""触发方式:cron 的 trigger、trigger_text、trigger_state 为 null;事件触发的给规范化后的触发条件、说法、在等的事件。"""
if not _event_kind(r):
return {"trigger_kind": "cron", "trigger": None, "trigger_text": None, "trigger_state": None}
# subject_name 是保存时记下的对象名(对象删掉之后界面写它);带回来时不认(triggers.normalize)
stored = dict(r.trigger or {}) if isinstance(r.trigger, dict) else {}
return {"trigger_kind": "event", "trigger": stored, "trigger_text": _trigger_text(r, trigger_names),
"trigger_state": states.get(r.id)}
return [{
"id": r.id,
"name": r.name,
**target(r),
**trigger(r),
"workflow_id": r.workflow_id,
# 工作流还在就给它现在的名字;删掉之后用保存时记下的那一份。非工作流目标这一项是目标的名字(同 target_name)
"workflow_name": names.get(r.workflow_id) or targets.get(r.id) or r.workflow_name,
@@ -454,7 +554,7 @@ async def _schedule_outs(session: AsyncSession, rows: list[Schedule]) -> list[di
"webhook": _webhook_out(r),
"next_run_at": _iso(r.next_run_at),
"last_fire": last.get(r.id),
"description_text": _description(r),
"description_text": _trigger_text(r, trigger_names) if _event_kind(r) else _description(r),
"created_by": r.created_by,
"created_at": _iso(r.created_at),
"updated_at": _iso(r.updated_at),
@@ -479,11 +579,54 @@ async def _get(session: AsyncSession, schedule_id: str) -> Schedule:
@router.get("/variables")
async def list_variables(tz: str | None = Query(default=None, alias="timezone")) -> list[dict[str, str]]:
async def list_variables(tz: str | None = Query(default=None, alias="timezone"), trigger_kind: str | None = None,
event: str | None = None) -> list[dict[str, str]]:
"""输入模板能用的变量:[{name, label, example, note}]。示例按现在这一刻、timezone(默认 Asia/Shanghai)触发算;
note 是补充说明(ISO 周的跨年规则),没有的是空串。"""
note 是补充说明(ISO 周的跨年规则),没有的是空串。trigger_kind=event 时按 event(事件种类)另给 {{ event.* }}、去掉
上一次计划触发时间。"""
zone = _timezone(tz) if tz is not None else service.DEFAULT_TIMEZONE
return service.variables(tz=zone)
if trigger_kind is None or _trigger_kind(trigger_kind) == "cron":
return service.variables(tz=zone)
if event not in triggers.EVENTS:
raise _invalid("请指明事件种类:上传表格出了新的一期、上游工作流运行成功结束、数据源有要处理的变化", "trigger")
return service.variables(tz=zone, event=event)
@router.get("/trigger-options")
async def trigger_options(session: AsyncSession = Depends(get_session)) -> dict[str, Any]:
"""新建、修改事件触发任务时能选的:每种事件的对象(能不能选、为什么不能,由这里判,界面不自己判)、「只认某种来源」的
来源、「只认某个应用」的应用、去抖的缺省值、触发链的上限。
{events: [{event, label, subject_label, objects: [{id, name, ok, reason}]}], origins: [{kind, label}],
apps: [{id, name}], default_debounce_s, max_debounce_s, max_depth}"""
from app.db.models import App, DataSource
sources = list((await session.execute(select(DataSource).order_by(DataSource.name))).scalars())
flows = list((await session.execute(select(Workflow).where(Workflow.is_template.is_(False))
.order_by(Workflow.name))).scalars())
tables = []
for src in sources:
if (src.origin or "manual") != "upload":
continue
ok = await triggers.accumulating(session, src)
tables.append({"id": src.id, "name": src.name, "ok": ok,
"reason": None if ok else "每次上传替换整张表,没有「新的一期」:要用这个事件请在导入设置里改为按期累积"})
objects = {
"table.version": tables,
"workflow.finished": [{"id": w.id, "name": w.name, "ok": bool(w.published_version),
"reason": None if w.published_version else "还没有发布版本:只有正式运行成功结束才算事件"}
for w in flows],
"source.changed": [{"id": src.id, "name": src.name, "ok": True, "reason": None} for src in sources],
}
apps = (await session.execute(select(App.id, App.name).where(App.status != "archived").order_by(App.name))).all()
return {
"events": [{"event": e, "label": triggers.EVENT_LABEL[e], "subject_label": triggers.SUBJECT_LABEL[e],
"objects": objects[e]} for e in triggers.EVENTS],
"origins": [{"kind": k, "label": triggers.ORIGIN_LABEL[k]} for k in triggers.FORMAL_ORIGINS],
"apps": [{"id": i, "name": n} for i, n in apps],
"default_debounce_s": triggers.DEFAULT_DEBOUNCE_S, "max_debounce_s": triggers.MAX_DEBOUNCE_S,
"max_depth": triggers.MAX_DEPTH,
}
@router.post("/preview")
@@ -491,23 +634,18 @@ async def preview(request: Request, session: AsyncSession = Depends(get_session)
"""{cron, timezone, input_template?, workflow_id?, n?=5} → {description_text, next: [ISO(UTC)…], inputs}。
inputs 是按下一次触发渲染的输入。带了 workflow_id 时对照入口字段的名字;必填项没填不在这里报——用户还在填。
trigger_kind=event 时带 trigger、不带 cron:description_text 是触发说明,next 为空,inputs 按一个示例事件渲染(对象最近
一次真实的事件,没有时是示例值),另给 event {text, real}。
"""
body = await _body(request)
if _trigger_kind(body.get("trigger_kind", "cron")) == "event":
return await _preview_event(session, body)
cron_expr, tz = _cron(body.get("cron")), _timezone(body.get("timezone"))
template = _template(body.get("input_template"))
n = body.get("n", 5)
n = min(max(n, 1), MAX_PREVIEW) if isinstance(n, int) and not isinstance(n, bool) else 5
workflow_id = body.get("workflow_id")
if isinstance(workflow_id, str) and workflow_id.strip():
workflow = await session.get(Workflow, workflow_id.strip())
if workflow is not None:
# 还不知道按哪一类运行:发布版本和画布的字段都认
published = await _published_graph(session, workflow) if workflow.published_version else None
fields = service.input_fields(workflow.graph) + service.input_fields(published)
try:
service.check_fields(template, fields, workflow.name, require=False)
except service.TemplateError as e:
raise _invalid(str(e), "input_template") from e
await _preview_fields(session, body, template)
upcoming = cron.preview(cron_expr, tz, n)
first = upcoming[0] if upcoming else utcnow()
return {
@@ -517,6 +655,44 @@ async def preview(request: Request, session: AsyncSession = Depends(get_session)
}
async def _preview_fields(session: AsyncSession, body: dict[str, Any], template: dict[str, str]) -> None:
"""预览时带了 workflow_id:对照入口字段的名字(发布版本和画布的字段都认,还不知道按哪一类运行)。"""
workflow_id = body.get("workflow_id")
if not (isinstance(workflow_id, str) and workflow_id.strip()):
return
workflow = await session.get(Workflow, workflow_id.strip())
if workflow is None:
return
published = await _published_graph(session, workflow) if workflow.published_version else None
fields = service.input_fields(workflow.graph) + service.input_fields(published)
try:
service.check_fields(template, fields, workflow.name, require=False)
except service.TemplateError as e:
raise _invalid(str(e), "input_template") from e
async def _preview_event(session: AsyncSession, body: dict[str, Any]) -> dict[str, Any]:
if not _blank(body.get("cron")):
raise _invalid(EVENT_NO_CRON, "cron")
tz = _timezone(body.get("timezone", service.DEFAULT_TIMEZONE))
trigger = _trigger(body.get("trigger"))
template = _template(body.get("input_template"), trigger["event"])
await _preview_fields(session, body, template)
try:
checked = await triggers.check(session, trigger)
except triggers.TriggerError as e:
raise _invalid(str(e), e.field) from e
sample = await triggers.sample(session, trigger)
fire = outbox.sample_fire(sample, tz)
now = utcnow().replace(microsecond=0)
return {
"description_text": triggers.describe(trigger, checked.subject_name, app_name=checked.app_name),
"next": [],
"inputs": service.render_inputs(template, service.template_values(now, tz, ""), event=fire.values),
"event": {"text": fire.text, "real": sample.real},
}
# --------------------------------------------------------------------------
# 增删改查
# --------------------------------------------------------------------------
@@ -540,7 +716,12 @@ def schedule_values(body: dict[str, Any]) -> dict[str, Any]:
不碰库:发布单的预检用它「只检查」,建任务的接口和发布单建任务都先过它。
target_kind 缺省 workflow:交回的键和以前一字不差。非工作流目标交回 target_kind、target_id,运行类别存探索(不用它)、
输入模板为空、没有 Webhook。"""
输入模板为空、没有 Webhook。
trigger_kind 缺省 cron:同上一字不差。event 时目标只能是工作流,不写 cron(存空串),时区缺省 Asia/Shanghai(日期变量按它算),
另交回 trigger_kind、trigger(规范化之后的触发条件),输入模板按事件种类核对。"""
if _trigger_kind(body.get("trigger_kind", "cron")) == "event":
return _event_values(body)
kind = _target_kind(body.get("target_kind", "workflow"))
if kind != "workflow":
_target_extras(body, kind)
@@ -558,6 +739,29 @@ def schedule_values(body: dict[str, Any]) -> dict[str, Any]:
return {**values, "webhook": _new_webhook(body.get("webhook"))}
#: 事件触发的任务不写 cron
EVENT_NO_CRON = "事件触发的定时任务不写 cron 表达式:它在事件发生时触发,不按时间"
#: 事件触发只能指向工作流
EVENT_WORKFLOW_ONLY = "事件触发的定时任务只能指向工作流:评测题集、数据源维护请用定时触发"
def _event_values(body: dict[str, Any]) -> dict[str, Any]:
"""事件触发任务的请求体 → 规范化之后的列(同 schedule_values,不碰库)。"""
if _target_kind(body.get("target_kind", "workflow")) != "workflow":
raise _invalid(EVENT_WORKFLOW_ONLY, "target_kind")
if not _blank(body.get("cron")):
raise _invalid(EVENT_NO_CRON, "cron")
trigger = _trigger(body.get("trigger"))
values = {"name": _name(body.get("name")), "workflow_id": _workflow_id(body.get("workflow_id")), "cron": "",
"timezone": _timezone(body.get("timezone", service.DEFAULT_TIMEZONE)),
"run_class": _run_class(body.get("run_class", "formal")),
"input_template": _template(body.get("input_template"), trigger["event"]),
"enabled": _enabled(body.get("enabled", True)), "overlap": _overlap(body.get("overlap", "skip")),
"misfire_grace_s": _grace(body.get("misfire_grace_s", DEFAULT_GRACE_S)),
"trigger_kind": "event", "trigger": trigger}
return {**values, "webhook": _new_webhook(body.get("webhook"))}
def template_problem(template: dict[str, str], graph: Any, workflow_name: str) -> str | None:
"""输入模板对不上这张图的入口字段(字段名、必填项):说明;对得上为 None。发布单的预检按将要发布的图查。"""
try:
@@ -591,10 +795,17 @@ async def create_schedule_row(session: AsyncSession, body: dict[str, Any], *, ac
await session.refresh(row)
return row
workflow = await _check_workflow(session, values["workflow_id"], values["run_class"], values["input_template"])
next_run_at = None
if values.get("trigger_kind") == "event":
# 事件触发:核对对象、查环;记下对象现在的名字(对象删掉之后说法里用它)。没有下一次「到点」
checked = await _check_trigger(session, values["trigger"], schedule_id=None,
target_workflow_id=values["workflow_id"])
values["trigger"] = {**values["trigger"], "subject_name": checked.subject_name}
else:
next_run_at = service.next_run(values["cron"], values["timezone"], values["enabled"])
# 目标的几列只经 scheduler/service 的一处封装写(工作流目标:target_id 同 workflow_id)
values |= service.target_columns("workflow", values["workflow_id"])
row = Schedule(**values, workflow_name=workflow.name,
next_run_at=service.next_run(values["cron"], values["timezone"], values["enabled"]),
row = Schedule(**values, workflow_name=workflow.name, next_run_at=next_run_at,
created_by=actor, **_webhook_columns(hook))
session.add(row)
await session.commit()
@@ -630,8 +841,13 @@ async def update_schedule(schedule_id: str, request: Request,
kind, current_target = service.target_of(row)
if "target_kind" in body and _target_kind(body["target_kind"]) != kind:
raise _invalid("定时任务的目标种类建好之后不能改:请新建一个定时任务", "target_kind")
event = _event_kind(row)
if "trigger_kind" in body and _trigger_kind(body["trigger_kind"]) != ("event" if event else "cron"):
raise _invalid("定时任务的触发方式建好之后不能改:请新建一个定时任务", "trigger_kind")
if kind != "workflow":
return await _update_target_schedule(session, row, body, kind, current_target, version)
if event:
return await _update_event_schedule(session, row, body, version)
if "name" in body:
values["name"] = _name(body["name"])
if "workflow_id" in body:
@@ -681,6 +897,60 @@ async def update_schedule(schedule_id: str, request: Request,
return await _schedule_out(session, await _get(session, schedule_id))
async def _update_event_schedule(session: AsyncSession, row: Schedule, body: dict[str, Any],
version: int) -> dict[str, Any]:
"""改一个事件触发的定时任务:名称、目标工作流、运行类别、输入模板、触发条件、时区、启用、重叠、补发宽限、Webhook。cron 不适用
(整份表单带回来的空串不算);下一次触发一直为空。看值变没变(同 update_schedule):触发条件、目标真改了才重新核对对象、查环,
模板、事件种类、目标、运行类别真改了才按改后的组合核对模板和入口字段。"""
if "cron" in body and not _blank(body["cron"]):
raise _invalid(EVENT_NO_CRON, "cron")
values: dict[str, Any] = {}
stored = dict(row.trigger or {}) if isinstance(row.trigger, dict) else {}
current = {k: v for k, v in stored.items() if k != "subject_name"}
trigger = _trigger(body["trigger"]) if "trigger" in body else current
if "name" in body:
values["name"] = _name(body["name"])
if "workflow_id" in body:
values |= service.target_columns("workflow", _workflow_id(body["workflow_id"]))
for key, check in (("timezone", _timezone), ("run_class", _run_class), ("enabled", _enabled),
("overlap", _overlap), ("misfire_grace_s", _grace)):
if key in body:
values[key] = check(body[key])
if "input_template" in body:
values["input_template"] = _template(body["input_template"], trigger.get("event"))
if "webhook" in body:
values |= _patched_webhook(row, body["webhook"])
def merged(key: str) -> Any:
return values.get(key, getattr(row, key))
def changed(key: str) -> bool:
if key not in values:
return False
return values[key] != (dict(getattr(row, key) or {}) if key == "input_template" else getattr(row, key))
trigger_changed = trigger != current
if trigger_changed or changed("workflow_id"):
checked = await _check_trigger(session, trigger, schedule_id=row.id, target_workflow_id=merged("workflow_id"))
values["trigger"] = {**trigger, "subject_name": checked.subject_name}
if trigger_changed and "input_template" not in values:
# 换了事件种类:原来的模板里的 {{ event.* }} 不一定还认
values["input_template"] = _template(dict(row.input_template or {}), trigger.get("event"))
if changed("workflow_id") or changed("run_class") or changed("input_template") or trigger_changed:
workflow = await _check_workflow(session, merged("workflow_id"), merged("run_class"), merged("input_template"))
values["workflow_name"] = workflow.name
result = await session.execute(
update(Schedule).where(Schedule.id == row.id, Schedule.version == version)
.values(**values, version=version + 1).execution_options(synchronize_session=False))
if result.rowcount != 1:
await session.rollback()
raise HTTPException(409, "这个定时任务刚被别人修改过,请刷新后再保存")
schedule_id = row.id
await session.commit()
session.expire_all()
return await _schedule_out(session, await _get(session, schedule_id))
async def _update_target_schedule(session: AsyncSession, row: Schedule, body: dict[str, Any], kind: str,
current_target: str, version: int) -> dict[str, Any]:
"""改一个非工作流目标的定时任务:名称、目标(同一种)、cron、时区、启用、重叠、补发宽限。工作流、输入模板、运行类别、
@@ -740,7 +1010,8 @@ async def set_enabled(session: AsyncSession, row: Schedule, enabled: bool) -> Sc
return row
await session.execute(
update(Schedule).where(Schedule.id == row.id)
.values(enabled=enabled, next_run_at=_next_after_edit(row, row.cron, row.timezone, enabled),
.values(enabled=enabled,
next_run_at=None if _event_kind(row) else _next_after_edit(row, row.cron, row.timezone, enabled),
version=row.version + 1).execution_options(synchronize_session=False))
await session.commit()
# 只重读这一行:调用方(发布单)同一个会话里还拿着别的对象,不能整个会话过期
@@ -787,9 +1058,16 @@ async def run_now(schedule_id: str, session: AsyncSession = Depends(get_session)
发布版本发起。运行记在点按钮的人名下,没有署名时记在「定时任务:<名字>」名下。返回 {fire, run_id};目标是评测题集的,
发起的是一次评测,fire.eval_run_id 是它,run_id 为空。
触发时间取到秒:连点两下落在同一秒的,交回同一条触发记录,不会发起两次。"""
await _get(session, schedule_id)
fire = await service.fire_once(schedule_id, utcnow().replace(microsecond=0), "manual", actor=actor_of(x_actor))
触发时间取到秒:连点两下落在同一秒的,交回同一条触发记录,不会发起两次。
事件触发的任务按示例事件发起(scheduler/triggers.sample:对象最近一次真实的事件):运行来源里记 trigger(sample=true),
触发链从这里起算。对象还没发生过这种事件、模板又用到 {{ event.* }} 的照实记一次未能发起。"""
row = await _get(session, schedule_id)
event = None
if _event_kind(row) and isinstance(row.trigger, dict):
event = outbox.sample_fire(await triggers.sample(session, row.trigger), row.timezone)
fire = await service.fire_once(schedule_id, utcnow().replace(microsecond=0), "manual", actor=actor_of(x_actor),
event=event)
if fire is None:
raise HTTPException(404, _GONE)
return {"fire": (await _fire_outs(session, [fire]))[0], "run_id": fire.run_id}
+5 -1
View File
@@ -290,7 +290,11 @@ async def _from_text(session: AsyncSession, source: Source) -> str:
n = await session.scalar(select(func.count(Run.id)).where(
Run.schedule_id == run.schedule_id, Run.created_at <= run.created_at))
name = schedule.name if schedule is not None else "已删除的定时任务"
return f"定时任务「{name}」第 {int(n or 1)} 次"
# 由数据事件触发的(scheduler/outbox:运行来源里记着 trigger)照实写是什么事件;立即运行按示例事件发起的不算
trigger = (run.origin or {}).get("trigger") if isinstance(run.origin, dict) else None
event = isinstance(trigger, dict) and trigger.get("text") and not trigger.get("sample")
lead = f"由事件触发:{trigger['text']}," if event else ""
return f"{lead}定时任务「{name}」第 {int(n or 1)} 次"
return "正式运行"
+5
View File
@@ -345,6 +345,11 @@ async def notify_after_job(source_id: str, job_id: str) -> int:
except Exception: # noqa: BLE001
logger.exception("任务 %s 结束后推送数据源变化的通知时出错", job_id)
n = 0
if n:
# 数据事件触发(scheduler/outbox):这一批「要处理」的变化记一条 source.changed(一个任务一条)
from app.scheduler import outbox
await outbox.emit_source_changed(source_id, job_id, n)
try:
# 口径账本的召回(next-round 02 做法 7 三):这次记下的变化里表或列被删、类型变了的,标用过它的答案「依据的表结构已变」
from app.ledger import recall
+16
View File
@@ -1081,6 +1081,16 @@ def _activation_row(source_id: str, snapshot: str, previous: str | None, activat
)
async def _version_event(session: AsyncSession, source_id: str, name: str, snap_id: str, previous: str | None,
fields: dict[str, Any], seq: int | None) -> None:
"""启用按期累积的新一期时记一条待派发事件(table.version,scheduler/outbox.stage_table_version),随这次提交一起落库。"""
from app.scheduler import outbox
await outbox.stage_table_version(session, source_id=source_id, source_name=name, snapshot_id=snap_id,
previous_snapshot_id=previous, period_start=fields.get("period_start"),
period_end=fields.get("period_end"), seq=seq)
def _usable_tmp(tmp: Path) -> bool:
"""临时库在、非空、在上传目录里。不在上传目录里的一律不认:发布结束要删它,不能删到别处去。"""
try:
@@ -1230,6 +1240,9 @@ async def publish_build(
if before_commit is not None:
await before_commit(imp_id, snap_id)
session.add(_activation_row(source_id, snap_id, previous, activation, default_import=imp_id))
if snap_mode == "accumulate":
# 数据事件触发(scheduler/outbox):按期累积的新一期启用了,和切指针同一个事务记一条待派发事件
await _version_event(session, source_id, source.name, snap_id, previous, import_fields, seq)
session.add(source)
source.kind, source.origin, source.readonly = "sqlite", "upload", True
@@ -1708,6 +1721,9 @@ async def publish_snapshot(
await before_commit(new_import.import_id if new_import is not None else None, snap_id)
session.add(_activation_row(source_id, snap_id, previous, activation,
default_import=new_import.import_id if new_import is not None else None))
if mode == "accumulate" and new_import is not None:
# 数据事件触发(scheduler/outbox):按期累积的新一期启用了(移除、作废没有新的一期,不算)
await _version_event(session, source_id, source.name, snap_id, previous, new_import.fields, seq)
source.current_snapshot_id = snap_id
source.schema_cache, source.schema_synced_at = cache, now
# database 只作显示:查询一律从快照解析
+2
View File
@@ -1094,3 +1094,5 @@ from app.db.models_blueprints import ( # noqa: E402,F401
# 下一轮第 2 波(docs/design/next-round):数据源对象索引与后台探查(models_sources)、评测(models_eval)。整模块导入,
# 各自的模型类由那两个文件自己登记,这里不逐个列名
from app.db import models_chat, models_eval, models_sources # noqa: E402,F401
# 编排完善第 1 波(docs/design/orchestration-round/00-总方案.md §4):数据事件触发的待派发事件和去抖等待
from app.db import models_triggers # noqa: E402,F401
+94
View File
@@ -0,0 +1,94 @@
"""数据事件触发的待派发事件和去抖等待(docs/design/orchestration-round/00-总方案.md §4)。
由 app/db/models.py 末尾整模块导入,create_all 建表(都是新表,只加不改)。这里不导入 app.db.models:models.py 导入本模块时
自己还没加载完,外键一律按表名写。
**为什么要一张「待派发事件」表,而不是在发生处直接发起运行。** 事件发生在别的事务里:启用上传表格的新版本、正式运行的
收尾、记下数据源的变化。在那里直接发起运行,发起失败要不要回滚那次启用?重复发生、服务重启时发起到一半又算什么?说不清。
改成在发生处写一行 TriggerEvent,调度循环一拍取走、按各定时任务的触发条件派发:发起运行的规矩(重叠跳过、正式运行只从
发布版本发起、触发记录唯一)全部沿用定时任务那一套。
什么时候和那件事同一个事务:table.version(切指针那一下)、workflow.finished(写终态那一下)是同一个事务——发生了就一定有
这一行,没发生就一定没有。source.changed 不是:一次探查、剖析的变化分好几批落库,「这一批只触发一次」要等任务结束才知道,
事件在任务结束后另开会话写(data/source_changes.notify_after_job)。中间进程死了、库被锁住写不进,变化记录在、事件没有:
调度的一拍按任务号补写(scheduler/outbox.backfill_source_changes),dedupe_key(source.changed:<任务号>)唯一,补写和正常的
那一次不会记成两条。
调度关着(AGENTLAB_SCHEDULER)时没人取,事件留在表里,接口照实说有几条在等。第 2 波多进程时,取走靠的是条件 UPDATE,
多个进程也只有一个取得到。
- TriggerEvent 一次事件:种类(table.version / workflow.finished / source.changed)、对象 id、给模板用的事件内容、触发链
的深度和经过的运行、发生时刻、去重键(只有 source.changed 用)。status:pending 还没派发;routed 已经按触发条件派给了
matched 个定时任务(0 个也算派完)。
- TriggerDelivery 一个事件派给一个定时任务、等着去抖:同一个定时任务窗口内(第一条事件发生之后 debounce_s 秒)的几条
合成一次触发,fire_id 是那次触发记录;status:pending 等着、fired 合进了某次触发(发起、跳过、未能发起都算)、dropped
定时任务在等待期间被停用或改了触发条件,不再派发。(定时任务, 事件) 唯一:同一个事件不会派给同一个任务两次。
"""
from __future__ import annotations
from datetime import datetime
from typing import Any
from sqlalchemy import ForeignKey, Index, Integer, String, UniqueConstraint
from sqlalchemy.orm import Mapped, mapped_column
from app.db.base import Base, UTCDateTime, new_id, utcnow
#: 事件种类:上传表格启用了新的一期、工作流的正式运行成功结束、数据源出现要处理的变化
EVENT_KINDS = ("table.version", "workflow.finished", "source.changed")
#: 事件的状态:还没派发、已经派发(派给了 matched 个定时任务)
EVENT_STATUSES = ("pending", "routed")
#: 派给一个定时任务之后的状态:等着去抖、合进了一次触发、不再派发
DELIVERY_STATUSES = ("pending", "fired", "dropped")
class TriggerEvent(Base):
"""一次待派发的事件。payload 只放给输入模板用的值({{ event.* }},见 scheduler/triggers.py),workflow.finished 的
出口字段不复制进来:派发时按 run_id 读那次运行的成果(运行记录已经落库、不会再变)。"""
__tablename__ = "trigger_events"
__table_args__ = (
Index("ix_trigger_events_status", "status", "created_at"),
Index("ix_trigger_events_subject", "event", "subject_id"),
)
id: Mapped[str] = mapped_column(String(32), primary_key=True, default=new_id)
event: Mapped[str] = mapped_column(String(30))
#: 对象 id:表格数据源 id(table.version)、工作流 id(workflow.finished)、数据源 id(source.changed)
subject_id: Mapped[str] = mapped_column(String(32), default="")
#: 对象当时的名字(给人看的说法里用;对象之后改名、删除都不影响这一条)
subject_name: Mapped[str] = mapped_column(String(200), default="")
payload: Mapped[dict[str, Any]] = mapped_column(default=dict)
#: 触发链的深度:workflow.finished 时是上游那次运行自己由事件触发了几层(不是事件触发的为 0),别的种类为 0。
#: 派发时新运行的深度是它加 1,超过上限(scheduler/triggers.MAX_DEPTH)不发起
depth: Mapped[int] = mapped_column(Integer, default=0)
#: 经过的运行 id(从最上游起),给记录、排查循环用;只截最近的几条
chain: Mapped[list[Any]] = mapped_column(default=list)
occurred_at: Mapped[datetime] = mapped_column(UTCDateTime, default=utcnow)
status: Mapped[str] = mapped_column(String(20), default="pending")
routed_at: Mapped[datetime | None] = mapped_column(UTCDateTime, default=None)
#: 派给了几个定时任务
matched: Mapped[int] = mapped_column(Integer, default=0)
#: 去重键:同一件事只记一条。source.changed 是 source.changed:<任务号>(任务结束时记、没记上的补写,两边撞上唯一约束);
#: 别的种类为空(它们和那件事同一个事务,不会记两遍)
dedupe_key: Mapped[str | None] = mapped_column(String(80), default=None, unique=True)
created_at: Mapped[datetime] = mapped_column(UTCDateTime, default=utcnow)
class TriggerDelivery(Base):
"""一个事件派给了一个定时任务,等着去抖、合成一次触发。"""
__tablename__ = "trigger_deliveries"
__table_args__ = (
UniqueConstraint("schedule_id", "event_id"),
Index("ix_trigger_deliveries_waiting", "schedule_id", "status", "occurred_at"),
)
id: Mapped[str] = mapped_column(String(32), primary_key=True, default=new_id)
schedule_id: Mapped[str] = mapped_column(ForeignKey("schedules.id", ondelete="CASCADE"))
event_id: Mapped[str] = mapped_column(ForeignKey("trigger_events.id", ondelete="CASCADE"))
#: 同事件的发生时刻(去抖窗口按它算)
occurred_at: Mapped[datetime] = mapped_column(UTCDateTime, default=utcnow)
status: Mapped[str] = mapped_column(String(20), default="pending")
#: 合进的那次触发(schedule_fires.id)
fire_id: Mapped[str | None] = mapped_column(String(32), default=None)
created_at: Mapped[datetime] = mapped_column(UTCDateTime, default=utcnow)
+2
View File
@@ -1063,6 +1063,8 @@ class RunManager:
await _close_approvals(session, run_id, actor, now)
if event is not None:
await _stage(session, event, run)
from app.scheduler import outbox # 数据事件触发:正式运行成功结束,同一个事务记一条待派发事件
outbox.stage_run_finished(session, run)
await session.commit()
if event is not None:
await bus.publish(event)
+2
View File
@@ -6,6 +6,8 @@
- webhook.py:通知推送(通用 / 飞书 / 企业微信 / 钉钉)
- auto_compare.py:运行成功结束后和同一定时任务上一次成功的运行自动对比,结果记在触发记录上,通知里只带计数摘要和
对比页链接
- triggers.py、outbox.py:数据事件触发(trigger_kind=event,docs/design/orchestration-round/00-总方案.md §4)——触发条件的
规则和说法、防环;待派发事件的写入(发生处)与每一拍的派发、去抖
服务启动时(main.py 的 lifespan)只在 scheduler_enabled() 时调 start,关闭时总是调 stop——stop 要能在没 start
过时安全返回。start 自己也看一次开关:拷来的数据目录(检查脚本的 --stacks、验收副本)不管从哪条路径起服务,
+19 -1
View File
@@ -6,6 +6,9 @@
错过的触发(服务停机、进程卡住):只看最近应触发的那一次——在宽限内补一次(kind=misfire),超出就记一条
skipped_misfire;更早的积压只记一条跳过,不补。下一次一律推进到现在之后,不会醒来就把积压的一口气全发起。
事件触发的任务(docs/design/orchestration-round/00-总方案.md §4)不在 fire_due 里:它们没有 next_run_at。每一拍发完到点的,
接着派待派发事件(scheduler/outbox.dispatch_due)。调度关着时这一拍不存在,事件留在表里。
"""
from __future__ import annotations
@@ -112,14 +115,28 @@ def _span(first: datetime, last: datetime, tz: str) -> str:
async def tick(now: datetime | None = None) -> None:
"""完整的一拍:发起到点的,处理流程的时限和核实,再推送该推送的通知。测试和手动调用用;循环里分开跑(见 _Runner._loop)。"""
"""完整的一拍:发起到点的,派发数据事件,处理流程的时限和核实,再推送该推送的通知。测试和手动调用用;循环里分开跑
(见 _Runner._loop)。"""
from app.scheduler import webhook
await fire_due(now)
await dispatch_events(now)
await process_due(now)
await webhook.notify_due(now)
async def dispatch_events(now: datetime | None = None) -> None:
"""派发待派发事件(scheduler/outbox)。出错只记日志:这一拍出错不该让调度循环停下,也不该挡着后面的流程处理。"""
from app.scheduler import outbox
try:
done = await outbox.dispatch_due(now)
if done:
logger.info("事件触发:这一拍记下了 %d 次触发", len(done))
except Exception: # noqa: BLE001
logger.exception("派发数据事件这一拍出错,下一拍照常进行")
async def process_due(now: datetime | None = None) -> None:
"""流程的一拍(app/process):待办的提醒、升级、超时驳回(永不自动批准);结果未知的写入按退避去查;
上传了却一直没提交的文件过了时限清掉(app/files,每小时一次);访问密钥的请求记录批量写库、过了保留期的清掉
@@ -175,6 +192,7 @@ class _Runner:
while not stopping.is_set():
try:
await fire_due()
await dispatch_events()
await process_due()
# stop 撞在这一拍的发起当中时,收尾不再新起通知扫描:stop 只等它动手时手上的那一份
if not stopping.is_set() and (self.notify is None or self.notify.done()):
+507
View File
@@ -0,0 +1,507 @@
"""待派发事件(docs/design/orchestration-round/00-总方案.md §4):在发生处写、调度循环一拍取走、按触发条件派给定时任务、去抖
合并、发起。触发条件的写法和说法在 triggers.py,发起在 service.fire_once。
**写(发生处)。** 三处,各自只加一两行调用这里:
- 上传表格按期累积的新一期启用(data/table_versions 的 publish_build / publish_snapshot,切指针的同一个事务):stage_table_version;
- 正式运行成功收尾(engine/runner 的 _finalize,写终态的同一个事务):stage_run_finished;途经驳回的(W1-b 的驳回出口)带上
rejected_via,缺省不认它的任务不派;
- 探查、剖析任务结束、这一批新记了「要处理」的变化(data/source_changes.notify_after_job):emit_source_changed,一个任务一条。
这一处不和变化记录同一个事务(一批只触发一次要等任务结束才知道),中间丢了的由每一拍的 backfill_source_changes 按任务号补写,
去重键唯一、不会记成两条。
发生处只写一行 TriggerEvent,不查有没有任务在等、不发起运行:事务边界、重复与重启都在这边说清。
**取(调度循环的一拍,dispatch_due)。**
1. 派:pending 的事件逐条按条件 UPDATE 认领成 routed(多个进程也只有一个认领得到),同一个事务里给每个合条件、启用着的
事件触发任务写一行 TriggerDelivery。没人合条件的也认领掉(matched=0)。
2. 发:每个有等待中的 delivery 的任务,按发生时刻把它们切成去抖窗口:窗口从第一条的发生时刻起算 debounce_s 秒,落在里面的
几条合成一次触发,内容取最新的那一条,fire_at 是窗口的结束时刻(同一个窗口算出来的永远是同一个时刻,触发记录
(任务, fire_at, kind) 唯一,重复派发、重启后补派都不会发起两次)。上传表格按期次分窗口(triggers.window_key):9 月、10 月
连着启用各有各的触发,同一期的重传照旧合并。一拍里把到点的窗口处理完(最多 MAX_WINDOWS 个),不是一拍一个:以前积压时
一拍只走一个窗口,后面的越等越久、最后全过了宽限。到点的窗口里已经过了补发宽限的(排在前面)合成**一条** skipped_misfire,
写合并了几次事件、几个窗口、时间跨度——派给任务就已经晚了(调度停过)才说「调度未运行」,否则照实说积压;之后宽限内的
照常发起:上一次的运行还没结束照 overlap=skip 记 skipped_overlap,触发链超过上限记未能发起。处理过的标 fired。任务停用了、
改了触发条件,等着的标 dropped,不再派发。
调度关着(AGENTLAB_SCHEDULER)时没有这一拍:事件留在表里,接口照实说有几条在等(pending_for)。
**留多久。** 派完的事件、合进触发的 delivery 留 RETAIN_DAYS 天给排查,之后由这一拍顺手清掉(每小时最多一次)。
"""
from __future__ import annotations
import logging
from dataclasses import dataclass, field
from datetime import datetime, timedelta
from typing import Any
from sqlalchemy import delete, func, select, update
from app.db.base import SessionLocal, utcnow
from app.db.models import Run, Schedule, ScheduleFire
from app.db.models_triggers import TriggerDelivery, TriggerEvent
from app.scheduler import triggers
logger = logging.getLogger(__name__)
#: 一拍最多派多少条事件(积压时分几拍派完,不让一拍拖太久)
ROUTE_BATCH = 500
#: 派完的事件、合进触发的 delivery 留多少天
RETAIN_DAYS = 30
#: 清理最多多久一次
CLEAN_EVERY = timedelta(hours=1)
#: 触发链里记几条运行 id(从最近的往前)
CHAIN_KEEP = 10
#: 一次触发里记几条合并进来的事件 id
MERGED_KEEP = 20
#: 一个任务一拍最多处理几个到点的去抖窗口、读几条等着的事件(积压特别多时分几拍处理完,不让一拍拖太久)
MAX_WINDOWS = 100
DRAIN_BATCH = 5000
#: source.changed 往回补写几天内结束的任务;补写最多多久一次(刚起的服务第一拍就补)
BACKFILL_DAYS = 2
BACKFILL_EVERY = timedelta(minutes=10)
# --------------------------------------------------------------------------
# 写(发生处)
# --------------------------------------------------------------------------
async def stage_table_version(session: Any, *, source_id: str, source_name: str, snapshot_id: str,
previous_snapshot_id: str | None, period_start: str | None, period_end: str | None,
seq: int | None) -> None:
"""按期累积的上传表格启用了新的一期:往调用方的会话里加一条 table.version(不提交,随切指针一起提交)。这一期在上一个
当前版本里已经有了(统计期相同)就是「替换了已有的一期」(replaced),只认新增一期的任务不认它。读上一个版本时不自动刷写
会话:调用方(table_versions)挂着还没刷进库的导入记录、快照。"""
replaced = False
if previous_snapshot_id and period_start:
from app.db.models import SourceSnapshot, TableImport
with session.no_autoflush:
prev = await session.get(SourceSnapshot, previous_snapshot_id)
ids = [str(x) for x in (prev.imports or [])] if prev is not None else []
if ids:
replaced = bool((await session.execute(select(func.count()).select_from(TableImport).where(
TableImport.id.in_(ids), TableImport.period_start == period_start,
TableImport.period_end == period_end))).scalar())
now = utcnow()
session.add(TriggerEvent(
event="table.version", subject_id=source_id, subject_name=(source_name or "")[:200], occurred_at=now,
payload=triggers.table_payload(source_name, snapshot_id=snapshot_id, period_start=period_start,
period_end=period_end, activated_at=now, replaced=replaced, seq=seq)))
def stage_run_finished(session: Any, run: Run, rejected_via: Any = None) -> None:
"""正式运行成功结束:往收尾的会话里加一条 workflow.finished(不提交,随终态一起提交)。探索运行、没挂在工作流上的不算。
这次运行自己是事件触发的,事件带上它的触发深度和经过的运行,下游据此防环。
rejected_via:这次运行途经了哪几个关卡的驳回(W1-b 的驳回出口:按驳回那条路走完,状态仍是 succeeded;runner 收尾时把
_rejected_via(state) 交进来),缺省 None 是没途经驳回。照发,事件里带上它:缺省不认途经驳回的任务不派(triggers.matches)。"""
if run.status != "succeeded" or run.run_class != "formal" or not run.workflow_id:
return
upstream = (run.origin or {}).get("trigger") if run.origin_kind == "schedule" and isinstance(run.origin, dict) else None
upstream = upstream if isinstance(upstream, dict) else {}
depth = upstream.get("depth") if isinstance(upstream.get("depth"), int) else 0
chain = [str(x) for x in (upstream.get("chain") or []) if x][-(CHAIN_KEEP - 1):] + [run.id]
session.add(TriggerEvent(
event="workflow.finished", subject_id=run.workflow_id, subject_name=(run.workflow_name or "")[:200],
payload=triggers.run_payload(run, rejected_via), depth=depth, chain=chain,
occurred_at=run.finished_at or utcnow()))
def source_key(job_id: str) -> str:
"""source.changed 的去重键:一个任务一条。"""
return f"source.changed:{job_id}"
async def emit_source_changed(source_id: str, job_id: str, new: int, *, occurred_at: datetime | None = None) -> bool:
"""一次探查、剖析任务结束、新记了 new 条「要处理」的变化:记一条 source.changed(自己开会话提交),返回记没记下。同一个任务
只记一条(去重键唯一):「同一次探查 / 剖析产生的一批只触发一次」,补写(backfill_source_changes)和这里撞上也只有一条。
出错只记日志:变化记录本身已经落库,没记上的由补写接着记。"""
from sqlalchemy.exc import IntegrityError
from app.data import source_changes
from app.db.models import DataSource
try:
async with SessionLocal() as session:
if await session.scalar(select(TriggerEvent.id).where(TriggerEvent.dedupe_key == source_key(job_id))):
return False
source = await session.get(DataSource, source_id)
got = await source_changes.counts(session, source_id)
by = got.get("by_severity") or {}
name = source.name if source is not None else ""
session.add(TriggerEvent(
event="source.changed", subject_id=source_id, subject_name=name[:200], dedupe_key=source_key(job_id),
occurred_at=occurred_at or utcnow(),
payload={"source": name, "source_id": source_id, "job_id": job_id, "counts": {
"new": int(new), "action": int(by.get("action") or 0), "warning": int(by.get("warning") or 0),
"info": int(by.get("info") or 0), "pending": int(got.get("pending") or 0)}}))
try:
await session.commit()
except IntegrityError:
await session.rollback() # 同一个任务刚被别处记上(补写和收尾撞在一起)
return False
return True
except Exception: # noqa: BLE001
logger.exception("任务 %s 结束后记数据源变化的待派发事件时出错", job_id)
return False
_backfilled_at: datetime | None = None
async def backfill_source_changes(now: datetime | None = None) -> int:
"""补写 source.changed:BACKFILL_DAYS 天内测到、还开着的「要处理」级变化,按任务号分组;任务已经结束(探查任务;剖析任务
只算取值观察那几种,同 notify_after_job)、却没有那个任务的事件的,补一条(发生时刻取任务结束的时刻)。返回补了几条。
在 Python 里按任务号分组,不用各库写法不同的 JSON 取值。"""
from app.data.catalog_profile_jobs import OBSERVING_SCOPES
from app.db.models import CatalogProfileJob
from app.db.models_sources import IntrospectJob, SourceChange
now = now or utcnow()
async with SessionLocal() as session:
rows = (await session.execute(select(SourceChange.source_id, SourceChange.detected_by).where(
SourceChange.severity == "action", SourceChange.status == "open",
SourceChange.detected_at >= now - timedelta(days=BACKFILL_DAYS)))).all()
jobs: dict[tuple[str, str, str], int] = {}
for source_id, detected in rows:
detected = detected if isinstance(detected, dict) else {}
job_id, kind = detected.get("job_id"), detected.get("job_kind")
if job_id and kind in ("introspect", "profile"):
key = (str(kind), str(job_id), source_id)
jobs[key] = jobs.get(key, 0) + 1
if not jobs:
return 0
have = set((await session.execute(select(TriggerEvent.dedupe_key).where(
TriggerEvent.dedupe_key.in_([source_key(j) for _, j, _ in jobs])))).scalars())
todo: list[tuple[str, str, int, datetime | None]] = []
for (kind, job_id, source_id), n in jobs.items():
if source_key(job_id) in have:
continue
job = await session.get(IntrospectJob if kind == "introspect" else CatalogProfileJob, job_id)
if job is None or job.status in ("queued", "running"):
continue # 还在进行:结束时自己会记
if kind == "profile" and job.scope not in OBSERVING_SCOPES:
continue
todo.append((source_id, job_id, n, job.finished_at))
made = 0
for source_id, job_id, n, finished in todo:
if await emit_source_changed(source_id, job_id, n, occurred_at=finished):
made += 1
logger.info("补写了任务 %s 的数据源变化事件(它结束时没记上)", job_id)
return made
# --------------------------------------------------------------------------
# 一次触发要带的事件
# --------------------------------------------------------------------------
@dataclass
class EventFire:
"""交给 service.fire_once 的事件:模板里 {{ event.* }} 的值、写进触发记录和运行来源的说明、新运行的触发深度。
sample:立即运行时按对象现在的情况凑的(不是真来了一次事件);real 为假时是示例值,用到 event.* 的模板不拿它发起。"""
event: str
subject_id: str
subject_name: str
values: dict[str, Any]
text: str
depth: int = 1
chain: list[str] = field(default_factory=list)
event_ids: list[str] = field(default_factory=list)
first_at: str | None = None
last_at: str | None = None
sample: bool = False
real: bool = True
def brief(self) -> dict[str, Any]:
"""写进触发记录 inputs.event 的那一份(触发历史、通知读它):不含出口字段的值。"""
return {"event": self.event, "subject_id": self.subject_id, "subject_name": self.subject_name,
"text": self.text, "event_ids": self.event_ids[-MERGED_KEEP:], "merged": len(self.event_ids),
"first_at": self.first_at, "last_at": self.last_at, "depth": self.depth, "sample": self.sample,
"real": self.real}
def origin(self) -> dict[str, Any]:
"""运行来源里的 trigger:{kind, event, 对象, 事件 id……},在封存范围内(engine/origin)。记录页据 text 写「由事件触发:…」。"""
return {"kind": "event", "event": self.event, "subject_id": self.subject_id, "subject_name": self.subject_name,
"event_id": self.event_ids[-1] if self.event_ids else None, "event_ids": self.event_ids[-MERGED_KEEP:],
"merged": len(self.event_ids), "depth": self.depth, "chain": self.chain[-CHAIN_KEEP:],
"text": self.text, "sample": self.sample, "real": self.real}
def sample_fire(sample: triggers.Sample, tz: str) -> EventFire:
"""立即运行:按示例事件发起(触发链从这里起算,第 1 层)。是不是示例值记在 real 上,说法不另加后缀:界面、印章按
sample / real 自己说「立即运行,按最近一次事件」「事件为示例值」。"""
text = triggers.event_text(sample.event, sample.subject_name, sample.payload)
return EventFire(event=sample.event, subject_id=sample.subject_id, subject_name=sample.subject_name,
values=triggers.values(sample.event, sample.payload, tz, outputs=sample.outputs),
text=text, sample=True, real=sample.real,
chain=[sample.payload["run_id"]] if sample.event == "workflow.finished" and sample.payload.get(
"run_id") else [])
# --------------------------------------------------------------------------
# 取(调度循环的一拍)
# --------------------------------------------------------------------------
_cleaned_at: datetime | None = None
async def dispatch_due(now: datetime | None = None) -> list[ScheduleFire]:
"""完整的一拍:补写没记上的 source.changed(每 BACKFILL_EVERY 一次,刚起的服务第一拍就补)、派新来的事件、发去抖窗口到了的
触发、顺手清理。返回这一拍记下的触发。一个任务出错不影响别的。"""
global _backfilled_at
now = now or utcnow()
if _backfilled_at is None or now - _backfilled_at >= BACKFILL_EVERY:
_backfilled_at = now
try:
await backfill_source_changes(now)
except Exception: # noqa: BLE001 - 补写是兜底,出错不挡派发
logger.exception("补写数据源变化的待派发事件时出错")
await route(now)
out = await fire_waiting(now)
await _clean(now)
return out
async def route(now: datetime | None = None) -> int:
"""把 pending 的事件派给合条件、启用着的事件触发任务。返回这一拍派了几条事件。"""
now = now or utcnow()
async with SessionLocal() as session:
events = list((await session.execute(select(TriggerEvent).where(TriggerEvent.status == "pending")
.order_by(TriggerEvent.created_at, TriggerEvent.id)
.limit(ROUTE_BATCH))).scalars())
if not events:
return 0
rows = (await session.execute(select(Schedule.id, Schedule.trigger, Schedule.target_kind).where(
Schedule.trigger_kind == "event", Schedule.enabled.is_(True)))).all()
listeners = [(sid, trigger) for sid, trigger, kind in rows if (kind or "workflow") == "workflow"]
routed = 0
for ev in events:
matched = [sid for sid, trigger in listeners if triggers.matches(trigger, ev.event, ev.subject_id, ev.payload)]
try:
async with SessionLocal() as session:
# 条件 UPDATE 认领:两拍撞在一起(以后多开了进程)只有一拍改得动这一行
claimed = await session.execute(
update(TriggerEvent).where(TriggerEvent.id == ev.id, TriggerEvent.status == "pending")
.values(status="routed", routed_at=now, matched=len(matched))
.execution_options(synchronize_session=False))
if claimed.rowcount != 1:
await session.rollback()
continue
for sid in matched:
session.add(TriggerDelivery(schedule_id=sid, event_id=ev.id, occurred_at=ev.occurred_at))
await session.commit()
routed += 1
except Exception: # noqa: BLE001 - 一条出错(任务刚被删掉、外键)不拖住别的,下一拍再派
logger.exception("派发事件 %s 时出错", ev.id)
return routed
async def fire_waiting(now: datetime | None = None) -> list[ScheduleFire]:
"""有等着的事件的任务,各把到点的去抖窗口处理完。"""
now = now or utcnow()
async with SessionLocal() as session:
waiting = list((await session.execute(
select(TriggerDelivery.schedule_id).where(TriggerDelivery.status == "pending").distinct())).scalars())
out: list[ScheduleFire] = []
for schedule_id in waiting:
try:
out.extend(await _drain(schedule_id, now))
except Exception: # noqa: BLE001 - 一个任务出错不能拖住这一拍的其他任务
logger.exception("事件触发的定时任务 %s 这一拍处理失败", schedule_id)
return out
@dataclass
class _Window:
"""一个去抖窗口:同一个分窗口的键(上传表格是期次)、落在里面的几条(delivery id, 事件, 派给任务的时刻)、结束的时刻。"""
key: str
start: datetime
due: datetime
items: list[tuple[str, TriggerEvent, datetime]] = field(default_factory=list)
@property
def events(self) -> list[TriggerEvent]:
return [ev for _, ev, _ in self.items]
def windows(items: list[tuple[str, TriggerEvent, datetime]], debounce_s: int) -> list[_Window]:
"""按发生时刻切成去抖窗口(items 已按发生时刻排好):每个分窗口的键各自一串,窗口从第一条起算 debounce_s 秒;按结束时刻
排。两个窗口结束在同一刻(不同期次同一刻启用)时后一个往后挪 1 微秒:fire_at 不撞触发记录的唯一约束,同样的输入永远切出
同样的窗口。"""
from app.scheduler import service
span = timedelta(seconds=debounce_s)
current: dict[str, _Window] = {}
out: list[_Window] = []
for did, ev, routed in items:
at = service.as_utc(ev.occurred_at)
key = triggers.window_key(ev.event, ev.payload)
win = current.get(key)
if win is None or at > win.due:
win = current[key] = _Window(key, at, at + span)
out.append(win)
win.items.append((did, ev, routed))
out.sort(key=lambda w: (w.due, w.key))
for prev, cur in zip(out, out[1:]):
if cur.due <= prev.due:
cur.due = prev.due + timedelta(microseconds=1)
return out
async def _drain(schedule_id: str, now: datetime) -> list[ScheduleFire]:
"""一个任务这一拍到点的窗口:已过补发宽限的合成一条跳过,其余照常发起。"""
from app.scheduler import loop, service
async with SessionLocal() as session:
schedule = await session.get(Schedule, schedule_id)
if schedule is None:
return []
trigger, tz = schedule.trigger if isinstance(schedule.trigger, dict) else {}, schedule.timezone
if not schedule.enabled or schedule.trigger_kind != "event":
# 等着的时候被停用、改成了别的触发方式:停用是「别再触发了」,等着的不再派发
await _close(session, _pending(schedule_id), "dropped")
return []
rows = (await session.execute(
select(TriggerDelivery.id, TriggerDelivery.created_at, TriggerEvent)
.join(TriggerEvent, TriggerEvent.id == TriggerDelivery.event_id)
.where(TriggerDelivery.schedule_id == schedule_id, TriggerDelivery.status == "pending")
.order_by(TriggerDelivery.occurred_at, TriggerEvent.created_at).limit(DRAIN_BATCH))).all()
# 等着的时候改了触发条件(换了对象、加了过滤):已经不合条件的不再派发
stale = [did for did, _, ev in rows if not triggers.matches(trigger, ev.event, ev.subject_id, ev.payload)]
if stale:
await _close(session, TriggerDelivery.id.in_(stale), "dropped")
live = [(did, ev, service.as_utc(routed)) for did, routed, ev in rows if did not in set(stale)]
due = [w for w in windows(live, triggers.debounce_of(trigger)) if w.due <= now][:MAX_WINDOWS]
if not due:
return []
grace = max(int(schedule.misfire_grace_s or 0), 0)
limit = max(loop.ON_TIME_S, grace)
missed = [w for w in due if (now - w.due).total_seconds() > limit]
timely = [w for w in due if w not in missed]
outputs = {id(w): await _outputs_of(session, w.events[-1]) for w in timely}
out: list[ScheduleFire] = []
if missed:
fire = await _skip_missed(schedule_id, missed, tz, grace)
if fire is not None:
out.append(fire)
await _close_window(missed, fire.id)
for w in timely:
fire = await service.fire_once(schedule_id, w.due, "event", event=_event_fire(w.events, tz, outputs[id(w)]))
if fire is not None:
out.append(fire)
await _close_window([w], fire.id)
return out
def _event_fire(events: list[TriggerEvent], tz: str, outputs: dict[str, Any]) -> EventFire:
"""几条合成一次的事件:内容取最新的那一条,深度取最深的那一条加 1。"""
latest = events[-1]
return EventFire(
event=latest.event, subject_id=latest.subject_id, subject_name=latest.subject_name,
values=triggers.values(latest.event, latest.payload, tz, outputs=outputs),
text=triggers.event_text(latest.event, latest.subject_name, latest.payload),
depth=max(int(ev.depth or 0) for ev in events) + 1, chain=[str(x) for x in latest.chain or []],
event_ids=[ev.id for ev in events], first_at=_iso(events[0].occurred_at), last_at=_iso(latest.occurred_at))
async def _skip_missed(schedule_id: str, missed: list[_Window], tz: str, grace: int) -> ScheduleFire | None:
"""已过补发宽限的几个窗口合成一条 skipped_misfire(fire_at 取最后一个的结束时刻):合并了几次事件、几个窗口、时间跨度、
合掉的期次。派给任务的时刻离事件发生已经超过宽限的(调度停过、没在派)才说「调度未运行」,否则照实说积压。"""
from app.scheduler import service
events = [ev for w in missed for ev in w.events]
events.sort(key=lambda ev: service.as_utc(ev.occurred_at))
late_routed = any((routed - service.as_utc(ev.occurred_at)).total_seconds() > grace
for w in missed for _, ev, routed in w.items)
first, last = service.local_text(events[0].occurred_at, tz), service.local_text(events[-1].occurred_at, tz)
span = first if first == last else f"{first} 至 {last}"
count = f"{len(events)} 次事件" + (f"({len(missed)} 个去抖窗口)" if len(missed) > 1 else "")
if late_routed:
why = "服务当时没有派发(调度未运行或已停止)"
else:
why = "事件积压,轮到派发时"
reason = f"{span} 的 {count}:{why},已超出补发宽限({service.duration_text(grace)}),没有发起;积压的事件不补"
brief = _event_fire(events, tz, {}).brief()
brief["windows"] = len(missed)
periods = [p for p in dict.fromkeys(str((ev.payload or {}).get("period") or "") for ev in events) if p]
if events[-1].event == "table.version" and periods:
brief["periods"] = periods
return await service.record_fire(schedule_id, max(w.due for w in missed), "event", "skipped_misfire", reason,
{"event": brief})
async def _close_window(wins: list[_Window], fire_id: str) -> None:
async with SessionLocal() as session:
await _close(session, TriggerDelivery.id.in_([did for w in wins for did, _, _ in w.items]), "fired",
fire_id=fire_id)
def _pending(schedule_id: str) -> Any:
return (TriggerDelivery.schedule_id == schedule_id) & (TriggerDelivery.status == "pending")
async def _close(session: Any, cond: Any, status: str, *, fire_id: str | None = None) -> None:
await session.execute(update(TriggerDelivery).where(cond, TriggerDelivery.status == "pending")
.values(status=status, fire_id=fire_id).execution_options(synchronize_session=False))
await session.commit()
async def _outputs_of(session: Any, ev: TriggerEvent) -> dict[str, Any]:
"""workflow.finished 的出口字段:读那次运行的成果(运行已经结束、不会再变),过滤同预览、立即运行(triggers.outputs_of)。
读不到为空。"""
if ev.event != "workflow.finished":
return {}
run_id = (ev.payload or {}).get("run_id")
run = await session.get(Run, run_id) if run_id else None
return triggers.outputs_of(run.output if run is not None else None)
def _iso(moment: datetime | None) -> str | None:
from app.scheduler import service
return service.as_utc(moment).isoformat() if moment is not None else None
async def _clean(now: datetime) -> None:
"""派完超过 RETAIN_DAYS 天的事件、合进触发或不再派发的 delivery 清掉。每小时最多一次;出错只记日志。"""
global _cleaned_at
if _cleaned_at is not None and now - _cleaned_at < CLEAN_EVERY:
return
_cleaned_at = now
cutoff = now - timedelta(days=RETAIN_DAYS)
try:
async with SessionLocal() as session:
await session.execute(delete(TriggerDelivery).where(TriggerDelivery.status != "pending",
TriggerDelivery.created_at < cutoff))
waiting = select(TriggerDelivery.id).where(TriggerDelivery.event_id == TriggerEvent.id).exists()
await session.execute(delete(TriggerEvent).where(TriggerEvent.status == "routed",
TriggerEvent.created_at < cutoff, ~waiting)
.execution_options(synchronize_session=False))
await session.commit()
except Exception: # noqa: BLE001
logger.exception("清理派完的待派发事件时出错")
# --------------------------------------------------------------------------
# 给接口看的
# --------------------------------------------------------------------------
async def pending_for(session: Any, schedule: Schedule) -> dict[str, Any]:
"""一个事件触发任务现在有几条事件在等:events 还没派发的(调度关着时一直留在这里)、waiting 已经派给它、在去抖窗口里的;
due_at 是窗口结束、会合成一次触发的时刻(没有在等的为空)。"""
trigger = schedule.trigger if isinstance(schedule.trigger, dict) else {}
subject, event = triggers.subject_of(trigger), trigger.get("event") or ""
rows = (await session.execute(select(TriggerEvent.payload).where(
TriggerEvent.status == "pending", TriggerEvent.event == event, TriggerEvent.subject_id == subject)
.limit(ROUTE_BATCH))).scalars() if subject else []
unrouted = sum(1 for payload in rows if triggers.matches(trigger, event, subject, payload))
waiting, first = (await session.execute(select(func.count(), func.min(TriggerDelivery.occurred_at)).where(
TriggerDelivery.schedule_id == schedule.id, TriggerDelivery.status == "pending"))).one()
from app.scheduler import service
due = service.as_utc(first) + timedelta(seconds=triggers.debounce_of(trigger)) if first is not None else None
return {"events": unrouted, "waiting": int(waiting or 0), "due_at": due.isoformat() if due else None}
+101 -22
View File
@@ -4,6 +4,11 @@
只有一份。发起本身走 engine/launch.py 的 launch_run,和 HTTP 发起是同一套规则——正式运行只能从当前发布版本
发起,这一条不会在定时任务这边被绕过。
**事件触发**(docs/design/orchestration-round/00-总方案.md §4):trigger_kind=event 的任务不按 cron 到点,而是由数据事件触发
(scheduler/triggers.py 的规则、scheduler/outbox.py 的派发)。派发到了照样调 fire_once(kind=event,带上事件 EventFire):
重叠跳过、记触发、发起运行还是这一份;多出来的只有三样——输入模板里能用 {{ event.* }}、运行来源里记 trigger、触发链超过
上限不发起。触发记录的 inputs 写 {event: 事件的说明, inputs: 渲染出的输入}。
**定时的目标**(docs/design/next-round/00-总方案.md X4:定时只有一套):一个定时任务指向一个目标——工作流、评测题集
(app/eval/scheduled.py)或数据源维护(app/data/source_maintenance.py)。认领、补跑、重叠跳过、触发记录、通知都只有这一套,
fire_once 按目标种类分派;建任务、改目标时核对目标也按种类交给各自的模块(check_target)。
@@ -182,6 +187,7 @@ def template_values(fire_at: datetime, tz: str, cron_expr: str | cron.CronSpec)
按 fire_at 算而不是按「现在」:补发晚了一小时、立即运行在零点前后点下去,「昨天」都是那次触发的昨天。
ISO 周的年份是周所在的年(isocalendar),不是日历年:2027-01-01 是 2026-W53,2025-12-29 是 2026-W01。
last_fire_time 是按 cron 往前找的上一次计划触发,不看历史记录:同一个 (cron, 时区, fire_at) 永远渲染出同一份输入。
事件触发的任务没有 cron(存空串),last_fire_time 为空(这个变量在事件触发的模板里也不让用,见 check_template)。
"""
zone = cron.zone(tz)
local = fire_at.astimezone(zone)
@@ -190,7 +196,7 @@ def template_values(fire_at: datetime, tz: str, cron_expr: str | cron.CronSpec)
month_start = today.replace(day=1)
last_month_end = month_start - timedelta(days=1)
last_month_start = last_month_end.replace(day=1)
previous = cron.prev_fire(cron_expr, zone, fire_at)
previous = cron.prev_fire(cron_expr, zone, fire_at) if cron_expr else None
return {
"today": _day(today),
"yesterday": _day(today - timedelta(days=1)),
@@ -225,17 +231,28 @@ def _month(value: date) -> str:
return f"{value.year:04d}-{value.month:02d}"
def render_inputs(template: dict[str, str], values: dict[str, str]) -> dict[str, str]:
"""渲染输入模板。用项目通用的 {{ }} 渲染(engine/expressions.render_template),变量挂在 schedule 下。"""
ctx = {"schedule": values}
def render_inputs(template: dict[str, str], values: dict[str, str], *,
event: dict[str, Any] | None = None) -> dict[str, str]:
"""渲染输入模板。用项目通用的 {{ }} 渲染(engine/expressions.render_template),变量挂在 schedule 下;事件触发的另有
event(scheduler/triggers.values)。"""
ctx: dict[str, Any] = {"schedule": values}
if event is not None:
ctx["event"] = event
return {name: render_template(text, ctx) for name, text in template.items()}
def check_template(template: Any) -> dict[str, str]:
#: 事件触发的模板里不能用的日期变量:没有 cron,就没有「上一次计划触发」
EVENT_UNSUPPORTED = ("schedule.last_fire_time",)
def check_template(template: Any, *, event: str | None = None) -> dict[str, str]:
"""校验输入模板,原样返回(None 当空模板)。写错抛 TemplateError。
项目的模板渲染取不到值时渲染成空串、不报错——画布上的半成品图要能运行。定时任务没人盯着,拼错的变量名
会让每一次运行都拿到空的日期,所以保存和预览时就要说出来。
event 是事件触发任务的事件种类:这时还能用这种事件的 {{ event.* }}(scheduler/triggers),不能用 schedule.last_fire_time。
不给(按 cron 触发的任务)时规则一字不变。
"""
if template is None:
return {}
@@ -252,18 +269,20 @@ def check_template(template: Any) -> dict[str, str]:
if not isinstance(text, str):
raise TemplateError(f"输入「{name}」的值需要是文本(可以带 {{{{ schedule.yesterday }}}} 这样的变量)"
f"{current_value(text)}")
_check_text(name, text)
_check_text(name, text, event)
out[name] = text
return out
def _check_text(name: str, text: str) -> None:
def _check_text(name: str, text: str, event: str | None = None) -> None:
for match in _TEMPLATE_RE.finditer(text):
expr = match.group(1).strip()
if not expr:
raise TemplateError(f"输入「{name}」的模板里有空的 {{{{ }}}},请在中间写变量名,例如 schedule.yesterday")
path, *filters = (part.strip() for part in expr.split("|"))
if path not in _NAMES:
if event is not None:
_check_event_path(name, path, event)
elif path not in _NAMES:
raise TemplateError(f"输入「{name}」的模板里「{path}」不是可用的变量,可用的变量:{'、'.join(_NAMES)}")
for filt in filters:
if filt not in _FILTERS:
@@ -273,15 +292,43 @@ def _check_text(name: str, text: str) -> None:
raise TemplateError(f"输入「{name}」的模板里 {{{{ 和 }}}} 没有成对出现,请检查是否少写了一半")
def variables(now: datetime | None = None, tz: str = DEFAULT_TIMEZONE) -> list[dict[str, str]]:
def _check_event_path(name: str, path: str, event: str) -> None:
"""事件触发任务的模板里的一个变量:schedule.*(上一次计划触发时间除外)或这种事件的 event.*。"""
from app.scheduler import triggers
if path in _NAMES and path not in EVENT_UNSUPPORTED:
return
if path in EVENT_UNSUPPORTED:
raise TemplateError(f"输入「{name}」的模板里「{path}」在事件触发的任务里不可用:没有按时间排的上一次触发")
if triggers.allowed_path(path, event):
return
usable = [n for n in _NAMES if n not in EVENT_UNSUPPORTED] + list(triggers.names(event))
raise TemplateError(f"输入「{name}」的模板里「{path}」不是可用的变量,可用的变量:{'、'.join(usable)}")
def variables(now: datetime | None = None, tz: str = DEFAULT_TIMEZONE, *,
event: str | None = None) -> list[dict[str, str]]:
"""变量清单:[{name, label, example, note}]。示例按 now(默认现在)这一刻触发算;上一次计划触发时间没有 cron
可依,示例按每天同一时刻触发算。note 是补充说明(ISO 周的跨年规则),没有的是空串。"""
可依,示例按每天同一时刻触发算。note 是补充说明(ISO 周的跨年规则),没有的是空串。
event 是事件触发任务的事件种类:去掉上一次计划触发时间,后面接这种事件的 {{ event.* }}(示例是写法示意)。"""
moment = (now or utcnow()).replace(second=0, microsecond=0)
local = moment.astimezone(cron.zone(tz))
daily = f"{local.minute} {local.hour} * * *"
values = template_values(moment, tz, daily)
return [{"name": f"schedule.{key}", "label": label, "example": values[key], "note": note}
for key, label, note in VARIABLES]
out = [{"name": f"schedule.{key}", "label": label, "example": values[key], "note": note}
for key, label, note in VARIABLES]
if event is None:
return out
from app.scheduler import triggers
kept = [({**v, "label": EVENT_FIRE_TIME} if v["name"] == "schedule.fire_time" else v)
for v in out if v["name"] not in EVENT_UNSUPPORTED]
return kept + triggers.variables(event)
#: 事件触发的任务里 schedule.fire_time 的说法:不是「计划」的时刻,是去抖窗口结束、合成这次触发的时刻
EVENT_FIRE_TIME = "本次触发时间(去抖结束的时刻,带时区偏移的 ISO 时间)"
# --------------------------------------------------------------------------
@@ -368,12 +415,15 @@ def next_run(cron_expr: str, tz: str, enabled: bool, now: datetime | None = None
async def fire_once(schedule_id: str, fire_at: datetime, kind: str, *, actor: str | None = None,
) -> ScheduleFire | None:
event: Any = None) -> ScheduleFire | None:
"""处理一次触发,返回记下的触发记录。同一 (任务, fire_at, kind) 已经有记录(两拍撞在一起、立即运行连点两下)
时不再发起,返回已有的那一条;任务不存在返回 None。
按目标种类分派(target_of):工作流在这里发起(_fire_workflow),评测题集、数据源维护交给各自模块的 fire(同样持着
launch_lock,触发记录用 record_fire 写);不认识的种类记 failed_to_start、写明原因,不发起。
event 是事件触发的那一次事件(scheduler/outbox.EventFire):派发时(kind=event)、事件触发任务的立即运行(kind=manual,
示例事件)带上。事件触发只能指向工作流。
"""
async with launch_lock():
async with SessionLocal() as session:
@@ -383,6 +433,8 @@ async def fire_once(schedule_id: str, fire_at: datetime, kind: str, *, actor: st
target_kind, target_id = target_of(schedule)
name, run_class = schedule.name, schedule.run_class
tz, cron_expr, template = schedule.timezone, schedule.cron, schedule.input_template
if target_kind != "workflow" and event is not None:
return await _record(schedule_id, fire_at, kind, "failed_to_start", EVENT_TARGET_ONLY, {})
if target_kind != "workflow":
handler = target_handler(target_kind)
if handler is None:
@@ -391,39 +443,66 @@ async def fire_once(schedule_id: str, fire_at: datetime, kind: str, *, actor: st
return await handler.fire(schedule_id=schedule_id, fire_at=fire_at, kind=kind, actor=actor,
target_id=target_id, name=name, tz=tz, cron_expr=cron_expr)
return await _fire_workflow(schedule_id, fire_at, kind, actor=actor, name=name, workflow_id=target_id,
run_class=run_class, tz=tz, cron_expr=cron_expr, template=template)
run_class=run_class, tz=tz, cron_expr=cron_expr, template=template, event=event)
#: 事件触发只能指向工作流(库里被改成别的目标时照实记)
EVENT_TARGET_ONLY = "事件触发的定时任务只能指向工作流,这次触发没有发起"
#: 触发链超过上限(scheduler/triggers.MAX_DEPTH)
DEPTH_REASON = ("这次运行会是事件触发链上的第 {depth} 层(上限 {limit} 层):为防止工作流之间循环触发,没有发起。"
"请检查「上游工作流运行成功结束」的定时任务是不是串成了环")
#: 立即运行事件触发的任务、对象还没发生过这种事件,模板又用到了 {{ event.* }}
NO_SAMPLE_REASON = "还没有发生过这种事件,输入模板里的 {{ event.* }} 没有可用的值,这次立即运行没有发起"
async def _fire_workflow(schedule_id: str, fire_at: datetime, kind: str, *, actor: str | None, name: str,
workflow_id: str, run_class: str, tz: str, cron_expr: str, template: Any,
) -> ScheduleFire | None:
event: Any = None) -> ScheduleFire | None:
"""目标是工作流的一次触发(持着 launch_lock)。
上一次运行还没结束就记 skipped_overlap;输入渲染不了、launch_run 拒绝或出错记 failed_to_start(拒绝写原话,
其他异常写通用说明、详细信息进日志);发起成功记 started 和运行 id。
事件触发(event 不为空):模板按事件种类校验、渲染 {{ event.* }};触发链超过上限先记 failed_to_start(防环的兜底,比重叠
跳过先判);触发记录的 inputs 写 {event: 说明, inputs: 渲染出的输入};运行来源里带 trigger。
"""
from app.scheduler import triggers
brief = event.brief() if event is not None else None
def recorded(inputs: dict[str, Any]) -> dict[str, Any]:
return inputs if brief is None else {"event": brief, "inputs": inputs}
if event is not None and event.depth > triggers.MAX_DEPTH:
return await _record(schedule_id, fire_at, kind, "failed_to_start",
DEPTH_REASON.format(depth=event.depth, limit=triggers.MAX_DEPTH), recorded({}))
try:
inputs = render_inputs(check_template(template), template_values(fire_at, tz, cron_expr))
inputs = render_inputs(check_template(template, event=event.event if event is not None else None),
template_values(fire_at, tz, cron_expr),
event=event.values if event is not None else None)
except (TemplateError, cron.CronError, cron.TimezoneError) as e:
# 存进库之后才坏掉的(手改过库、时区库升级删了时区):照实说是哪一项,不发起
return await _record(schedule_id, fire_at, kind, "failed_to_start", f"无法生成这次运行的输入:{e}", {})
return await _record(schedule_id, fire_at, kind, "failed_to_start", f"无法生成这次运行的输入:{e}", recorded({}))
if event is not None and not event.real and triggers.uses_event(template):
return await _record(schedule_id, fire_at, kind, "failed_to_start", NO_SAMPLE_REASON, recorded({}))
busy = await _active_run(schedule_id)
if busy is not None:
return await _record(schedule_id, fire_at, kind, "skipped_overlap", overlap_reason(*busy), inputs)
fire = await _record(schedule_id, fire_at, kind, "failed_to_start", INTERRUPTED_REASON, inputs, fresh=True)
return await _record(schedule_id, fire_at, kind, "skipped_overlap", overlap_reason(*busy), recorded(inputs))
fire = await _record(schedule_id, fire_at, kind, "failed_to_start", INTERRUPTED_REASON, recorded(inputs), fresh=True)
if fire is None:
return await _existing(schedule_id, fire_at, kind)
from app.engine.launch import LaunchRefused, launch_run
origin: dict[str, Any] = {"schedule_id": schedule_id, "schedule": name, "fire_id": fire.id, "fire_kind": kind}
if event is not None:
origin["trigger"] = event.origin()
_LAUNCHING.add(fire.id)
try:
try:
async with SessionLocal() as session:
run = await launch_run(session, workflow_id=workflow_id, run_class=run_class,
input_payload=inputs, actor=actor or actor_label(name),
schedule_id=schedule_id, origin_kind="schedule",
origin={"schedule_id": schedule_id, "schedule": name,
"fire_id": fire.id, "fire_kind": kind})
schedule_id=schedule_id, origin_kind="schedule", origin=origin)
except LaunchRefused as e:
return await _finish(fire.id, status="failed_to_start", reason=e.message)
except Exception: # noqa: BLE001 - 发起失败不能让调度循环停下;详细信息进日志,记录上写通用说明
+610
View File
@@ -0,0 +1,610 @@
"""数据事件触发的规则(docs/design/orchestration-round/00-总方案.md §4):触发条件的写法与校验、给人看的说法、输入模板里的
{{ event.* }}、示例事件、防环。
**一套定时机制(X4)。** 事件触发不是另一套调度:它就是一个 trigger_kind=event 的定时任务,目标只能是工作流。认领、重叠跳过、
触发记录、通知、正式运行只从发布版本发起,全部沿用定时任务那一套(scheduler/service.fire_once);不同的只是「什么时候触发」
——不是到点,而是三种数据事件之一(已拍板:不接「口径发布」):
- table.version:一张按期累积的上传表格启用了新的一期(data/table_versions 启用那一下)。可选只认新增的一期(period=new):
重传、替换已有那一期(改错数)的不算;
- workflow.finished:一个工作流的正式运行成功结束。可选只认某个应用(app_id)发起的、某种来源(origin_kind)的;
下游的输入模板能引用这次运行的出口字段({{ event.outputs.<字段> }})。两种成功结束缺省不认:途经驳回的(审批接了驳回出口、
按驳回那条路走完,状态仍是 succeeded,事件里带 rejected_via)——下游会以为「A 成功出了结果」,其实那一期被驳回了,
include_rejected 时才认、模板里有 {{ event.rejected }};应用补算的(app_backfill,升版后按历次输入补算,一连发好几条、
各是某一期的历史输入),include_backfill 或只认这种来源时才认;
- source.changed:一个数据源出现「要处理」级的变化。按源去抖:同一次探查、剖析产生的一批只记一条事件(data/source_changes)。
trigger 的形状:{event, 对象(source_id / workflow_id / source_id), 过滤(period;app_id、origin_kind、include_rejected、
include_backfill), debounce_s(缺省 60)}。同一个定时任务在 debounce_s 内的几次事件合成一次触发,上传表格按期次分窗口
(window_key,scheduler/outbox)。
**防环。** workflow.finished 能把工作流串起来:A 跑完触发 B、B 跑完又触发 A 就停不下来。保存时查串联成不成环(find_cycle,
停用的任务也算:启用不该绕过这道检查);运行时按 origin 里记的触发链深度兜底,超过 MAX_DEPTH 层不发起、照实记一次未能发起
(手改过库、经别处间接串成环时还有这一道)。
这里不碰调度循环、不发起运行:派发在 scheduler/outbox.py,发起在 scheduler/service.py。
"""
from __future__ import annotations
from dataclasses import dataclass, field
from datetime import date, datetime, timedelta, timezone
from typing import Any
from sqlalchemy import select
#: 事件种类,顺序即界面上的顺序(和 db/models_triggers.EVENT_KINDS 是同一份)
EVENTS = ("table.version", "workflow.finished", "source.changed")
#: 事件种类的叫法(界面另有一份在 lib/termsTrigger.ts,逐字对照)
EVENT_LABEL = {
"table.version": "上传表格出了新的一期",
"workflow.finished": "上游工作流运行成功结束",
"source.changed": "数据源有要处理的变化",
}
#: 每种事件的对象写在 trigger 的哪个键上、叫什么
SUBJECT_KEY = {"table.version": "source_id", "workflow.finished": "workflow_id", "source.changed": "source_id"}
SUBJECT_LABEL = {"table.version": "上传表格", "workflow.finished": "工作流", "source.changed": "数据源"}
DEFAULT_DEBOUNCE_S = 60
MAX_DEBOUNCE_S = 24 * 3600
#: 触发链最多几层:A 触发 B 是第 1 层,B 再触发 C 是第 2 层……超过就不发起
MAX_DEPTH = 5
#: 「期次规则」:any 新增的一期和替换已有的一期都认(缺省);new 只认新增的一期
PERIOD_RULES = ("any", "new")
PERIOD_RULE_LABEL = {"any": "新增的一期和替换已有的一期都认", "new": "只认新增的一期"}
#: 能发起正式运行的来源:workflow.finished 的「只认某种来源」从这几种里选(问数据、画布试跑、评测发起的都是探索运行,
#: 不会有 workflow.finished)
FORMAL_ORIGINS = ("launcher", "schedule", "app_entry", "app_view", "app_backfill", "hook", "api", "ledger_issue")
#: 来源的叫法(同前端 lib/terms.ts 的 ORIGIN_LABEL)
ORIGIN_LABEL = {
"launcher": "手动正式运行", "schedule": "定时任务", "app_entry": "应用入口", "app_view": "应用视图",
"app_backfill": "应用补算", "hook": "入站钩子", "api": "接口调用", "ledger_issue": "按口径正式出具",
}
#: trigger 里认得的键(多出来的拒收:拼错的过滤条件悄悄不生效,比报错更糟)。subject_name 是保存时记下的对象名(接口不给、
#: 带回来也不认,对象删掉之后说法里用它)
_KEYS = {"event", "source_id", "workflow_id", "period", "app_id", "origin_kind", "debounce_s", "subject_name",
"include_rejected", "include_backfill"}
#: 应用补算发起的运行的来源(engine/origin.py)
BACKFILL_ORIGIN = "app_backfill"
#: 示例事件里给不出真实值时写的占位说明
EXAMPLE_NOTE = "示例值"
class TriggerError(ValueError):
"""触发条件写错了、对象不在或不合用。str() 是给人看的中文说明,field 是表单上的那一项(缺省 trigger)。"""
def __init__(self, message: str, field: str = "trigger") -> None:
super().__init__(message)
self.field = field
# --------------------------------------------------------------------------
# 写法
# --------------------------------------------------------------------------
def _text(value: Any) -> str | None:
return value.strip() if isinstance(value, str) and value.strip() else None
def normalize(value: Any) -> dict[str, Any]:
"""校验 trigger 的形状并规范化(不碰库):{event, 对象键, debounce_s, 过滤…},没写的过滤不出现。不对抛 TriggerError。"""
if not isinstance(value, dict):
raise TriggerError("事件触发需要写明事件种类和对象,例如 {event: table.version, source_id: …}")
event = value.get("event")
if event not in EVENTS:
labels = "、".join(EVENT_LABEL[e] for e in EVENTS)
raise TriggerError(f"事件种类需要是{labels}之一")
unknown = sorted(k for k in value if k not in _KEYS)
if unknown:
raise TriggerError(f"事件触发的设置里有不认识的项:{'、'.join(unknown)}")
key = SUBJECT_KEY[event]
subject = _text(value.get(key))
if subject is None:
raise TriggerError(f"请选择{SUBJECT_LABEL[event]}")
for other in {"source_id", "workflow_id"} - {key}:
if _text(value.get(other)):
raise TriggerError(f"「{EVENT_LABEL[event]}」只选{SUBJECT_LABEL[event]},不写别的对象")
out: dict[str, Any] = {"event": event, key: subject}
period = value.get("period")
if period not in (None, ""):
if event != "table.version":
raise TriggerError("只有「上传表格出了新的一期」能按期次过滤")
if period not in PERIOD_RULES:
raise TriggerError("期次规则需要是「新增的一期和替换已有的一期都认」或「只认新增的一期」")
if period != "any":
out["period"] = period
for name in ("app_id", "origin_kind"):
raw = value.get(name)
if raw in (None, ""):
continue
if event != "workflow.finished":
raise TriggerError("只有「上游工作流运行成功结束」能按应用、来源过滤")
text = _text(raw)
if text is None:
raise TriggerError("只认的应用、来源需要从列表里选")
if name == "origin_kind" and text not in FORMAL_ORIGINS:
labels = "、".join(ORIGIN_LABEL[o] for o in FORMAL_ORIGINS)
raise TriggerError(f"只认的来源需要是{labels}之一")
out[name] = text
for name in ("include_rejected", "include_backfill"):
raw = value.get(name)
if raw in (None, False):
continue
if event != "workflow.finished":
raise TriggerError("只有「上游工作流运行成功结束」能选也认途经驳回的、应用补算的")
if raw is not True:
raise TriggerError("也认途经驳回的、应用补算的需要是 true 或 false")
# 只认补算的:补算本来就认,不再另记
if name == "include_backfill" and out.get("origin_kind") == BACKFILL_ORIGIN:
continue
out[name] = True
debounce = value.get("debounce_s", DEFAULT_DEBOUNCE_S)
if isinstance(debounce, bool) or not isinstance(debounce, int) or not 0 <= debounce <= MAX_DEBOUNCE_S:
raise TriggerError(f"去抖需要是 0 到 {MAX_DEBOUNCE_S} 之间的整数秒数(最长 1 天)", "debounce_s")
out["debounce_s"] = debounce
return out
def subject_of(trigger: dict[str, Any] | None) -> str:
"""触发条件里的对象 id;不成形的为空串。"""
trigger = trigger if isinstance(trigger, dict) else {}
key = SUBJECT_KEY.get(trigger.get("event") or "")
return str(trigger.get(key) or "") if key else ""
def debounce_of(trigger: dict[str, Any] | None) -> int:
value = (trigger or {}).get("debounce_s", DEFAULT_DEBOUNCE_S) if isinstance(trigger, dict) else DEFAULT_DEBOUNCE_S
return value if isinstance(value, int) and not isinstance(value, bool) and value >= 0 else DEFAULT_DEBOUNCE_S
def matches(trigger: dict[str, Any] | None, event: str, subject_id: str, payload: dict[str, Any] | None) -> bool:
"""这条事件合不合这个触发条件:种类、对象对得上,过滤条件都满足。"""
if not isinstance(trigger, dict) or trigger.get("event") != event or subject_of(trigger) != subject_id:
return False
payload = payload or {}
if event == "table.version" and trigger.get("period") == "new" and payload.get("replaced"):
return False
if event == "workflow.finished":
if trigger.get("app_id") and payload.get("app_id") != trigger["app_id"]:
return False
if trigger.get("origin_kind") and payload.get("origin_kind") != trigger["origin_kind"]:
return False
if payload.get("rejected") and not trigger.get("include_rejected"):
return False
if payload.get("origin_kind") == BACKFILL_ORIGIN and not (
trigger.get("include_backfill") or trigger.get("origin_kind") == BACKFILL_ORIGIN):
return False
return True
# --------------------------------------------------------------------------
# 核对对象(建任务、改触发条件时)
# --------------------------------------------------------------------------
@dataclass
class Checked:
"""核对通过的触发条件:对象现在的名字,过滤里的应用名(有的话)。"""
subject_name: str
app_name: str | None = None
async def accumulating(session: Any, source: Any) -> bool:
"""这个上传表格是不是按期累积的:当前版本是累积快照,或者现行配方写的是按期累积(还没导入过第二期的也算)。"""
from app.db.models import SourceSnapshot, TableRecipe
if source.current_snapshot_id:
snap = await session.get(SourceSnapshot, source.current_snapshot_id)
if snap is not None and (snap.mode or "replace") == "accumulate":
return True
if source.current_recipe_id:
recipe = await session.get(TableRecipe, source.current_recipe_id)
if recipe is not None and isinstance(recipe.recipe, dict) and recipe.recipe.get("mode") == "accumulate":
return True
return False
async def check(session: Any, trigger: dict[str, Any]) -> Checked:
"""核对触发条件里的对象(trigger 已经 normalize 过):表格在、是按期累积的上传表格;工作流在、已发布(只有正式运行成功
结束才算事件);数据源在;过滤里的应用在。不对抛 TriggerError。"""
from app.db.models import App, DataSource, Workflow
event, subject = trigger["event"], subject_of(trigger)
if event in ("table.version", "source.changed"):
source = await session.get(DataSource, subject)
if source is None:
raise TriggerError(f"{SUBJECT_LABEL[event]}不存在,可能已被删除")
if event == "table.version":
if (source.origin or "manual") != "upload":
raise TriggerError(f"「{source.name}」不是上传表格:只有上传表格有「出了新的一期」")
if not await accumulating(session, source):
raise TriggerError(f"「{source.name}」不是按期累积的上传表格:每次上传替换整张表的没有「新的一期」。"
"请在导入设置里改为按期累积,或改用「数据源有要处理的变化」")
return Checked(source.name)
workflow = await session.get(Workflow, subject)
if workflow is None:
raise TriggerError("上游工作流不存在,可能已被删除")
if not workflow.published_version:
raise TriggerError(f"上游工作流「{workflow.name}」还没有发布版本:只有正式运行成功结束才算事件,"
"正式运行只能从发布版本发起。请先在编排页发布")
app_name = None
if trigger.get("app_id"):
app = await session.get(App, trigger["app_id"])
if app is None:
raise TriggerError("只认的应用不存在,可能已被删除")
app_name = app.name
return Checked(workflow.name, app_name)
async def find_cycle(session: Any, *, schedule_id: str | None, trigger: dict[str, Any],
target_workflow_id: str) -> list[str] | None:
"""workflow.finished 串成的环:这个任务(A 跑完触发 B)加进去之后,从 B 沿着别的事件触发任务能不能回到 A。成环时返回经过的
工作流 id(A, B, …, A),否则 None。停用的任务也算进去:启用不该绕过这道检查;schedule_id 是正在改的那个任务(它原来的
那条边不算)。"""
if trigger.get("event") != "workflow.finished" or not target_workflow_id:
return None
from app.db.models import Schedule
start, target = subject_of(trigger), target_workflow_id
if start == target:
return [start, start]
edges: dict[str, set[str]] = {}
rows = (await session.execute(select(Schedule.id, Schedule.trigger, Schedule.workflow_id, Schedule.target_kind)
.where(Schedule.trigger_kind == "event"))).all()
for sid, other, workflow_id, kind in rows:
if sid == schedule_id or (kind or "workflow") != "workflow" or not isinstance(other, dict):
continue
if other.get("event") == "workflow.finished" and subject_of(other) and workflow_id:
edges.setdefault(subject_of(other), set()).add(workflow_id)
# 从 B 出发找回 A 的路(广度优先,取最短的一条说给人听)
parent: dict[str, str | None] = {target: None}
queue = [target]
while queue:
node = queue.pop(0)
for nxt in sorted(edges.get(node, ())):
if nxt in parent:
continue
parent[nxt] = node
if nxt == start:
back = [start] # A ← … ← B,倒着记
while back[-1] != target:
back.append(parent[back[-1]] or target)
return [start, *reversed(back)]
queue.append(nxt)
return None
def cycle_text(names: list[str]) -> str:
"""成环时的说明(422):「A」→「B」→「A」,箭头说的是「正式运行成功结束后触发」。"""
chain = "→".join(f"「{n}」" for n in names)
return (f"这样设置会循环触发:{chain}(每个箭头表示前一个工作流的正式运行成功结束后触发后一个),"
"运行会一直接力下去。请去掉其中一环")
# --------------------------------------------------------------------------
# 给人看的说法
# --------------------------------------------------------------------------
def describe(trigger: dict[str, Any], subject_name: str | None, *, app_name: str | None = None) -> str:
"""一个事件触发任务的说法(列表、详情页头、预览,同 cron 任务的 description_text):
「上传表格「门店销售」出了新的一期时触发(只认新增的一期;去抖 60 秒)」。对象不在了写保存时记的名字。"""
event = trigger.get("event")
name = f"「{subject_name}」" if subject_name else ""
if event == "table.version":
head = f"上传表格{name}出了新的一期时触发"
elif event == "workflow.finished":
head = f"工作流{name}的正式运行成功结束时触发"
elif event == "source.changed":
head = f"数据源{name}出现要处理的变化时触发"
else:
return "由事件触发(触发条件无法识别)"
notes: list[str] = []
if trigger.get("period") == "new":
notes.append(PERIOD_RULE_LABEL["new"])
if trigger.get("app_id"):
notes.append(f"只认应用「{app_name}」发起的" if app_name else "只认指定的应用发起的")
if trigger.get("origin_kind"):
notes.append(f"只认来源为「{ORIGIN_LABEL.get(trigger['origin_kind'], trigger['origin_kind'])}」的")
if event == "workflow.finished":
# 缺省不认的两种成功结束,照实写出来
kinds = [("途经驳回", bool(trigger.get("include_rejected")))]
if trigger.get("origin_kind") != BACKFILL_ORIGIN:
kinds.append(("应用补算", bool(trigger.get("include_backfill"))))
excluded = "、".join(k for k, on in kinds if not on)
included = "、".join(k for k, on in kinds if on)
notes += [f"{included}的也算"] if included else []
notes += [f"{excluded}的不算"] if excluded else []
debounce = debounce_of(trigger)
from app.scheduler.service import duration_text
notes.append(f"去抖 {duration_text(debounce)}" if debounce else "不去抖")
return f"{head}({';'.join(notes)})"
def event_text(event: str, subject_name: str, payload: dict[str, Any] | None) -> str:
"""一次事件的说法(触发历史、运行来源、通知):「上传表格「门店销售」出了新的一期(2026-09)」。不写出口字段的值、变化的
内容(可能带着数,通知会推进群里)。"""
payload = payload or {}
name = f"「{subject_name}」" if subject_name else ""
if event == "table.version":
period = payload.get("period") or ""
what = "替换了已有的一期" if payload.get("replaced") else "出了新的一期"
return f"上传表格{name}{what}{f'({period})' if period else ''}"
if event == "workflow.finished":
run = str(payload.get("run_id") or "")
rejected = "(途经驳回)" if payload.get("rejected") else ""
return f"工作流{name}的正式运行成功结束{f'(#{run[:6]})' if run else ''}{rejected}"
if event == "source.changed":
n = (payload.get("counts") or {}).get("new")
return f"数据源{name}出现要处理的变化{f'(新记 {n} 条)' if isinstance(n, int) and n else ''}"
return "数据事件"
# --------------------------------------------------------------------------
# 输入模板里的 {{ event.* }}
# --------------------------------------------------------------------------
#: 每种事件能用的变量:(变量名, 说明, 示例)。outputs.<字段>、counts.<项> 之下的名字是开放的,见 allowed_path
VARIABLES: dict[str, tuple[tuple[str, str, str], ...]] = {
"table.version": (
("table", "上传表格的名字", "门店销售"),
("version", "启用的版本编号", "3f2a9c…"),
("period", "这一期(整月写 YYYY-MM,整周写 YYYY-Www,单日写 YYYY-MM-DD)", "2026-09"),
("period_start", "这一期的第一天(YYYY-MM-DD)", "2026-09-01"),
("period_end", "这一期的最后一天(YYYY-MM-DD)", "2026-09-30"),
("activated_at", "启用的时间(带时区偏移的 ISO 时间)", "2026-10-08T09:30:00+08:00"),
),
"workflow.finished": (
("workflow", "上游工作流的名字", "门店日报"),
("workflow_id", "上游工作流的编号", "wf…"),
("run_id", "上游那次运行的编号", "run…"),
("finished_at", "上游运行结束的时间(带时区偏移的 ISO 时间)", "2026-10-08T09:30:00+08:00"),
("rejected", "上游这次运行有没有途经驳回(true / false;缺省不认途经驳回的,勾了「也认」才会是 true)", "false"),
("outputs", "上游运行的出口字段(写 event.outputs.<字段名>)", "{…}"),
),
"source.changed": (
("source", "数据源的名字", "门店库"),
("source_id", "数据源的编号", "ds…"),
("counts", "变化的条数(写 event.counts.new 这次新记的要处理的条数,"
"event.counts.action / warning / info 各级还要人看的条数)", "{…}"),
),
}
#: 开放的那一级:event.outputs.<字段>、event.counts.<项>
_OPEN = {"workflow.finished": "outputs", "source.changed": "counts"}
_COUNT_KEYS = ("new", "action", "warning", "info", "pending")
def names(event: str) -> tuple[str, ...]:
"""这种事件能写在模板里的变量(带 event. 前缀),给报错列出来。"""
out = [f"event.{key}" for key, _, _ in VARIABLES.get(event, ())]
if event == "workflow.finished":
out = [n if n != "event.outputs" else "event.outputs.<字段名>" for n in out]
if event == "source.changed":
out = [n for n in out if n != "event.counts"] + [f"event.counts.{k}" for k in _COUNT_KEYS]
return tuple(out)
def allowed_path(path: str, event: str) -> bool:
"""event.* 的路径合不合这种事件:固定的几个名字,加上 outputs.<字段>(可以再往下取)、counts.<项>。"""
if not path.startswith("event."):
return False
rest = path[len("event."):]
keys = {key for key, _, _ in VARIABLES.get(event, ())}
head, _, tail = rest.partition(".")
if head not in keys:
return False
if head == _OPEN.get(event):
if event == "source.changed":
return not tail or tail in _COUNT_KEYS
return bool(tail.strip())
return not tail
def _zoned(text: Any, tz: str) -> str:
"""存的 UTC ISO 时间换成任务时区、只到秒的 ISO(同 schedule.fire_time 的写法);读不出来的照原样。"""
if not isinstance(text, str) or not text:
return ""
try:
moment = datetime.fromisoformat(text)
except ValueError:
return text
if moment.tzinfo is None:
moment = moment.replace(tzinfo=timezone.utc)
from app.scheduler import cron
try:
return moment.astimezone(cron.zone(tz)).isoformat(timespec="seconds")
except ValueError:
return moment.isoformat(timespec="seconds")
def values(event: str, payload: dict[str, Any] | None, tz: str, *, outputs: dict[str, Any] | None = None,
) -> dict[str, Any]:
"""模板里 event 下面的值(render_inputs 的 ctx["event"])。时间按任务时区写;workflow.finished 的出口字段由派发时读到的
那次运行的成果交进来。"""
payload = payload or {}
if event == "table.version":
return {"table": payload.get("table") or "", "version": payload.get("version") or "",
"period": payload.get("period") or "", "period_start": payload.get("period_start") or "",
"period_end": payload.get("period_end") or "", "activated_at": _zoned(payload.get("activated_at"), tz)}
if event == "workflow.finished":
return {"workflow": payload.get("workflow") or "", "workflow_id": payload.get("workflow_id") or "",
"run_id": payload.get("run_id") or "", "finished_at": _zoned(payload.get("finished_at"), tz),
"rejected": "true" if payload.get("rejected") else "false", "outputs": dict(outputs or {})}
if event == "source.changed":
counts = payload.get("counts") if isinstance(payload.get("counts"), dict) else {}
return {"source": payload.get("source") or "", "source_id": payload.get("source_id") or "",
"counts": {k: int(counts.get(k) or 0) for k in _COUNT_KEYS}}
return {}
def variables(event: str) -> list[dict[str, str]]:
"""变量清单接口里 event.* 的那几项:[{name, label, example, note}]。"""
out = []
for key, label, example in VARIABLES.get(event, ()):
name = "event.outputs.<字段名>" if (event, key) == ("workflow.finished", "outputs") else f"event.{key}"
if (event, key) == ("source.changed", "counts"):
name = "event.counts.new"
out.append({"name": name, "label": label, "example": example, "note": ""})
return out
# --------------------------------------------------------------------------
# 期次的写法
# --------------------------------------------------------------------------
def period_label(start: str | None, end: str | None) -> str:
"""这一期写成一个词:整月 2026-09、整季 2026-Q3、整年 2026、ISO 整周 2026-W40、单日 2026-09-30;别的写「起~止」。"""
if not start:
return ""
if not end or end == start:
return start
try:
d0, d1 = date.fromisoformat(start), date.fromisoformat(end)
except ValueError:
return f"{start}~{end}"
last_of_month = (d1 + timedelta(days=1)).day == 1
if d0.day == 1 and last_of_month:
if (d0.year, d0.month) == (d1.year, d1.month):
return f"{d0.year:04d}-{d0.month:02d}"
if d0.year == d1.year and d0.month == 1 and d1.month == 12:
return f"{d0.year:04d}"
if d0.year == d1.year and d0.month in (1, 4, 7, 10) and d1.month == d0.month + 2:
return f"{d0.year:04d}-Q{(d0.month - 1) // 3 + 1}"
if d0.weekday() == 0 and d1 - d0 == timedelta(days=6):
year, week, _ = d0.isocalendar()
return f"{year}-W{week:02d}"
return f"{start}~{end}"
# --------------------------------------------------------------------------
# 示例事件(预览、立即运行)
# --------------------------------------------------------------------------
@dataclass
class Sample:
"""按对象现在的情况凑出的一次事件:real 为假时是示例值(对象还没发生过这种事件),立即运行不拿它发起用到 event.* 的运行。"""
event: str
subject_id: str
subject_name: str
payload: dict[str, Any]
outputs: dict[str, Any] = field(default_factory=dict)
real: bool = True
async def sample(session: Any, trigger: dict[str, Any]) -> Sample:
"""预览、立即运行用的事件:上传表格取当前版本里最近的一期,上游工作流取最近一次成功的正式运行,数据源取现在的变化计数。
对象不在、还没有过这种事件时给示例值(real=False)。"""
from app.db.models import DataSource, Run, SourceSnapshot, TableImport, Workflow
event, subject = trigger.get("event") or "", subject_of(trigger)
if event in ("table.version", "source.changed"):
source = await session.get(DataSource, subject) if subject else None
name = source.name if source is not None else ""
if event == "source.changed":
if source is None:
return Sample(event, subject, name, {"source": name or EXAMPLE_NOTE, "source_id": subject,
"counts": {"new": 2, "action": 2}}, real=False)
from app.data import source_changes
got = await source_changes.counts(session, subject)
by = got.get("by_severity") or {}
return Sample(event, subject, name, {"source": name, "source_id": subject, "counts": {
"new": int(by.get("action") or 0), "action": int(by.get("action") or 0),
"warning": int(by.get("warning") or 0), "info": int(by.get("info") or 0),
"pending": int(got.get("pending") or 0)}})
snap = await session.get(SourceSnapshot, source.current_snapshot_id) \
if source is not None and source.current_snapshot_id else None
imports = list((await session.execute(select(TableImport).where(TableImport.id.in_(
[str(x) for x in snap.imports or []])))).scalars()) if snap is not None else []
latest = max(imports, key=lambda i: (i.period_end or "", i.seq or 0), default=None)
if snap is None or latest is None:
return Sample(event, subject, name, {"table": name or EXAMPLE_NOTE, "version": "", "period": "2026-09",
"period_start": "2026-09-01", "period_end": "2026-09-30",
"activated_at": ""}, real=False)
return Sample(event, subject, name, table_payload(
name, snapshot_id=snap.id, period_start=latest.period_start, period_end=latest.period_end,
activated_at=snap.activated_at or snap.created_at, replaced=False, seq=latest.seq))
workflow = await session.get(Workflow, subject) if subject else None
name = workflow.name if workflow is not None else ""
query = select(Run).where(Run.workflow_id == subject, Run.run_class == "formal", Run.status == "succeeded")
if trigger.get("app_id"):
query = query.where(Run.app_id == trigger["app_id"])
if trigger.get("origin_kind"):
query = query.where(Run.origin_kind == trigger["origin_kind"])
candidates = list((await session.execute(query.order_by(Run.finished_at.desc()).limit(SAMPLE_SCAN)))
.scalars()) if subject else []
# 和真派发同一套条件(matches):缺省不认的途经驳回、应用补算跳过,取合条件的最近一次
for run in candidates:
payload = run_payload(run, await _rejected_via_of(session, run.id))
if matches(trigger, event, subject, payload):
return Sample(event, subject, name, payload, outputs=outputs_of(run.output))
return Sample(event, subject, name, {"workflow": name or EXAMPLE_NOTE, "workflow_id": subject, "run_id": "",
"finished_at": "", "rejected": False}, real=False)
#: 示例事件往回找几次成功的正式运行(找合条件的最近一次)
SAMPLE_SCAN = 20
async def _rejected_via_of(session: Any, run_id: str) -> list[Any] | None:
"""这次运行途经的驳回:run.finished 事件里的 rejected_via(W1-b 的驳回出口写的),没有为 None。"""
from app.db.models import RunEvent
data = (await session.execute(select(RunEvent.data).where(RunEvent.run_id == run_id, RunEvent.type == "run.finished")
.order_by(RunEvent.seq.desc()).limit(1))).scalar()
via = data.get("rejected_via") if isinstance(data, dict) else None
return list(via) if isinstance(via, list) and via else None
def outputs_of(output: Any) -> dict[str, Any]:
"""给模板的出口字段({{ event.outputs.* }}):「_」开头的是系统写的(出具档位之类),不给。真派发、预览、立即运行共用。"""
return {k: v for k, v in output.items() if not str(k).startswith("_")} if isinstance(output, dict) else {}
def table_payload(name: str, *, snapshot_id: str, period_start: str | None, period_end: str | None,
activated_at: datetime | None, replaced: bool, seq: int | None) -> dict[str, Any]:
"""table.version 的事件内容(发生处和示例共用一份写法)。"""
return {"table": name, "version": snapshot_id, "period": period_label(period_start, period_end),
"period_start": period_start or "", "period_end": period_end or "",
"activated_at": _iso(activated_at), "replaced": bool(replaced), "seq": seq}
def run_payload(run: Any, rejected_via: Any = None) -> dict[str, Any]:
"""workflow.finished 的事件内容:不含出口字段(派发时按 run_id 读成果)。rejected_via 是途经了哪几个关卡的驳回(W1-b 的
驳回出口;runner 收尾时交进来),没途经驳回为 None。"""
via = (list(rejected_via) if isinstance(rejected_via, (list, tuple)) else [rejected_via]) if rejected_via else None
return {"workflow": run.workflow_name or "", "workflow_id": run.workflow_id or "", "run_id": run.id,
"finished_at": _iso(run.finished_at), "app_id": run.app_id, "origin_kind": run.origin_kind,
"version": run.version, "rejected": bool(via), "rejected_via": via}
def window_key(event: str, payload: dict[str, Any] | None) -> str:
"""去抖时按什么分窗口:上传表格按期次分(9 月、10 月连着启用,各有各的触发,不把 9 月合掉),别的不分。"""
if event != "table.version":
return ""
payload = payload or {}
return str(payload.get("period") or f"{payload.get('period_start') or ''}~{payload.get('period_end') or ''}")
def _iso(moment: datetime | None) -> str:
if moment is None:
return ""
if moment.tzinfo is None:
moment = moment.replace(tzinfo=timezone.utc)
return moment.astimezone(timezone.utc).isoformat()
def uses_event(template: dict[str, str] | None) -> bool:
"""输入模板里有没有用到 {{ event.* }}(立即运行拿示例值时据此决定能不能发起)。"""
from app.scheduler.service import _TEMPLATE_RE
for text in (template or {}).values():
for match in _TEMPLATE_RE.finditer(text if isinstance(text, str) else ""):
if match.group(1).split("|")[0].strip().startswith("event."):
return True
return False
+14 -1
View File
@@ -119,6 +119,8 @@ class Notice:
#: 运行结束在 cancelled 的原因(Run.end_reason):rejected 时按「未出具(被驳回)」说,error_node 是驳回的那一步
end_reason: str | None = None
next_fire: bool = False
#: 事件触发的那一次(fire_kind=event):事件的说法(scheduler/triggers.event_text),不含出口字段的值、变化的内容
trigger_text: str | None = None
compare: dict[str, Any] | None = None
compare_url: str | None = None
fmt: str | None = None
@@ -195,6 +197,9 @@ def message(notice: Notice) -> tuple[str, list[tuple[str, str]], str | None]:
# 立即运行不写「计划触发」:群里的人会以为报告按时出了
if notice.fire_kind == "manual":
lines.append(("手动触发", when))
elif notice.fire_kind == "event":
# 事件触发没有「计划」:写事件和合成这次触发的时刻(去抖结束的那一刻)
lines.append(("事件触发", f"{notice.trigger_text},{when}" if notice.trigger_text else when))
elif notice.fire_kind == "misfire":
lines.append(("计划触发", f"{when},错过后补发"))
else:
@@ -549,10 +554,18 @@ async def _notice(session: Any, schedule: Schedule, fire: ScheduleFire, run: Run
error_node=_failed_node(run) if run is not None and (run.status == "failed" or run.end_reason == "rejected")
else None,
end_reason=run.end_reason if run is not None else None,
next_fire=bool(schedule.enabled and schedule.next_run_at is not None),
# 事件触发的任务没有下一次「到点」,启用着就是下一次事件来了照常发起
next_fire=bool(schedule.enabled and (schedule.next_run_at is not None or schedule.trigger_kind == "event")),
trigger_text=_trigger_text(fire),
)
def _trigger_text(fire: ScheduleFire) -> str | None:
event = fire.inputs.get("event") if isinstance(fire.inputs, dict) else None
text = event.get("text") if isinstance(event, dict) else None
return text if isinstance(text, str) and text else None
def _failed_node(run: Run) -> str | None:
"""失败节点在图上的名字(没有名字的给节点 id)。节点名是搭图时起的,不带查询到的数据。"""
if not run.error_node_id: