Skip to content

Commit c0f9266

Browse files
committed
New combined error and retrying page
1 parent bf74ffb commit c0f9266

File tree

5 files changed

+68
-89
lines changed

5 files changed

+68
-89
lines changed

docs/mint.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -473,8 +473,7 @@
473473
"pages": [
474474
"v3/writing-tasks-introduction",
475475
"v3/logging",
476-
"v3/errors",
477-
"v3/retrying",
476+
"v3/errors-retrying",
478477
{
479478
"group": "Wait",
480479
"pages": [
@@ -492,7 +491,7 @@
492491
},
493492
"v3/idempotency",
494493
"v3/versioning",
495-
"v3/replaying",
494+
"v3/reattempting-replaying",
496495
"v3/trigger-filters",
497496
"v3/notifications",
498497
"v3/rollbacks",

docs/v3/retrying.mdx renamed to docs/v3/errors-retrying.mdx

Lines changed: 59 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
---
2-
title: "Retrying"
3-
description: "You can configure entire tasks to retry and you can retry smaller parts of a task."
2+
title: "Errors & Retrying"
3+
description: "How to deal with errors and write reliable tasks."
44
---
55

6-
## Task attempts and retrying
7-
86
When an uncaught error is thrown inside your task, that task attempt will fail.
97

108
You can configure retrying in two ways:
@@ -14,26 +12,17 @@ You can configure retrying in two ways:
1412

1513
<Note>
1614
By default when you create your project using the CLI init command we disabled retrying in the DEV
17-
environment.
15+
environment. You can enable it in your [trigger.config file](/v3/reference-trigger-config).
1816
</Note>
1917

20-
Here's a contrived example showing a task that will retry 10 times with exponential backoff:
18+
## A simple example with OpenAI
2119

22-
```ts /trigger/retrying.ts
23-
export const taskWithRetries = task({
24-
id: "task-with-retries",
25-
retry: {
26-
maxAttempts: 10,
27-
factor: 1.8,
28-
minTimeoutInMs: 500,
29-
maxTimeoutInMs: 30_000,
30-
randomize: false,
31-
},
32-
run: async (payload, { ctx }) => {
33-
throw new Error(`This is attempt ${ctx.attempt.number}. It will fail :(`);
34-
},
35-
});
36-
```
20+
This task will retry 10 times with exponential backoff.
21+
22+
- `openai.chat.completions.create()` can throw an error.
23+
- The result can be empty and we want to try again. So we manually throw an error.
24+
25+
<Snippet file="v3/code/openai-retry.mdx" />
3726

3827
## Combining tasks
3928

@@ -72,7 +61,7 @@ Another complimentary strategy is to perform retrying inside of your task.
7261

7362
We provide some useful functions that you can use to retry smaller parts of a task. Of course, you can also write your own logic or use other packages.
7463

75-
### `retry.onThrow()`
64+
### retry.onThrow()
7665

7766
You can retry a block of code that can throw an error, with the same retry settings as a task.
7867

@@ -176,3 +165,51 @@ export const taskWithFetchRetries = task({
176165
If all of the attempts with `retry.fetch` fail, an error will be thrown. You can catch this or let
177166
it cause a retry of the entire task.
178167
</Note>
168+
169+
## Handling errors and deciding the retry behavior
170+
171+
## Using try/catch to prevent retries
172+
173+
Sometimes you want to catch an error and don't want to retry the task. You can use try/catch as you normally would. In this example we fallback to using Replicate if OpenAI fails.
174+
175+
```ts /trigger/
176+
import { task } from "@trigger.dev/sdk/v3";
177+
178+
export const openaiTask = task({
179+
id: "openai-task",
180+
run: async (payload: { prompt: string }) => {
181+
try {
182+
//if this fails, it will throw an error and retry
183+
const chatCompletion = await openai.chat.completions.create({
184+
messages: [{ role: "user", content: payload.prompt }],
185+
model: "gpt-3.5-turbo",
186+
});
187+
188+
if (chatCompletion.choices[0]?.message.content === undefined) {
189+
//sometimes OpenAI returns an empty response, let's retry by throwing an error
190+
throw new Error("OpenAI call failed");
191+
}
192+
193+
return chatCompletion.choices[0].message.content;
194+
} catch (error) {
195+
//use Replicate if OpenAI fails
196+
const prediction = await replicate.run(
197+
"meta/llama-2-70b-chat:02e509c789964a7ea8736978a43525956ef40397be9033abf9fd2badfe68c9e3",
198+
{
199+
input: {
200+
prompt: payload.prompt,
201+
max_new_tokens: 250,
202+
},
203+
}
204+
);
205+
206+
if (prediction.output === undefined) {
207+
//retry if Replicate fails
208+
throw new Error("Replicate call failed");
209+
}
210+
211+
return prediction.output;
212+
}
213+
},
214+
});
215+
```

docs/v3/errors.mdx

Lines changed: 0 additions & 63 deletions
This file was deleted.

docs/v3/reattempting-replaying.mdx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
title: "Reattempting & Replaying"
3+
description: "You can reattempt a task that has failed all of its attempts. You can also replay a task with a new version of your code."
4+
---
5+
6+
<Snippet file="coming-soon.mdx" />

docs/v3/rollbacks.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ title: "Rollbacks"
33
description: "You can rollback changes when errors happen, to give transactional guarantees to your operations."
44
---
55

6-
<Snippet file="incomplete-docs.mdx" />
6+
<Snippet file="coming-soon.mdx" />

0 commit comments

Comments
 (0)