Skip to content

Commit 1f11a8d

Browse files
committed
Added an AI prompt for converting v2 jobs to v3 tasks
1 parent af427aa commit 1f11a8d

File tree

1 file changed

+171
-0
lines changed

1 file changed

+171
-0
lines changed

docs/guides/use-cases/upgrading-from-v2.mdx

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,177 @@ The main difference is that things in v3 are far simpler. That's because in v3 y
1212
3. Just use official SDKs, not integrations.
1313
4. `task`s are the new primitive, not `job`s.
1414

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+
<Accordion title="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 = await io.runTask("get-stuff-from-db", async () => {
48+
const socials = await db.query.Socials.findMany({
49+
where: eq(Socials.service, "tiktok"),
50+
});
51+
return socials;
52+
});
53+
54+
io.logger.info("Completed fetch successfully");
55+
},
56+
});
57+
```
58+
59+
In v3 it looks like this:
60+
61+
```ts
62+
import { task } from "@trigger.dev/sdk/v3";
63+
import { db } from "@/lib/db";
64+
export const getCreatorVideosFromTikTok = task({
65+
id: "my-job-id",
66+
run: async (payload: { phoneNumber: string, verified: boolean }) => {
67+
//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 = await db.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 = new OpenAI({
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
96+
},
97+
run: async (payload, io, ctx) => {
98+
// Now you can access it through the io object
99+
const completion = await io.openai.chat.completions.create("completion", {
100+
model: "gpt-3.5-turbo",
101+
messages: [
102+
{
103+
role: "user",
104+
content: "Create a good programming joke about background jobs",
105+
},
106+
],
107+
});
108+
},
109+
});
110+
```
111+
112+
Would become in v3:
113+
114+
```ts
115+
import OpenAI from "openai";
116+
const openai = new OpenAI({
117+
apiKey: process.env.OPENAI_API_KEY,
118+
});
119+
export const openaiJob = task({
120+
id: "openai-job",
121+
run: async (payload) => {
122+
const completion = await openai.chat.completions.create(
123+
{
124+
model: "gpt-3.5-turbo",
125+
messages: [
126+
{
127+
role: "user",
128+
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.
141+
v2:
142+
143+
```ts
144+
export const parentJob = client.defineJob({
145+
id: "parent-job",
146+
run: async (payload, io) => {
147+
//send event
148+
await client.sendEvent({
149+
name: "user.created",
150+
payload: { name: "John Doe", email: "john@doe.com", paidPlan: true },
151+
});
152+
153+
//invoke
154+
await exampleJob.invoke({ foo: "bar" }, {
155+
idempotencyKey: `some_string_here_${
156+
payload.someValue
157+
}_${new Date().toDateString()}`,
158+
});
159+
},
160+
});
161+
```
162+
163+
v3:
164+
165+
```ts
166+
export const parentJob = task({
167+
id: "parent-job",
168+
run: async (payload) => {
169+
//trigger
170+
await userCreated.trigger({ name: "John Doe", email: "john@doe.com", paidPlan: true });
171+
172+
//trigger, you can pass in an idempotency key
173+
await exampleJob.trigger({ foo: "bar" }, {
174+
idempotencyKey: `some_string_here_${
175+
payload.someValue
176+
}_${new Date().toDateString()}`,
177+
});
178+
}
179+
});
180+
```
181+
182+
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+
15186
## OpenAI example comparison
16187

17188
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

Comments
 (0)