Skip to content

Commit 8ddd151

Browse files
committed
Re-ordered runTask params in docs, and example Jobs in the app.
1 parent 9947383 commit 8ddd151

File tree

19 files changed

+321
-218
lines changed

19 files changed

+321
-218
lines changed

apps/webapp/app/components/integrations/CustomHelp.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,16 @@ client.defineJob({
2525
//wrap an SDK call in io.runTask so it's resumable and displays in logs
2626
const repo = await io.runTask(
2727
"Get repo",
28-
//you can add metadata to the task to improve the display in the logs
29-
{ name: "Get repo", icon: "github" },
3028
async () => {
3129
//this is the regular GitHub SDK
3230
const response = await octokit.rest.repos.get({
3331
owner: "triggerdotdev",
3432
repo: "trigger.dev",
3533
});
3634
return response.data;
37-
}
35+
},
36+
//you can add metadata to the task to improve the display in the logs
37+
{ name: "Get repo", icon: "github" }
3838
);
3939
},
4040
});
@@ -60,13 +60,13 @@ client.defineJob({
6060
//wrap anything in io.runTask so it's resumable and displays in logs
6161
const repo = await io.runTask(
6262
"Get org",
63-
//you can add metadata to the task to improve the display in the logs
64-
{ name: "Get org", icon: "github" },
6563
async () => {
6664
//you can use fetch, axios, or any other library to make requests
6765
const response = await fetch('https://api.github.com/orgs/nodejs');
6866
return response.json();
69-
}
67+
},
68+
//you can add metadata to the task to improve the display in the logs
69+
{ name: "Get org", icon: "github" }
7070
);
7171
},
7272
});

apps/webapp/app/services/externalApis/integrations/github.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,14 @@ const usageSample: HelpSample = {
4343
//wrap the SDK call in runTask
4444
const { data } = await io.runTask(
4545
"create-card",
46-
{ name: "Create card" },
4746
async () => {
4847
//create a project card using the underlying client
4948
return io.github.client.rest.projects.createCard({
5049
column_id: 123,
5150
note: "test",
5251
});
53-
}
52+
},
53+
{ name: "Create card" }
5454
);
5555
5656
//log the url of the created card

docs/documentation/concepts/tasks.mdx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,13 @@ client.defineJob({
4444
await io.wait("wait", 60 * 60 * 3); // wait for 3 hours
4545

4646
// You can wrap your own code in a Task, for retrying, resumability and logging
47-
const response = await io.runTask("my-task", { name: "My Task" }, async () => {
48-
return await longRunningCode(payload.userId);
49-
});
47+
const response = await io.runTask(
48+
"my-task",
49+
async () => {
50+
return await longRunningCode(payload.userId);
51+
},
52+
{ name: "My Task" }
53+
);
5054

5155
return response;
5256
},

docs/documentation/guides/create-a-job.mdx

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,9 @@ client.defineJob({
3232
The `id` and `name` are important because they are used to create and identify your Job in the app.
3333
3434
<Note>
35-
This Job must be imported in the `trigger` file in order to be registered when
36-
the CLI dev command is run. This can be found in either the
37-
`app/api/trigger/route.ts` file if you're using the Next.js App Router, or
38-
`pages/api/trigger.ts` if you're using the Next.js Pages Router.
35+
This Job must be imported in the `trigger` file in order to be registered when the CLI dev command
36+
is run. This can be found in either the `app/api/trigger/route.ts` file if you're using the
37+
Next.js App Router, or `pages/api/trigger.ts` if you're using the Next.js Pages Router.
3938
</Note>
4039
4140
### 2. Choose a Trigger
@@ -112,30 +111,30 @@ This is what kicks-off a Job. There are a few different types of Triggers you ca
112111
> A Task is a resumable unit of a Run that can be retried, resumed and is logged.
113112
114113
<Info>
115-
You can use just regular code in your Jobs. But you don't get the benefits of
116-
retrying, logging and resumability. More info on [Tasks vs regular
114+
You can use just regular code in your Jobs. But you don't get the benefits of retrying, logging
115+
and resumability. More info on [Tasks vs regular
117116
code](/documentation/concepts/tasks#tasks-vs-regular-code).
118117
</Info>
119118
120119
You can string together multiple Tasks and regular code in any order you want.
121120
122121
**Useful built-in Tasks:**
123122
124-
| Task | Description | Task code |
125-
| ------------------------------------------- | ----------------------------------------------------------------------- | ------------------------------------------------------------------------------------------ |
126-
| [Delay](/documentation/concepts/delays) | Wait for a period of time | `await io.wait("wait", 60);` |
127-
| [Log](/sdk/io/logger) | Log a message | `await io.logger.log("Hello");` |
128-
| [Send Event](/sdk/io/sendevent) | Send an event (for eventTrigger) | `await io.sendEvent("my-event", { name: "my.event", payload: { hello: "world" } });` |
129-
| [Run task](/sdk/io/runtask) | Wrap your own code in this to create a Task | `await io.runTask("My Task", { name: "My Task" }, async () => { console.log("Hello"); });` |
130-
| [Background fetch](/sdk/io/backgroundfetch) | Fetch data from a URL that can take longer that the serverless timeout. | `await io.backgroundFetch("fetch-some-data", { url: "https://example.com" });` |
123+
| Task | Description | Task code |
124+
| ------------------------------------------- | ----------------------------------------------------------------------- | ------------------------------------------------------------------------------------ |
125+
| [Delay](/documentation/concepts/delays) | Wait for a period of time | `await io.wait("wait", 60);` |
126+
| [Log](/sdk/io/logger) | Log a message | `await io.logger.log("Hello");` |
127+
| [Send Event](/sdk/io/sendevent) | Send an event (for eventTrigger) | `await io.sendEvent("my-event", { name: "my.event", payload: { hello: "world" } });` |
128+
| [Run task](/sdk/io/runtask) | Wrap your own code in this to create a Task | `await io.runTask("My Task", async () => { console.log("Hello"); });` |
129+
| [Background fetch](/sdk/io/backgroundfetch) | Fetch data from a URL that can take longer that the serverless timeout. | `await io.backgroundFetch("fetch-some-data", { url: "https://example.com" });` |
131130
132131
For a full list of built-in Tasks, see the [io SDK reference](/sdk/io).
133132
134133
**Integration Task examples:**
135134
136135
<Info>
137-
To use our integrations you will need to set them up in the app first. Our
138-
guide is [here](/documentation/guides/using-integrations).
136+
To use our integrations you will need to set them up in the app first. Our guide is
137+
[here](/documentation/guides/using-integrations).
139138
</Info>
140139
141140
<AccordionGroup>
@@ -229,10 +228,9 @@ yarn dlx @trigger.dev/cli@latest dev
229228
This will register all of your Jobs, they should appear in your dashboard.
230229
231230
<Note>
232-
Not seeing your Job in the web app? It might be because you forgot to import
233-
it. This will need to be either in `app/api/trigger/route.ts` file if you're
234-
using the Next,js App Router, or `pages/api/trigger.ts` if you're using the
235-
Next,js Pages Router.
231+
Not seeing your Job in the web app? It might be because you forgot to import it. This will need to
232+
be either in `app/api/trigger/route.ts` file if you're using the Next,js App Router, or
233+
`pages/api/trigger.ts` if you're using the Next,js Pages Router.
236234
</Note>
237235
238236
If you are having trouble getting your job running, please reach out to us and we will help you fix any issues:

docs/integrations/apis/github.mdx

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,17 @@ client.defineJob({
8282
github,
8383
},
8484
run: async (payload, io, ctx) => {
85-
//wrap the SDK call in runTask
86-
const { data } = await io.runTask("create-card", { name: "Create card" }, async () => {
87-
//create a project card using the underlying client
88-
return io.github.client.rest.projects.createCard({
89-
column_id: 123,
90-
note: "test",
91-
});
92-
});
85+
//io.github.runTask allows you to use the underlying SDK client
86+
const { data } = await io.github.runTask(
87+
"create-card",
88+
async (client) => {
89+
return client.rest.projects.createCard({
90+
column_id: 123,
91+
note: "test",
92+
});
93+
},
94+
{ name: "Create card" }
95+
);
9396

9497
//log the url of the created card
9598
await io.logger.info(data.url);

docs/integrations/apis/plain.mdx

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ client.defineJob({
131131

132132
## Using the underlying client
133133

134-
You can use the underlying client to do anything [@team-plain/typescript-sdk](https://github.com/team-plain/typescript-sdk) supports, but make sure to wrap it in a task:
134+
You can use the underlying client to do anything [@team-plain/typescript-sdk](https://github.com/team-plain/typescript-sdk) supports by using runTask:
135135

136136
```ts
137137
import { Plain } from "@trigger.dev/plain";
@@ -151,21 +151,14 @@ client.defineJob({
151151
name: "plain.client",
152152
}),
153153
run: async (payload, io, ctx) => {
154-
const issue = await io.runTask(
154+
const result = await io.plain.runTask(
155155
"create-issue",
156-
{ name: "Create issue", icon: "plain" },
157-
async () => {
158-
const result = await io.plain.client.createIssue({
156+
async (client) =>
157+
client.createIssue({
159158
customerId: "abcdefghij",
160159
issueTypeId: "123456",
161-
});
162-
163-
if (result.error) {
164-
throw result.error;
165-
}
166-
167-
return result.data;
168-
}
160+
}),
161+
{ name: "Create issue" }
169162
);
170163
},
171164
});

docs/integrations/apis/stripe.mdx

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ If there are any tasks missing that you'd like to see added, please [open a new
215215

216216
## Using the underlying Stripe client
217217

218-
You can use the underlying client to do anything the [stripe-node](https://github.com/stripe/stripe-node) client supports by using the `client` property on the integration:
218+
You can use the underlying client to do anything the [stripe-node](https://github.com/stripe/stripe-node) client supports by using `runTask` on the integration:
219219

220220
```ts
221221
const stripe = new Stripe({
@@ -234,20 +234,25 @@ client.defineJob({
234234
stripe,
235235
},
236236
run: async (payload, io, ctx) => {
237-
await io.runTask("create-price", { name: "Create Price" }, async (task) => {
238-
return stripe.client.prices.create(
239-
{
240-
unit_amount: 2000,
241-
currency: "usd",
242-
product_data: {
243-
name: "T-shirt",
237+
const price = await io.stripe.runTask(
238+
"create-price",
239+
async (client, task) => {
240+
return client.prices.create(
241+
{
242+
unit_amount: 2000,
243+
currency: "usd",
244+
product_data: {
245+
name: "T-shirt",
246+
},
244247
},
245-
},
246-
{
247-
idempotencyKey: task.idempotencyKey,
248-
}
249-
);
250-
});
248+
{
249+
idempotencyKey: task.idempotencyKey,
250+
}
251+
);
252+
},
253+
//this is optional, it will appear on the Run page
254+
{ name: "Create Price" }
255+
);
251256
},
252257
});
253258
```

docs/integrations/apis/typeform.mdx

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ client.defineJob({
119119

120120
## Using the underlying client
121121

122-
You can use the underlying client to do anything [@typeform/api-client](https://www.npmjs.com/package/@typeform/api-client) supports, but make sure to wrap it in a task:
122+
You can use the underlying client to do anything [@typeform/api-client](https://www.npmjs.com/package/@typeform/api-client) supports:
123123

124124
```ts
125125
import { Typeform } from "@trigger.dev/typeform";
@@ -139,14 +139,28 @@ client.defineJob({
139139
name: "typeform.client",
140140
}),
141141
run: async (payload, io, ctx) => {
142-
const form = await io.runTask(
142+
const form = await io.typeform.runTask(
143143
"create-form",
144-
{ name: "Create Form", icon: "typeform" },
145-
async () => {
146-
return io.typeform.client.forms.create({
147-
data: { ... }
148-
})
149-
}
144+
async (client) => {
145+
return client.forms.create({
146+
data: {
147+
title: "My Form",
148+
fields: [
149+
{
150+
title: "What is your name?",
151+
type: "short_text",
152+
ref: "name",
153+
},
154+
{
155+
title: "What is your email?",
156+
type: "email",
157+
ref: "email",
158+
},
159+
],
160+
},
161+
});
162+
},
163+
{ name: "Create Form" }
150164
);
151165
},
152166
});

docs/integrations/create-tasks.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -374,10 +374,10 @@ const createIssueCommentWithReaction: GithubAuthenticatedTask<
374374
run: async (params, client, task, io, auth) => {
375375
const comment = await io.runTask(
376376
`Comment on Issue #${params.issueNumber}`,
377-
createIssueComment.init(params),
378377
async (t) => {
379378
return createIssueComment.run(params, client, t, io, auth);
380-
}
379+
},
380+
createIssueComment.init(params)
381381
);
382382

383383
await io.runTask(

docs/sdk/io/runtask.mdx

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,26 @@ A Task is a resumable unit of a Run that can be retried, resumed and is logged.
1010

1111
<Snippet file="stable-key-param.mdx" />
1212

13-
<ResponseField name="options" type="object" required>
14-
The options of how you'd like to run and log the Task. `name` is the only required field.
13+
<ResponseField name="callback" type="function" required>
14+
The callback that will be called when the Task is run, this is where your logic should go. The callback receives
15+
the Task and the IO as parameters.
16+
17+
<Expandable title="arguments">
18+
<ResponseField name="task" type="Task">
19+
The Task that is running. It has some useful properties like `idempotencyKey` and `attempts`.
20+
</ResponseField>
21+
<ResponseField name="io" type="IO">
22+
[IO](/sdk/io/overview) holds Integrations and useful actions you can perform.
23+
</ResponseField>
24+
</Expandable>
25+
</ResponseField>
26+
27+
<ResponseField name="options" type="object">
28+
The options of how you'd like to run and log the Task.
1529

1630
<Expandable title="options">
17-
<ResponseField name="name" type="string" required>
18-
The name of the Task is required. This is displayed on the Task in the logs.
31+
<ResponseField name="name" type="string">
32+
This is displayed on the Task in the logs.
1933
</ResponseField>
2034
<ResponseField name="delayUntil" type="date">
2135
The Task will wait and only start at the specified Date.
@@ -87,20 +101,6 @@ A Task is a resumable unit of a Run that can be retried, resumed and is logged.
87101
</Expandable>
88102
</ResponseField>
89103

90-
<ResponseField name="callback" type="function" required>
91-
The callback that will be called when the Task is run, this is where your logic should go. The callback receives
92-
the Task and the IO as parameters.
93-
94-
<Expandable title="arguments">
95-
<ResponseField name="task" type="Task">
96-
The Task that is running. It has some useful properties like `idempotencyKey` and `attempts`.
97-
</ResponseField>
98-
<ResponseField name="io" type="IO">
99-
[IO](/sdk/io/overview) holds Integrations and useful actions you can perform.
100-
</ResponseField>
101-
</Expandable>
102-
</ResponseField>
103-
104104
<ResponseField name="onError" type="function">
105105
An optional callback that will be called when the Task fails. You can perform
106106
logic in here and optionally return a custom error object. Returning an object with `{ retryAt: Date, error?: Error }` will retry the Task at the specified Date. You can also just return a new `Error` object to throw a new error. Return nothing to rethrow the original error.

0 commit comments

Comments
 (0)