Skip to content

常见模式配方 ​

这一页是"照着改就能用"的配方集。每个配方给完整 DSL(节点 + 边),并说明它解决什么问题、改哪里适配你的场景。 基础语法见工作流总览,字段细节见节点手册。

约定 ​

  • 所有配方都是路由 workflow 字段的值(裸 spec 形态);
  • ch-* 是占位的渠道 ID,替换成你实际创建的渠道;
  • 提交前先 POST /v1/admin/workflow/validate,再 POST /v1/admin/routes/{id}/dry-run。

配方 1:按优先级分级分流 ​

问题:紧急的走短信,其余走 IM;两条路都不能因为失败而静默。

json
{
  "trigger": { "type": "api" },
  "nodes": [
    { "id": "c1", "type": "condition", "with": { "expr": "priority == 'urgent' || payload.severity == 'P1'" } },
    { "id": "s_sms", "type": "send", "with": { "channel": "ch-sms" } },
    { "id": "s_im", "type": "send", "with": { "channel": "ch-dingtalk" } },
    { "id": "h1", "type": "error-handler" },
    { "id": "s_fallback", "type": "send", "with": { "channel": "ch-email-ops" } }
  ],
  "edges": [
    { "from": "c1", "to": "s_sms", "when": "true" },
    { "from": "c1", "to": "s_im", "when": "false" },
    { "from": "s_sms", "to": "h1", "when": "error" },
    { "from": "s_im", "to": "h1", "when": "error" },
    { "from": "h1", "to": "s_fallback" }
  ]
}

要点:error 边只能指向 error-handler;兜底链在 handler 之后继续画。h1 的输出包含 error 与 failed_node,可以用模板把失败原因带进兜底消息。

改哪里:把 c1.with.expr 换成你的判定;兜底渠道换成"一定会到达"的那个(邮件通常比短信更可靠)。

配方 2:多路分支 ​

问题:按告警级别走三条不同的通道组合。

json
{
  "nodes": [
    { "id": "sw1", "type": "switch", "with": { "expr": "payload.severity" } },
    { "id": "s_p0", "type": "send", "with": { "channel": "ch-phone" } },
    { "id": "s_p1", "type": "send", "with": { "channel": "ch-sms" } },
    { "id": "s_p2", "type": "send", "with": { "channel": "ch-dingtalk" } }
  ],
  "edges": [
    { "from": "sw1", "to": "s_p0", "when": "P0" },
    { "from": "sw1", "to": "s_p1", "when": "P1" },
    { "from": "sw1", "to": "s_p2", "when": "default" }
  ]
}

要点:switch 的出边标签就是取值本身(字符串相等);一定要有 default 兜底,否则未匹配的运行会走"无入边激活 → 跳过"的路径,静默什么都不发。

配方 3:降级链(前一个失败才发下一个) ​

问题:短信最贵最直接,失败后换电话,再失败才发邮件;不想三条都发。

json
{
  "nodes": [
    { "id": "s_sms", "type": "send", "with": { "channel": "ch-sms", "await": true } },
    { "id": "h1", "type": "error-handler" },
    { "id": "s_call", "type": "send", "with": { "channel": "ch-voice", "await": true } },
    { "id": "h2", "type": "error-handler" },
    { "id": "s_mail", "type": "send", "with": { "channel": "ch-email-ops" } }
  ],
  "edges": [
    { "from": "s_sms", "to": "h1", "when": "error" },
    { "from": "h1", "to": "s_call" },
    { "from": "s_call", "to": "h2", "when": "error" },
    { "from": "h2", "to": "s_mail" }
  ]
}

要点:

  • await: true 让 send 真正等到投递终态,失败才会触发 error 边;不写 await 时 send 只是"写进 outbox 就算成功",降级链不会生效;
  • 最后一环不写 await(已是最后兜底,没必要等)。

简单场景优先用策略

如果只是"按顺序尝试已绑定的渠道",路由策略 failover 更省事(不用画图),见投递策略规范。

配方 4:定数送达(3/4 成功就算成功) ​

问题:同一条告警发 4 个渠道,只要 3 个到达就收工,别为最后一个苦等。

json
{
  "nodes": [
    { "id": "f1", "type": "fanout" },
    { "id": "s1", "type": "send", "with": { "channel": "ch-sms", "await": true } },
    { "id": "s2", "type": "send", "with": { "channel": "ch-dingtalk", "await": true } },
    { "id": "s3", "type": "send", "with": { "channel": "ch-feishu", "await": true } },
    { "id": "s4", "type": "send", "with": { "channel": "ch-email-ops", "await": true } },
    { "id": "m1", "type": "merge", "with": { "k": 3 } }
  ],
  "edges": [
    { "from": "f1", "to": "s1" },
    { "from": "f1", "to": "s2" },
    { "from": "f1", "to": "s3" },
    { "from": "f1", "to": "s4" },
    { "from": "s1", "to": "m1" },
    { "from": "s2", "to": "m1" },
    { "from": "s3", "to": "m1" },
    { "from": "s4", "to": "m1" }
  ]
}

要点:

  • merge.k=3 达成后立即成功,未开始的纯分支节点被标 skipped(省掉多余发送);已在途的不撤回;
  • 失败分支被 merge 吸收,不会直接把运行判死;
  • 全部终态仍不足 3 个成功 → merge 失败,可给它接 error 边;
  • merge 的输出是 {"succeeded": [...], "failed": [...], "k": 3, "total": 4},兜底时可以直接引用。

配方 5:重试直到成功(最多 N 次) ​

问题:第三方网关不稳定,失败就等 1 分钟再试,最多 5 次,成功即跳出。

json
{
  "limits": { "deadline": "1h" },
  "nodes": [
    { "id": "l1", "type": "loop", "with": { "count": 5, "maxIterations": 5, "body": ["s1", "h1", "d1", "br1"] } },
    { "id": "s1", "type": "send", "with": { "channel": "ch-flaky-gateway", "await": true } },
    { "id": "h1", "type": "error-handler" },
    { "id": "d1", "type": "delay", "with": { "duration": "1m" } },
    { "id": "br1", "type": "break", "with": { "expr": "nodes.s1.output != nil" } }
  ],
  "edges": [
    { "from": "l1", "to": "s1", "when": "body" },
    { "from": "s1", "to": "br1" },
    { "from": "s1", "to": "h1", "when": "error" },
    { "from": "h1", "to": "d1" },
    { "from": "d1", "to": "br1" },
    { "from": "br1", "to": "l1", "when": "body" }
  ]
}

要点:

  • loop.count 是"按次数迭代",item 为 0..N-1;
  • 成功的路径直接到 br1,nodes.s1.output != nil 成立 → 跳出;
  • 失败的路径经 h1 兜底、d1 等一分钟,再进 br1(此时 s1 没有输出,条件为假 → 继续下一轮);
  • body 边的回连表示"迭代收尾";
  • 等待时间计入 limits.deadline:5 轮 × 1m 的等待必须放进 deadline(这里给 1h)。

配方 6:夜间静默,早上补发 ​

问题:22:00–08:00 不打扰,但消息不能丢——早上 8 点统一补发。

json
{
  "limits": { "deadline": "24h" },
  "nodes": [
    { "id": "c1", "type": "condition", "with": { "expr": "payload.night_ok == true" } },
    { "id": "w1", "type": "wait-until", "with": { "daily": "08:00", "timezone": "Asia/Shanghai" } },
    { "id": "s_night", "type": "send", "with": { "channel": "ch-email-ops" } },
    { "id": "s_now", "type": "send", "with": { "channel": "ch-dingtalk" } }
  ],
  "edges": [
    { "from": "c1", "to": "s_now", "when": "true" },
    { "from": "c1", "to": "w1", "when": "false" },
    { "from": "w1", "to": "s_night" }
  ]
}

要点:

  • wait-until.daily 取下一次该时刻:如果现在是 09:00,它会等到明天 08:00;
  • 跨天等待必须放大 limits.deadline(默认 10m 会直接失败);
  • 更省事的做法是策略层 quiet_hours(窗口内挂起、窗口结束放行),只有"需要换渠道/换内容"时才用工作流。

配方 7:逐条处理,最后合并一条发出去 ​

问题:一个告警涉及多个服务,想逐条处理(查状态、算等级),但只发一条汇总消息,而不是刷屏。

json
{
  "limits": { "maxLoopIterations": 50 },
  "nodes": [
    { "id": "l1", "type": "loop", "with": { "over": "payload.services", "as": "svc", "maxIterations": 50, "body": ["t1", "sc1"] } },
    { "id": "t1", "type": "template", "with": { "template": "- {{ .svc.name }}:{{ .svc.status }}({{ .svc.latency_ms }}ms)" } },
    { "id": "sc1", "type": "script", "with": {
      "apply": true,
      "source": "const line = nodes.t1.text; const prev = payload.lines || []; return { payload: { ...payload, lines: [...prev, line] } };"
    } },
    { "id": "s1", "type": "send", "with": { "channel": "ch-dingtalk" } }
  ],
  "edges": [
    { "from": "l1", "to": "t1", "when": "body" },
    { "from": "t1", "to": "sc1" },
    { "from": "sc1", "to": "l1", "when": "body" },
    { "from": "l1", "to": "s1", "when": "done" }
  ]
}

要点:

  • loop.over 求值出列表,每次迭代把当前项绑定到 as 指定的名字(缺省 item);
  • script.apply 把每一行的渲染结果累加进消息 payload(payload.lines),循环结束后的 send 发出的是最终 payload;
  • 用 done 边接住循环结束再发送,避免每轮都发一条;
  • 循环体内节点按(节点, 迭代)落记录,中断后从当前迭代续跑;
  • 出站内容的真相:send.with.template 只把渲染结果写进节点输出的 text(用于运行记录/预览),不改变实际发出的内容——实际发出的是消息记录里的 payload。所以要改文案,用 script.apply 改 payload,或把内容放在通知请求的 payload 里。
  • 循环次数受 maxIterations 与 limits.maxLoopIterations 双重限制。

配方 8:把多次告警压成一条摘要 ​

问题:一分钟内 30 条同源告警,只想收到一条。

这不是工作流,而是发送侧的 digest 字段:

json
{
  "route_id": "ops-alert",
  "payload": { "alert_name": "db_replica_lag", "title": "主从延迟 42s" },
  "digest": { "window": "5m", "key": "payload.alert_name" }
}

窗口内首条即时发,后续挂起,窗口结束合并成一条摘要。详见投递策略规范。

组合建议 ​

你想表达用什么不要用
按值分多路switch一串嵌套 condition
只要一个成功策略 quorum 或 merge.k三个 send 串起来
按顺序备用策略 failover手画 error 链(除非要在链上做数据加工)
等一段时间delay空转 loop
等到某个钟点wait-untildelay 算秒数
失败重试send(await) + error-handler + delay + loop/break依赖队列重试做业务重试(它只重协议失败)

下一步 ​

基于 Apache License 2.0 发布