They are defined using the task()
function and can be triggered from your backend or inside another task.
Like all tasks they don’t have timeouts, they should be placed inside a /trigger folder, and you can configure them.
Example tasks
A task that does an OpenAI call with retrying
Sometimes OpenAI calls can take a long time to complete, or they can fail. This task will retry if the API call fails completely or if the response is empty.
import { task } from "@trigger.dev/sdk/v3";
import OpenAI from "openai";
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
});
export const openaiTask = task({
id: "openai-task",
retry: {
maxAttempts: 10,
factor: 1.8,
minTimeoutInMs: 500,
maxTimeoutInMs: 30_000,
randomize: false,
},
run: async (payload: { prompt: string }) => {
const chatCompletion = await openai.chat.completions.create({
messages: [{ role: "user", content: payload.prompt }],
model: "gpt-3.5-turbo",
});
if (chatCompletion.choices[0]?.message.content === undefined) {
throw new Error("OpenAI call failed");
}
return chatCompletion.choices[0].message.content;
},
});
A Task that sends emails in a sequence with delays in between
This example uses Resend to send a sequence of emails over several days.
Each email is wrapped in retry.onThrow
. This will retry the block of code if an error is thrown. This is useful when you don’t want to retry the whole task, but just a part of it. The entire task will use the default retrying, so can also retry.
Additionally this task uses wait.for
to wait for a certain amount of time before sending the next email. During the waiting time, the task will be paused and will not consume any resources.
/trigger/email-sequence.ts
import { Resend } from "resend";
const resend = new Resend(process.env.RESEND_ASP_KEY);
export const emailSequence = task({
id: "email-sequence",
run: async (payload: { userId: string; email: string; name: string }) => {
console.log(`Start email sequence for user ${payload.userId}`, payload);
const firstEmailResult = await retry.onThrow(
async ({ attempt }) => {
const { data, error } = await resend.emails.send({
from: "[email protected]",
to: payload.email,
subject: "Welcome to Trigger.dev",
html: `<p>Hello ${payload.name},</p><p>Welcome to Trigger.dev</p>`,
});
if (error) {
throw error;
}
return data;
},
{ maxAttempts: 3 }
);
await wait.for({ days: 3 });
const secondEmailResult = await retry.onThrow(
async ({ attempt }) => {
const { data, error } = await resend.emails.send({
from: "[email protected]",
to: payload.email,
subject: "Some tips for you",
html: `<p>Hello ${payload.name},</p><p>Here are some tips for you…</p>`,
});
if (error) {
throw error;
}
return data;
},
{ maxAttempts: 3 }
);
},
});