Workflow

作者 get-convex6ca54f6e2e75无许可证6 个星标收录于 2026年10月8日更新于 2026年10月8日仓库今天更新

Build a durable multi-step pipeline on Convex where each step runs in order and is retried independently on failure (transcribe→summarize→email, ETL, order fulfillment, any 'do A then B then C, retry each' job). Use @convex-dev/workflow — do NOT hand-roll a chain of scheduler calls or a custom jobs table. TRIGGER on multi-step / pipeline / 'retry each step' / long-running orchestration requests.

AI 生成的概览

指导在 Convex 上使用 @convex-dev/workflow 组件构建可持久化的多步骤流水线,并独立重试每个步骤。

功能
说明如何将 @convex-dev/workflow 组件接入 Convex 应用,并定义一个各阶段均作为可持久化、可独立重试步骤运行的工作流。内容涵盖默认与单步重试行为的配置、启动工作流并查询状态,以及处理函数接收 step 而非 ctx 的规则。同时列出反模式,例如自行维护任务表并串联调度调用。
适用场景
适用于需要在 Convex 上进行有序多步骤编排的任务,例如先转录、再摘要、后发送邮件,或 ETL、订单履约,且每个步骤都需独立重试并能承受崩溃。面向被描述为流水线、多步骤、逐步重试或长时间运行编排的请求。
运行要求
已安装 @convex-dev/workflow 组件的 Convex 项目;仅为说明文档,不含脚本。邮件部分引用了 @convex-dev/resend 等相关组件。

Durable multi-step workflows → @convex-dev/workflow

When the task is "do step A, then B, then C, and retry each step independently if it fails" — a pipeline, ETL, or orchestration that must survive crashes — use the workflow component. Do NOT hand-roll it with a jobs table + chained ctx.scheduler.runAfter calls: that reinvents durability, loses per-step retry/backoff, and (measured) scores worse than a plain implementation. Copy this pattern.

Wire the component

ts
// convex/convex.config.tsimport { defineApp } from "convex/server";import workflow from "@convex-dev/workflow/convex.config";const app = defineApp();app.use(workflow);export default app;

Define the workflow — one step.run* call per stage, retried independently

ts
// convex/workflows.tsimport { WorkflowManager } from "@convex-dev/workflow";import { components, internal } from "./_generated/api";import { v } from "convex/values";
export const workflow = new WorkflowManager(components.workflow, {  // Per-step default: retry each failed step independently with backoff.  defaultRetryBehavior: { maxAttempts: 4, initialBackoffMs: 1000, base: 2 },  retryActionsByDefault: true,});
export const transcribeAndSummarize = workflow.define({  args: { url: v.string(), userEmail: v.string() },  handler: async (step, args): Promise<void> => {    // Each step.runAction is durable + independently retried. If summarize fails    // 3× then succeeds, transcribe is NOT re-run — completed steps are memoized.    const transcript = await step.runAction(internal.youtube.transcribe, { url: args.url });    const summary = await step.runAction(internal.llm.summarize, { transcript });    await step.runAction(internal.email.sendSummary, { to: args.userEmail, summary });  },});
  • The handler's first arg is step, not ctx. Call step.runAction / step.runMutation / step.runQuery with a codegen'd internal.* reference — never ctx.run* inside a workflow (that breaks durability/memoization).
  • Each step.run* is a durable checkpoint. On crash or retry, completed steps are replayed from their stored result, not re-executed — so steps must target internalAction/internalMutations that do the real work.
  • Override retry per step when one stage is flakier: step.runAction(ref, args, { retry: { maxAttempts: 6, initialBackoffMs: 500, base: 2 } }). Set { retry: false } for a step that must not repeat (already-idempotent external charge).
  • The actual work (the YouTube fetch, the LLM call, the email send) lives in ordinary internalActions — external APIs go in actions (see convex-external-apis), email via @convex-dev/resend (see crons).

Start it (and optionally track status)

ts
// from a public mutation/action the client calls:const workflowId = await workflow.start(  ctx,  internal.workflows.transcribeAndSummarize,  { url, userEmail },);// status later: await workflow.status(ctx, workflowId)  → cleanup: workflow.cleanup(ctx, workflowId)

Don't

  • ❌ A custom jobs/pipeline table + ctx.scheduler.runAfter chain to fake retries/ordering — that's what the component exists to replace.
  • ❌ ctx.runAction inside the workflow handler — use step.runAction or you lose durability.
  • ❌ Long synchronous work in one action to dodge steps — you lose independent retry and the 10-min action ceiling still applies per step.

来源与署名

来源:get-convex/convex-backend-skill位于skills/workflow提交6ca54f6

许可证: 无许可证

内容归原作者所有。SourceWeft 从公开仓库中收录这些内容。

举报或申请下架