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/guides/use-cases/upgrading-from-v2.mdx
+171Lines changed: 171 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -12,6 +12,177 @@ The main difference is that things in v3 are far simpler. That's because in v3 y
12
12
3. Just use official SDKs, not integrations.
13
13
4.`task`s are the new primitive, not `job`s.
14
14
15
+
## Convert your v2 job using an AI prompt
16
+
17
+
The prompt in the accordion below gives good results when using Anthropic Claude 3.5 Sonnet. You’ll need a relatively large token limit.
18
+
19
+
<Note>Don't forget to paste your own v2 code in a markdown codeblock at the bottom of the prompt before running it.</Note>
20
+
21
+
<Accordiontitle="Copy and paste this prompt in full:">
22
+
23
+
I would like you to help me convert from Trigger.dev v2 to Trigger.dev v3.
24
+
The important differences:
25
+
1. The syntax for creating "background jobs" has changed. In v2 it looked like this:
26
+
27
+
```ts
28
+
import { eventTrigger } from"@trigger.dev/sdk";
29
+
import { client } from"@/trigger";
30
+
import { db } from"@/lib/db";
31
+
client.defineJob({
32
+
enabled: true,
33
+
id: "my-job-id",
34
+
name: "My job name",
35
+
version: "0.0.1",
36
+
// This is triggered by an event using eventTrigger. You can also trigger Jobs with webhooks, on schedules, and more: https://trigger.dev/docs/documentation/concepts/triggers/introduction
37
+
trigger: eventTrigger({
38
+
name: "theevent.name",
39
+
schema: z.object({
40
+
phoneNumber: z.string(),
41
+
verified: z.boolean(),
42
+
}),
43
+
}),
44
+
run: async (payload, io) => {
45
+
46
+
//everything needed to be wrapped in io.runTask in v2, to make it possible for long-running code to work
47
+
const result =awaitio.runTask("get-stuff-from-db", async () => {
//in v3 there are no timeouts, so you can just use the code as is, no need to wrap in `io.runTask`
68
+
const socials =awaitdb.query.Socials.findMany({
69
+
where: eq(Socials.service, "tiktok"),
70
+
});
71
+
72
+
//use `logger` instead of `io.logger`
73
+
logger.info("Completed fetch successfully");
74
+
},
75
+
});
76
+
```
77
+
78
+
Notice that the schema on v2 `eventTrigger` defines the payload type. In v3 that needs to be done on the TypeScript type of the `run` payload param.
79
+
2. v2 had integrations with some APIs. Any package that isn't `@trigger.dev/sdk` can be replaced with an official SDK. The syntax may need to be adapted.
80
+
For example:
81
+
v2:
82
+
83
+
```ts
84
+
import { OpenAI } from"@trigger.dev/openai";
85
+
const openai =newOpenAI({
86
+
id: "openai",
87
+
apiKey: process.env.OPENAI_API_KEY!,
88
+
});
89
+
client.defineJob({
90
+
id: "openai-job",
91
+
name: "OpenAI Job",
92
+
version: "1.0.0",
93
+
trigger: invokeTrigger(),
94
+
integrations: {
95
+
openai, // Add the OpenAI client as an integration
content: "Create a good programming joke about background jobs",
129
+
},
130
+
],
131
+
});
132
+
},
133
+
});
134
+
```
135
+
136
+
So don't use the `@trigger.dev/openai` package in v3, use the official OpenAI SDK.
137
+
Bear in mind that the syntax for the latest official SDK will probably be different from the @trigger.dev integration SDK. You will need to adapt the code accordingly.
138
+
3. The most critical difference is that inside the `run` function you do NOT need to wrap everything in `io.runTask`. So anything inside there can be extracted out and be used in the main body of the function without wrapping it.
139
+
4. The import for `task` in v3 is `import { task } from "@trigger.dev/sdk/v3";`
140
+
5. You can trigger jobs from other jobs. In v2 this was typically done by either calling `io.sendEvent()` or by calling `yourOtherTask.invoke()`. In v3 you call `.trigger()` on the other task, there are no events in v3.
Can you help me convert the following code from v2 to v3? Please include the full converted code in the answer, do not truncate it anywhere.
183
+
184
+
</Accordion>
185
+
15
186
## OpenAI example comparison
16
187
17
188
This is a (very contrived) example that does a long OpenAI API call (>10s), stores the result in a database, waits for 5 mins, and then returns the result.
0 commit comments