如何把 Mastra workflows 部署到 Inngest 获得步骤记忆化与自动重试
发布时间:2026/9/13 14:08:03
分类:文化教育
浏览:1234

如何把 Mastra workflows 部署到 Inngest 获得步骤记忆化与自动重试【免费下载链接】mastraMastra is the modern TypeScript framework for AI-powered applications and agents.项目地址: https://gitcode.com/GitHub_Trending/ma/mastra如果你在本地用 Mastra 内置 runner 跑 workflow步骤结果依赖进程内存进程一旦崩溃就要从头再来。把 workflow 部署到 Inngest 之后编排、重试和状态记忆交给 Inngest 平台每个 Mastra workflow 变成一个带唯一标识的 Inngest function每个createStep映射为一个 Inngest step事件触发后 Inngest 逐步执行 workflow 并记忆化memoize每个步骤的结果重试或恢复时直接跳过已完成的步骤。前提条件inngest^4SDK、Inngest Dev Serverv1.18.0或更高版本。v4 的 SDK 已内置 Realtime不再需要inngest/realtime和realtimeMiddleware。安装依赖npm install mastra/inngestlatest inngest初始化 Inngest 客户端开发环境指向本地 Dev Serverimport { Inngest } from inngest export const inngest new Inngest({ id: mastra, baseUrl: http://localhost:8288, isDev: true, })生产环境去掉isDev和baseUrlimport { Inngest } from inngest export const inngest new Inngest({ id: mastra, })也可以不写死模式用INNGEST_DEV环境变量让同一份客户端代码跨环境工作INNGEST_DEV1强制 dev 模式SDK 连接本地 Inngest Dev Server 并关闭签名校验INNGEST_DEV0强制 cloud 模式连接 Inngest Cloud需要 event key 和 signing keyINNGEST_DEVurldev 模式并指定 Dev Server 地址例如http://localhost:8288不设置默认 cloud 模式v4 SDK 默认 cloud 模式需要INNGEST_EVENT_KEY和INNGEST_SIGNING_KEY。设置INNGEST_DEV后可以从客户端代码里删掉isDev和baseUrl。注意生产环境不要设置INNGEST_DEV它会关闭接收 Inngest Cloud 事件所必需的签名校验。若同时设置了isDev和INNGEST_DEVisDev优先生效。创建步骤与 workflowinit(inngest)返回与 Mastra 和 Inngest 双兼容的createWorkflow/createStep。以下示例是一个计数器 workflow用于后续验证与文档示例一致import { z } from zod import { inngest } from ../inngest import { init } from mastra/inngest // Initialize Inngest with Mastra, pointing to your local Inngest server const { createWorkflow, createStep } init(inngest) // Step: Increment the counter value const incrementStep createStep({ id: increment, inputSchema: z.object({ value: z.number(), }), outputSchema: z.object({ value: z.number(), }), execute: async ({ inputData }) { return { value: inputData.value 1 } }, }) // workflow that is registered as a function on inngest server const workflow createWorkflow({ id: increment-workflow, inputSchema: z.object({ value: z.number(), }), outputSchema: z.object({ value: z.number(), }), }).then(incrementStep) workflow.commit() export { workflow as incrementWorkflow }在 Mastra 实例上注册并暴露 serve 端点serve()负责把 Mastra workflows 注册为 Inngest functions并建立执行与监控所需的事件处理。自定义路由路径是/inngest/api不是/api/inngestMastra 把/api前缀保留给内置路由agents、workflows、memory以默认apiPrefix/api开头的自定义apiRoutes路径会在启动时直接抛错。如果你必须保留/api/inngest比如匹配 Inngest 的 auto-discover 约定见文末改用 /api/inngest。import { Mastra } from mastra/core import { serve } from mastra/inngest import { incrementWorkflow } from ./workflows import { inngest } from ./inngest import { PinoLogger } from mastra/loggers export const mastra new Mastra({ workflows: { incrementWorkflow }, server: { host: 0.0.0.0, apiRoutes: [ { path: /inngest/api, method: ALL, createHandler: async ({ mastra }) { return serve({ mastra, inngest }) }, }, ], }, logger: new PinoLogger({ name: Mastra, level: info }), })本地运行并验证记忆化与重试行为启动 Mastra 服务器默认监听 4111 端口npx mastra dev新开终端启动 Inngest Dev Server-u参数告诉 Dev Server 到哪里找你的/inngest/api端点npx inngest-clilatest dev -u http://localhost:4111/inngest/api打开 Inngest Dashboardhttp://localhost:8288在侧边栏Apps区确认你的 Mastra workflow 已注册。在Functions里打开 workflow选择Invoke输入{ data: { inputData: { value: 5 } } }在Runs页签监控执行可以看到逐步骤的执行进度每个步骤执行时的状态与输出都会被跟踪必要时可以 resume。重试或恢复时Inngest 依据已保存的 memoization 结果跳过已完成步骤——这就是部署到 Inngest 后 workflow 获得的 durable 行为。部署到生产文档给出的生产路径是部署到 Vercel。开始前的清单Vercel 账号并已安装 Vercel CLInpm i -g vercelInngest 账号Vercel token。设置 Vercel tokenexport VERCEL_TOKENyour_vercel_token在 Mastra 实例中添加VercelDeployeryour_team_slug、your_project_name替换为你自己的团队和项目import { VercelDeployer } from mastra/deployer-vercel export const mastra new Mastra({ deployer: new VercelDeployer({ teamSlug: your_team_slug, projectName: your_project_name, // you can get your vercel token from the vercel dashboard by clicking on the user icon in the top right corner // and then clicking on Account Settings and then clicking on Tokens on the left sidebar. token: process.env.VERCEL_TOKEN, }), })构建npx mastra build部署cd .mastra/output vercel login vercel --prod在 Inngest dashboard 中选择Sync new app with Vercel并按提示完成同步。这里有一个容易踩的坑Inngest 的 auto-discover 约定假设路径是/api/inngest而本指南用的是/inngest/api所以必须把 Inngest app 的URL字段手动设为部署域名加/inngest/api例如https://your-app.vercel.app/inngest/api。保持默认值的话dashboard 将找不到你的 app functions。在Functions中打开workflow.increment-workflow选择All actionsInvoke输入与本地相同的 JSON 载荷然后在Runs页签确认步骤级执行进度。备选用 connect() 跑长驻 worker如果你运行在无公网入口的私有网络Kubernetes、Docker、ECS、Fly.io、Render 等长驻进程不想暴露 HTTP 端点可以用connect()建立 worker 到 Inngest 的出站长连接Mastra server 在这种情况下不需要暴露/inngest/api。要求inngest^4、Node.js22.13.0及以上、一个长驻进程。注意 Inngest Connect 目前处于 public betaVercel、AWS Lambda 这类 serverless 运行时不支持 Connect worker这类环境请继续用serve()import { connect } from mastra/inngest/connect import { mastra } from ./mastra import { inngest } from ./mastra/inngest await connect({ mastra, inngest, instanceId: process.env.INNGEST_CONNECT_INSTANCE_ID, maxWorkerConcurrency: Number(process.env.INNGEST_CONNECT_MAX_WORKER_CONCURRENCY ?? 10), })本地开发用INNGEST_DEV1 node --import tsx src/worker.ts启动 workerDev Server 通过出向连接发现它不需要-u参数。生产设置INNGEST_EVENT_KEY和INNGEST_SIGNING_KEY并在Inngest客户端上把appVersion设为部署标识commit SHA 或镜像 tag便于 Inngest 管理滚动部署。从serve()迁移到connect()的存量生产应用文档建议先用一个独立的 Inngest app 测试 worker再切流量。改用 /api/inngest自定义 apiPrefix如果一定要保留/api/inngest路径以匹配 Inngest 的 auto-discover 约定就把server.apiPrefix改掉把 Mastra 内置路由挪走import { Mastra } from mastra/core import { serve } from mastra/inngest import { inngest } from ./inngest export const mastra new Mastra({ server: { apiPrefix: /_mastra, apiRoutes: [ { path: /api/inngest, method: ALL, createHandler: async ({ mastra }) serve({ mastra, inngest }), }, ], }, })改完后内置路由落在/_mastra/agents、/_mastra/workflows等处/api/inngest空出来给自定义路由。注意默认 auth 配置保护的是/api/*并把/api、/api/auth/*视为公开改apiPrefix后这些默认值不再匹配需要同步更新server.auth.protected、server.auth.public以及所有访问/api/*的客户端代码包括MastraClient的apiPrefix。参考资料Inngest 部署指南全文含 Express / Fastify / Koa / Next.js 适配器和 flow control 配置InngestWorkflow runners 总览Inngest 与 Temporal 的取舍Workflow runners完整示例含 observability、cron、flow control、durable agentsexamples/inngest/README.md【免费下载链接】mastraMastra is the modern TypeScript framework for AI-powered applications and agents.项目地址: https://gitcode.com/GitHub_Trending/ma/mastra创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考