Skip to content

Commit b66b46c

Browse files
committed
Merge remote-tracking branch 'origin/main' into fix/retry-fixes
2 parents ad5fb5f + 1f11a8d commit b66b46c

File tree

3 files changed

+175
-21
lines changed

3 files changed

+175
-21
lines changed

apps/proxy/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"private": true,
55
"scripts": {
66
"deploy": "wrangler deploy",
7-
"dev": "wrangler dev"
7+
"dev": "wrangler dev",
8+
"dry-run:staging": "wrangler deploy --dry-run --outdir=dist --env staging"
89
},
910
"devDependencies": {
1011
"@cloudflare/workers-types": "^4.20240512.0",

apps/proxy/src/index.ts

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,9 @@ export interface Env {
1717

1818
export default {
1919
async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
20-
if (!env.REWRITE_HOSTNAME) throw new Error("Missing REWRITE_HOSTNAME");
21-
console.log("url", request.url);
22-
2320
if (!queueingIsEnabled(env)) {
2421
console.log("Missing AWS credentials. Passing through to the origin.");
25-
return redirectToOrigin(request, env);
22+
return fetch(request);
2623
}
2724

2825
const url = new URL(request.url);
@@ -42,25 +39,10 @@ export default {
4239
}
4340

4441
//the same request but with the hostname (and port) changed
45-
return redirectToOrigin(request, env);
42+
return fetch(request);
4643
},
4744
};
4845

49-
function redirectToOrigin(request: Request, env: Env) {
50-
const newUrl = new URL(request.url);
51-
newUrl.hostname = env.REWRITE_HOSTNAME;
52-
newUrl.port = env.REWRITE_PORT || newUrl.port;
53-
54-
const requestInit: RequestInit = {
55-
method: request.method,
56-
headers: request.headers,
57-
body: request.body,
58-
};
59-
60-
console.log("rewritten url", newUrl.toString());
61-
return fetch(newUrl.toString(), requestInit);
62-
}
63-
6446
function queueingIsEnabled(env: Env) {
6547
return (
6648
env.AWS_SQS_ACCESS_KEY_ID &&

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)