Skip to content

Commit 6cd8870

Browse files
committed
Added docs
1 parent 54e76ce commit 6cd8870

File tree

3 files changed

+153
-8
lines changed

3 files changed

+153
-8
lines changed

docs/docs.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,13 @@
4747
"errors-retrying",
4848
{
4949
"group": "Wait",
50-
"pages": ["wait", "wait-for", "wait-until", "wait-for-token"]
50+
"pages": [
51+
"wait",
52+
"wait-for",
53+
"wait-until",
54+
"wait-for-token",
55+
"wait-for-http-callback"
56+
]
5157
},
5258
"queue-concurrency",
5359
"versioning",

docs/wait-for-http-callback.mdx

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
---
2+
title: "Wait for HTTP callback"
3+
description: "Pause runs until an HTTP callback is made to the provided URL."
4+
---
5+
6+
import UpgradeToV4Note from "/snippets/upgrade-to-v4-note.mdx";
7+
8+
The `wait.forHttpCallback()` function gives you a callback URL, and then pauses the run until that callback is hit. This is most commonly used with 3rd party APIs that take a long time and that accept a callback (or webhook) URL.
9+
10+
When the callback URL is requested the run will continue where it left off with the body of the request as the output available for you to use.
11+
12+
<UpgradeToV4Note />
13+
14+
## Usage
15+
16+
In this example we create an image using Replicate. Their API accepts a "webhook", which is a callback.
17+
18+
```ts
19+
import { logger, task, wait } from "@trigger.dev/sdk";
20+
21+
const replicate = new Replicate({
22+
auth: process.env.REPLICATE_API_KEY,
23+
});
24+
25+
export const replicate = task({
26+
id: "replicate",
27+
run: async () => {
28+
// This will pause the run and give you a URL
29+
const result = await wait.forHttpCallback<Prediction>(
30+
async (url) => {
31+
// 👆 This URL continues your run when hit with a POST request
32+
33+
// Make the call to Replicate, passing in the URL
34+
await replicate.predictions.create({
35+
version: "27b93a2413e7f36cd83da926f3656280b2931564ff050bf9575f1fdf9bcd7478",
36+
input: {
37+
prompt: "A painting of a cat by Any Warhol",
38+
},
39+
// Make sure to pass the callback URL
40+
// 👇
41+
webhook: url,
42+
// We only want to call when it's completed
43+
webhook_events_filter: ["completed"],
44+
});
45+
},
46+
{
47+
// We'll fail the waitpoint after 10m of inactivity
48+
timeout: "10m",
49+
}
50+
);
51+
52+
if (!result.ok) {
53+
throw new Error("Failed to create prediction");
54+
}
55+
56+
logger.log("Result", result);
57+
58+
const imageUrl = result.output.output;
59+
logger.log("Image URL", imageUrl);
60+
},
61+
});
62+
```
63+
64+
## unwrap()
65+
66+
We provide a handy `.unwrap()` method that will throw an error if the result is not ok. This means your happy path is a lot cleaner.
67+
68+
```ts
69+
const prediction = await wait
70+
.forHttpCallback<Prediction>(
71+
async (url) => {
72+
// ...
73+
},
74+
{
75+
timeout: "10m",
76+
}
77+
)
78+
.unwrap();
79+
// 👆 This will throw an error if the waitpoint times out
80+
81+
// This is the actual data you sent to the callback now, not a result object
82+
logger.log("Prediction", prediction);
83+
```
84+
85+
### Options
86+
87+
The `wait.forHttpCallback` function accepts an optional second parameter with the following properties:
88+
89+
<ParamField query="timeout" type="string" optional>
90+
The maximum amount of time to wait for the token to be completed.
91+
</ParamField>
92+
93+
<ParamField query="idempotencyKey" type="string" optional>
94+
An idempotency key for the token. If provided, the token will be completed with the same output if
95+
the same idempotency key is used again.
96+
</ParamField>
97+
98+
<ParamField query="idempotencyKeyTTL" type="string" optional>
99+
The time to live for the idempotency key. After this time, the idempotency key will be ignored and
100+
can be reused to create a new waitpoint.
101+
</ParamField>
102+
103+
<ParamField query="tags" type="string[]" optional>
104+
Tags to attach to the token. Tags can be used to filter waitpoints in the dashboard.
105+
</ParamField>
106+
107+
<ParamField query="releaseConcurrency" type="boolean" optional>
108+
If set to true, this will cause the waitpoint to release the current run from the queue's concurrency.
109+
110+
This is useful if you want to allow other runs to execute while waiting
111+
112+
Note: It's possible that this run will not be able to resume when the waitpoint is complete if this is set to true.
113+
It will go back in the queue and will resume once concurrency becomes available.
114+
115+
The default is `false`.
116+
117+
</ParamField>
118+
119+
### returns
120+
121+
The `forHttpCallback` function returns a result object with the following properties:
122+
123+
<ParamField query="ok" type="boolean">
124+
Whether the token was completed successfully.
125+
</ParamField>
126+
127+
<ParamField query="output" type="any">
128+
If `ok` is `true`, this will be the output of the token.
129+
</ParamField>
130+
131+
<ParamField query="error" type="Error">
132+
If `ok` is `false`, this will be the error that occurred. The only error that can occur is a
133+
timeout error.
134+
</ParamField>
135+
136+
### unwrap() returns
137+
138+
If you use the `unwrap()` method, it will just return the output of the token. If an error occurs it will throw an error.

docs/wait.mdx

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ sidebarTitle: "Overview"
44
description: "During your run you can wait for a period of time or for something to happen."
55
---
66

7-
import PausedExecutionFree from "/snippets/paused-execution-free.mdx"
7+
import PausedExecutionFree from "/snippets/paused-execution-free.mdx";
88

9-
Waiting allows you to write complex tasks as a set of async code, without having to scheduled another task or poll for changes.
9+
Waiting allows you to write complex tasks as a set of async code, without having to schedule another task or poll for changes.
1010

1111
<PausedExecutionFree />
1212

13-
| Function | What it does |
14-
| :--------------------------------------| :---------------------------------------------------------------------------------------- |
15-
| [wait.for()](/wait-for) | Waits for a specific period of time, e.g. 1 day. |
16-
| [wait.until()](/wait-until) | Waits until the provided `Date`. |
17-
| [wait.forToken()](/wait-for-token) | Pauses task runs until a token is completed. |
13+
| Function | What it does |
14+
| :------------------------------------------------ | :------------------------------------------------------------- |
15+
| [wait.for()](/wait-for) | Waits for a specific period of time, e.g. 1 day. |
16+
| [wait.until()](/wait-until) | Waits until the provided `Date`. |
17+
| [wait.forToken()](/wait-for-token) | Pauses runs until a token is completed. |
18+
| [wait.forHttpCallback()](/wait-for-http-callback) | Pause runs until an HTTP callback is made to the provided URL. |

0 commit comments

Comments
 (0)