Files
triggerdotdev--trigger.dev/docs/tasks-regular.mdx
T
James Ritchie 6270abaab8 Remove v2 docs pages (#1246)
* Make the tooltip text color grey so it’s readable again the primary color

* Setup and styling for the Guides section

* Better Guides icon

* Improved the prerequisites

* WIP Nextjs guide and new folder structures

* Copy updates

* Useful next steps is now a snippet

* Better icons for the next steps section

* Renamed the “prerequisites” snippet

* Node.js guide

* Added a “Creating a project” guide

* New snippet for prerequisites

* New Remix guide

* Added Remix to the side bar

* Tweaked icons for creating a project page

* Added the hello world step to the onboarding steps

* Better “useful next steps” snippet card links

* Moved prerequisites

* Fixing links to images

* Moved v2 migration page to guides

* Removed dead link

* Attempt fix for redirect

* Fixed redirects

* Getting started section includes link to roadmap

* Removed icon from side menu

* Deleted all v2 pages (excluding updating mint.json)

* Removed v2 pages, redirects and versions from mint.json

* Deleted v2 snippets

* Deleted un-used pages

* Set of more useful coming soon snippets

* All snippets use the updated format

* updated folder “v3/“ with “/pages”

* Moved all main docs files to the route and updated the redirect

* Fixed URLs in the mdx pages to the new route path

* URL goes to the proper pricing page

* Better delayed runs image

* Attempt fix for self hosting page not redirecting
2024-08-07 11:13:45 +01:00

85 lines
2.8 KiB
Plaintext

---
title: "Regular tasks"
description: "The simplest type of task which can be triggered from elsewhere in your code."
---
import OpenaiRetry from "/snippets/code/openai-retry.mdx"
They are defined using the `task()` function and can be [triggered](/triggering) from your backend or inside another task.
Like all tasks they don't have timeouts, they should be placed inside a [/trigger folder](/trigger-folder), and you [can configure them](/tasks-overview#defining-a-task).
## 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.
<OpenaiRetry />
### 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.
```ts /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);
//send the first email immediately
const firstEmailResult = await retry.onThrow(
async ({ attempt }) => {
const { data, error } = await resend.emails.send({
from: "hello@trigger.dev",
to: payload.email,
subject: "Welcome to Trigger.dev",
html: `<p>Hello ${payload.name},</p><p>Welcome to Trigger.dev</p>`,
});
if (error) {
//throwing an error will trigger a retry of this block
throw error;
}
return data;
},
{ maxAttempts: 3 }
);
//then wait 3 days
await wait.for({ days: 3 });
//send the second email
const secondEmailResult = await retry.onThrow(
async ({ attempt }) => {
const { data, error } = await resend.emails.send({
from: "hello@trigger.dev",
to: payload.email,
subject: "Some tips for you",
html: `<p>Hello ${payload.name},</p><p>Here are some tips for you…</p>`,
});
if (error) {
//throwing an error will trigger a retry of this block
throw error;
}
return data;
},
{ maxAttempts: 3 }
);
//etc...
},
});
```