trigger-dev-tasks by triggerdotdev/trigger.dev
npx skills add https://github.com/triggerdotdev/trigger.dev --skill trigger-dev-tasks您是一位专业的 Trigger.dev 开发者,专注于构建生产级的后台作业系统。部署到 Trigger.dev 的任务运行在 Node.js 21+ 环境中,并使用 @trigger.dev/sdk 包。
@trigger.dev/sdk - 切勿使用 @trigger.dev/sdk/v3 或已弃用的 client.defineJob 模式node-fetch - 使用内置的 fetch 函数triggerAndWait、batchTriggerAndWait 和 调用不能被包装在 或 中广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
wait.*Promise.allPromise.allSettledimport { task } from "@trigger.dev/sdk";
export const processData = task({
id: "process-data",
retry: {
maxAttempts: 10,
factor: 1.8,
minTimeoutInMs: 500,
maxTimeoutInMs: 30_000,
},
run: async (payload: { userId: string; data: any[] }) => {
console.log(`Processing ${payload.data.length} items`);
return { processed: payload.data.length };
},
});
import { schemaTask } from "@trigger.dev/sdk";
import { z } from "zod";
export const validatedTask = schemaTask({
id: "validated-task",
schema: z.object({
name: z.string(),
email: z.string().email(),
}),
run: async (payload) => {
// 负载会自动验证并具有类型
return { message: `Hello ${payload.name}` };
},
});
import { tasks } from "@trigger.dev/sdk";
import type { processData } from "./trigger/tasks";
const handle = await tasks.trigger<typeof processData>("process-data", {
userId: "123",
data: [{ id: 1 }],
});
export const parentTask = task({
id: "parent-task",
run: async (payload) => {
// 触发并等待 - 返回 Result 对象,而非直接输出
const result = await childTask.triggerAndWait({ data: "value" });
if (result.ok) {
console.log("Output:", result.output);
} else {
console.error("Failed:", result.error);
}
// 或者直接解包(出错时抛出异常)
const output = await childTask.triggerAndWait({ data: "value" }).unwrap();
},
});
从其他任务内部触发任务时,始终使用幂等性密钥:
import { idempotencyKeys } from "@trigger.dev/sdk";
export const paymentTask = task({
id: "process-payment",
run: async (payload: { orderId: string }) => {
// 限定在当前运行范围内 - 在重试中持续存在
const key = await idempotencyKeys.create(`payment-${payload.orderId}`);
await chargeCustomer.trigger(payload, {
idempotencyKey: key,
idempotencyKeyTTL: "24h",
});
},
});
await myTask.trigger(payload, {
delay: "1h", // 延迟执行
ttl: "10m", // 如果在 TTL 内未启动则取消
idempotencyKey: key,
queue: "my-queue",
machine: "large-1x", // micro, small-1x, small-2x, medium-1x, medium-2x, large-1x, large-2x
maxAttempts: 3,
tags: ["user_123"], // 最多 10 个标签
debounce: { // 合并快速触发
key: "unique-key",
delay: "5s",
mode: "trailing", // "leading"(默认)或 "trailing"
},
});
将多次触发合并为单次执行:
// 具有相同密钥的快速触发 = 单次执行
await myTask.trigger({ userId: "123" }, {
debounce: {
key: "user-123-update",
delay: "5s",
},
});
// 尾部模式:使用最后一次触发的负载
await myTask.trigger({ data: "latest" }, {
debounce: {
key: "my-key",
delay: "10s",
mode: "trailing",
},
});
使用场景:用户活动更新、Webhook 去重、搜索索引、通知批处理。
每批最多 1,000 项,每个负载最大 3MB:
const results = await myTask.batchTriggerAndWait([
{ payload: { userId: "1" } },
{ payload: { userId: "2" } },
]);
for (const result of results) {
if (result.ok) console.log(result.output);
}
| 预设 | vCPU | 内存 |
|---|---|---|
| micro | 0.25 | 0.25GB |
| small-1x | 0.5 | 0.5GB |
| small-2x | 1 | 1GB |
| medium-1x | 1 | 2GB |
| medium-2x | 2 | 4GB |
| large-1x | 4 | 8GB |
| large-2x | 8 | 16GB |
Promise.allSettled 比使用多个子任务更好(每个任务都有专用进程并按毫秒计费)maxAttemptslogger.info()、logger.error() 等进行记录有关特定主题的详细文档,请阅读以下文件:
basic-tasks.md - 任务基础、触发、等待advanced-tasks.md - 标签、队列、并发、元数据、错误处理scheduled-tasks.md - Cron 计划、声明式和命令式realtime.md - 实时订阅、流、React 钩子config.md - trigger.config.ts、构建扩展(Prisma、Playwright、FFmpeg 等)每周安装量
115
仓库
GitHub 星标数
14.2K
首次出现
2026 年 1 月 24 日
安全审计
安装于
opencode102
cursor98
claude-code98
github-copilot95
gemini-cli94
codex94
You are an expert Trigger.dev developer specializing in building production-grade background job systems. Tasks deployed to Trigger.dev run in Node.js 21+ and use the @trigger.dev/sdk package.
@trigger.dev/sdk - Never use @trigger.dev/sdk/v3 or deprecated client.defineJob patternnode-fetch - Use the built-in fetch functiontriggerAndWait, batchTriggerAndWait, and wait.* calls cannot be wrapped in Promise.all or Promise.allSettledimport { task } from "@trigger.dev/sdk";
export const processData = task({
id: "process-data",
retry: {
maxAttempts: 10,
factor: 1.8,
minTimeoutInMs: 500,
maxTimeoutInMs: 30_000,
},
run: async (payload: { userId: string; data: any[] }) => {
console.log(`Processing ${payload.data.length} items`);
return { processed: payload.data.length };
},
});
import { schemaTask } from "@trigger.dev/sdk";
import { z } from "zod";
export const validatedTask = schemaTask({
id: "validated-task",
schema: z.object({
name: z.string(),
email: z.string().email(),
}),
run: async (payload) => {
// Payload is automatically validated and typed
return { message: `Hello ${payload.name}` };
},
});
import { tasks } from "@trigger.dev/sdk";
import type { processData } from "./trigger/tasks";
const handle = await tasks.trigger<typeof processData>("process-data", {
userId: "123",
data: [{ id: 1 }],
});
export const parentTask = task({
id: "parent-task",
run: async (payload) => {
// Trigger and wait - returns Result object, NOT direct output
const result = await childTask.triggerAndWait({ data: "value" });
if (result.ok) {
console.log("Output:", result.output);
} else {
console.error("Failed:", result.error);
}
// Or unwrap directly (throws on error)
const output = await childTask.triggerAndWait({ data: "value" }).unwrap();
},
});
Always use idempotency keys when triggering tasks from inside other tasks:
import { idempotencyKeys } from "@trigger.dev/sdk";
export const paymentTask = task({
id: "process-payment",
run: async (payload: { orderId: string }) => {
// Scoped to current run - survives retries
const key = await idempotencyKeys.create(`payment-${payload.orderId}`);
await chargeCustomer.trigger(payload, {
idempotencyKey: key,
idempotencyKeyTTL: "24h",
});
},
});
await myTask.trigger(payload, {
delay: "1h", // Delay execution
ttl: "10m", // Cancel if not started within TTL
idempotencyKey: key,
queue: "my-queue",
machine: "large-1x", // micro, small-1x, small-2x, medium-1x, medium-2x, large-1x, large-2x
maxAttempts: 3,
tags: ["user_123"], // Max 10 tags
debounce: { // Consolidate rapid triggers
key: "unique-key",
delay: "5s",
mode: "trailing", // "leading" (default) or "trailing"
},
});
Consolidate multiple triggers into a single execution:
// Rapid triggers with same key = single execution
await myTask.trigger({ userId: "123" }, {
debounce: {
key: "user-123-update",
delay: "5s",
},
});
// Trailing mode: use payload from LAST trigger
await myTask.trigger({ data: "latest" }, {
debounce: {
key: "my-key",
delay: "10s",
mode: "trailing",
},
});
Use cases: user activity updates, webhook deduplication, search indexing, notification batching.
Up to 1,000 items per batch, 3MB per payload:
const results = await myTask.batchTriggerAndWait([
{ payload: { userId: "1" } },
{ payload: { userId: "2" } },
]);
for (const result of results) {
if (result.ok) console.log(result.output);
}
| Preset | vCPU | Memory |
|---|---|---|
| micro | 0.25 | 0.25GB |
| small-1x | 0.5 | 0.5GB |
| small-2x | 1 | 1GB |
| medium-1x | 1 | 2GB |
| medium-2x | 2 | 4GB |
| large-1x | 4 | 8GB |
| large-2x | 8 | 16GB |
Promise.allSettled inside a single task is better than many subtasks (each task has dedicated process and is charged by millisecond)maxAttempts based on the operationlogger.info(), logger.error(), etc.For detailed documentation on specific topics, read these files:
basic-tasks.md - Task basics, triggering, waitsadvanced-tasks.md - Tags, queues, concurrency, metadata, error handlingscheduled-tasks.md - Cron schedules, declarative and imperativerealtime.md - Real-time subscriptions, streams, React hooksconfig.md - trigger.config.ts, build extensions (Prisma, Playwright, FFmpeg, etc.)Weekly Installs
115
Repository
GitHub Stars
14.2K
First Seen
Jan 24, 2026
Security Audits
Gen Agent Trust HubFailSocketPassSnykWarn
Installed on
opencode102
cursor98
claude-code98
github-copilot95
gemini-cli94
codex94
Azure Data Explorer (Kusto) 查询技能:KQL数据分析、日志遥测与时间序列处理
138,800 周安装