@convex-dev/action-retrier

中文文档
0.3.1发布于 2026/06/25Apache-2.0TypeScriptES Module零依赖近 30 天下载 121.9万

Convex component for retrying idempotent actions.

原始使用文档
文档目录8 节
  1. Pre-requisite: Convex
  2. Installation
  3. API
  4. Starting a run
  5. Run status
  6. Canceling a run
  7. Cleaning up completed runs
  8. Logging

Convex Action Retrier

npm version

Actions can sometimes fail due to network errors, server restarts, or issues with a 3rd party API, and it's often useful to retry them. The Action Retrier component makes this really easy.

import { ActionRetrier } from "@convex-dev/action-retrier";
import { components } from "./convex/_generated/server";

const retrier = new ActionRetrier(components.actionRetrier);

// `retrier.run` will automatically retry your action up to four times before giving up.
await retrier.run(ctx, internal.module.myAction, { arg: 123 });

The retrier component will run the action and retry it on failure, sleeping with exponential backoff, until the action succeeds or the maximum number of retries is reached.

Pre-requisite: Convex

You'll need an existing Convex project to use the component. Convex is a hosted backend platform, including a database, serverless functions, and a ton more you can learn about here.

Run npm create convex or follow any of the quickstarts to set one up.

Installation

First, add @convex-dev/action-retrier as an NPM dependency:

npm install @convex-dev/action-retrier

Then, install the component into your Convex project within the convex/convex.config.ts configuration file:

// convex/convex.config.ts
import { defineApp } from "convex/server";
import actionRetrier from "@convex-dev/action-retrier/convex.config.js";

const app = defineApp();
app.use(actionRetrier);
export default app;

Finally, create a new ActionRetrier within your Convex project, and point it to the installed component:

// convex/index.ts
import { ActionRetrier } from "@convex-dev/action-retrier";
import { components } from "./_generated/api";

export const retrier = new ActionRetrier(components.actionRetrier);

You can optionally configure the retrier's backoff behavior in the ActionRetrier constructor.

const retrier = new ActionRetrier(components.actionRetrier, {
  initialBackoffMs: 10000,
  base: 10,
  maxFailures: 4,
});
  • initialBackoffMs is the initial delay after a failure before retrying (default: 250).
  • base is the base for the exponential backoff (default: 2).
  • maxFailures is the maximum number of times to retry the action (default: 4).

API

Starting a run

After installing the component, use the run method from either a mutation or action to kick off an action.

export const kickoffExampleAction = mutation({
  handler: async (ctx) => {
    const runId = await retrier.run(ctx, internal.index.exampleAction, {
      foo: "bar",
    });
    // ... optionally persist or pass along the runId
  },
});

export const exampleAction = internalAction({
  args: { foo: v.string() },
  handler: async (ctx, args) => {
    return operationThatMightFail(args);
  },
});

The return value of retrier.run is not the result of the action, but rather an ID that you can use to query its status or cancel it. The action's return value is saved along with the status, when it succeeds.

You can optionally specify overrides to the backoff parameters in an options argument.

export const kickoffExampleAction = action(async (ctx) => {
  const runId = await retrier.run(
    ctx,
    internal.index.exampleAction,
    { failureRate: 0.8 },
    {
      initialBackoffMs: 125,
      base: 2.71,
      maxFailures: 3,
    },
  );
});

You can specify an onComplete mutation callback in the options argument as well. This mutation is guaranteed to eventually run exactly once.

// convex/index.ts

import { runResultValidator, runIdValidator } from "@convex-dev/action-retrier";

export const kickoffExampleAction = action(async (ctx) => {
  const runId = await retrier.run(
    ctx,
    internal.index.exampleAction,
    { failureRate: 0.8 },
    {
      onComplete: internal.index.exampleCallback,
    },
  );
});

export const exampleCallback = internalMutation({
  args: { id: runIdValidator, result: runResultValidator },
  handler: async (ctx, args) => {
    if (args.result.type === "success") {
      console.log(
        "Action succeeded with return value:",
        args.result.returnValue,
      );
    } else if (args.result.type === "failed") {
      console.log("Action failed with error:", args.result.error);
    } else if (args.result.type === "canceled") {
      console.log("Action was canceled.");
    }
  },
});

Run status

The run method returns a RunId, which can then be used for querying a run's status.

export const kickoffExampleAction = action(async (ctx) => {
  const runId = await retrier.run(ctx, internal.index.exampleAction, {
    failureRate: 0.8,
  });
  while (true) {
    const status = await retrier.status(ctx, runId);
    if (status.type === "inProgress") {
      await new Promise((resolve) => setTimeout(resolve, 1000));
      continue;
    } else {
      console.log("Run completed with result:", status.result);
      break;
    }
  }
});

Canceling a run

You can cancel a run using the cancel method.

export const kickoffExampleAction = action(async (ctx) => {
  const runId = await retrier.run(ctx, internal.index.exampleAction, {
    failureRate: 0.8,
  });
  await new Promise((resolve) => setTimeout(resolve, 1000));
  await retrier.cancel(ctx, runId);
});

Runs that are currently executing will be canceled best effort, so they may still continue to execute. A succcesful call to cancel, however, does guarantee that subsequent status calls will indicate cancelation.

Cleaning up completed runs

Runs take up space in the database, since they store their return values. After a run completes, you can immediately clean up its storage by using retrier.cleanup(ctx, runId). The system will automatically cleanup completed runs after 7 days.

export const kickoffExampleAction = action(async (ctx) => {
  const runId = await retrier.run(ctx, internal.index.exampleAction, {
    failureRate: 0.8,
  });
  try {
    while (true) {
      const status = await retrier.status(ctx, runId);
      if (status.type === "inProgress") {
        await new Promise((resolve) => setTimeout(resolve, 1000));
        continue;
      } else {
        console.log("Run completed with result:", status.result);
        break;
      }
    }
  } finally {
    await retrier.cleanup(ctx, runId);
  }
});

Logging

You can set the ACTION_RETRIER_LOG_LEVEL to DEBUG to have the retrier log out more of its internal information, which you can then view on the Convex dashboard.

npx convex env set ACTION_RETRIER_LOG_LEVEL DEBUG

The default log level is INFO, but you can also set it to ERROR for even fewer logs.

版本与发布记录

@convex-dev/action-retrier 共发布 11 个版本,首次发布于 2024/09/20;过去 12 个月发布了 2 个版本。

版本发布日期距今
0.3.1latest2026/06/254 个月前
0.3.02025/11/1111 个月前
0.2.12025/05/311.4 年前
0.2.02025/02/191.7 年前
0.1.7-alpha.1alpha2025/02/151.7 年前
0.1.7-alpha.02025/02/151.7 年前

关于 @convex-dev/action-retrier 的常见问题

@convex-dev/action-retrier 是什么?

Convex component for retrying idempotent actions.

如何安装 @convex-dev/action-retrier?

在项目目录运行 npm install @convex-dev/action-retrier,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:convex(^1.24.8)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@convex-dev/action-retrier 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@convex-dev/action-retrier 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@convex-dev/action-retrier 可以商用吗?

@convex-dev/action-retrier 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@convex-dev/action-retrier 的下载量有多少?

根据 npm 官方下载统计,@convex-dev/action-retrier 在 2026/10/11 – 2026/11/09 这 30 天内被下载 1,218,917 次,日均约 40,631 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@convex-dev/action-retrier 还在维护吗?

最新版本 0.3.1 发布于 2026/06/25(4 个月前);至今共发布 11 个版本,其中过去 12 个月发布了 2 个;首次发布于 2024/09/20。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。