You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/v3/errors-retrying.mdx
+59-22Lines changed: 59 additions & 22 deletions
Original file line number
Diff line number
Diff line change
@@ -1,10 +1,8 @@
1
1
---
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."
4
4
---
5
5
6
-
## Task attempts and retrying
7
-
8
6
When an uncaught error is thrown inside your task, that task attempt will fail.
9
7
10
8
You can configure retrying in two ways:
@@ -14,26 +12,17 @@ You can configure retrying in two ways:
14
12
15
13
<Note>
16
14
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).
18
16
</Note>
19
17
20
-
Here's a contrived example showing a task that will retry 10 times with exponential backoff:
18
+
## A simple example with OpenAI
21
19
22
-
```ts /trigger/retrying.ts
23
-
exportconst 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
-
thrownewError(`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
+
<Snippetfile="v3/code/openai-retry.mdx" />
37
26
38
27
## Combining tasks
39
28
@@ -72,7 +61,7 @@ Another complimentary strategy is to perform retrying inside of your task.
72
61
73
62
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.
74
63
75
-
### `retry.onThrow()`
64
+
### retry.onThrow()
76
65
77
66
You can retry a block of code that can throw an error, with the same retry settings as a task.
If all of the attempts with `retry.fetch` fail, an error will be thrown. You can catch this or let
177
166
it cause a retry of the entire task.
178
167
</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.
0 commit comments