Skip to content

Commit 62c03bf

Browse files
committed
Created a shared determineEngineVersion function
1 parent 677a2d6 commit 62c03bf

File tree

2 files changed

+62
-36
lines changed

2 files changed

+62
-36
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { RunEngineVersion, RuntimeEnvironmentType } from "@trigger.dev/database";
2+
import { AuthenticatedEnvironment } from "~/services/apiAuth.server";
3+
4+
export async function determineEngineVersion({
5+
environment,
6+
version,
7+
}: {
8+
environment: AuthenticatedEnvironment;
9+
version?: RunEngineVersion;
10+
}): Promise<RunEngineVersion> {
11+
if (version) return version;
12+
13+
// If the project is V1, then none of the background workers are running V2
14+
if (environment.project.engine === RunEngineVersion.V1) {
15+
return "V1";
16+
}
17+
18+
// For now, dev is always V1
19+
if (environment.type === RuntimeEnvironmentType.DEVELOPMENT) {
20+
return "V1";
21+
}
22+
23+
//todo we need to determine the version using the BackgroundWorker
24+
//- triggerAndWait we can lookup the BackgroundWorker easily, and get the engine.
25+
//- No locked version: lookup the BackgroundWorker via the Deployment/latest dev BW
26+
// const workerWithTasks = workerId
27+
// ? await getWorkerDeploymentFromWorker(prisma, workerId)
28+
// : run.runtimeEnvironment.type === "DEVELOPMENT"
29+
// ? await getMostRecentWorker(prisma, run.runtimeEnvironmentId)
30+
// : await getWorkerFromCurrentlyPromotedDeployment(prisma, run.runtimeEnvironmentId);
31+
32+
//todo Additional checks
33+
/*
34+
- If the `triggerVersion` is 3.2 or higher AND the project has engine V2, we will use the run engine.
35+
- Add an `engine` column to `Project` in the database.
36+
37+
Add `engine` to the trigger.config file. It would default to "V1" for now, but you can set it to V2.
38+
39+
You run `npx trigger.dev@latest deploy` with config v2.
40+
- Create BackgroundWorker with `engine`: `v2`.
41+
- Set the `project` `engine` column to `v2`.
42+
43+
You run `npx trigger.dev@latest dev` with config v2
44+
- Create BackgroundWorker with `engine`: `v2`.
45+
- Set the `project` `engine` column to `v2`.
46+
47+
When triggering
48+
- triggerAndWait we can lookup the BackgroundWorker easily, and get the engine.
49+
- No locked version: lookup the BackgroundWorker via the Deployment/latest dev BW
50+
*/
51+
52+
return "V2";
53+
}

apps/webapp/app/v3/services/triggerTask.server.ts

Lines changed: 9 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { WithRunEngine } from "./baseService.server";
44
import { RunEngineVersion, RuntimeEnvironmentType } from "@trigger.dev/database";
55
import { TriggerTaskServiceV1 } from "./triggerTaskV1.server";
66
import { TriggerTaskServiceV2 } from "./triggerTaskV2.server";
7+
import { determineEngineVersion } from "../engineVersion.server";
78

89
export type TriggerTaskServiceOptions = {
910
idempotencyKey?: string;
@@ -35,44 +36,16 @@ export class TriggerTaskService extends WithRunEngine {
3536
return await this.traceWithEnv("call()", environment, async (span) => {
3637
span.setAttribute("taskId", taskId);
3738

38-
//todo we need to determine the version using the BackgroundWorker
39-
//- triggerAndWait we can lookup the BackgroundWorker easily, and get the engine.
40-
//- No locked version: lookup the BackgroundWorker via the Deployment/latest dev BW
41-
// const workerWithTasks = workerId
42-
// ? await getWorkerDeploymentFromWorker(prisma, workerId)
43-
// : run.runtimeEnvironment.type === "DEVELOPMENT"
44-
// ? await getMostRecentWorker(prisma, run.runtimeEnvironmentId)
45-
// : await getWorkerFromCurrentlyPromotedDeployment(prisma, run.runtimeEnvironmentId);
39+
const version = await determineEngineVersion({ environment });
4640

47-
if (environment.project.engine === RunEngineVersion.V1) {
48-
return await this.callV1(taskId, environment, body, options);
41+
switch (version) {
42+
case "V1": {
43+
return await this.callV1(taskId, environment, body, options);
44+
}
45+
case "V2": {
46+
return await this.callV2(taskId, environment, body, options);
47+
}
4948
}
50-
51-
if (environment.type === RuntimeEnvironmentType.DEVELOPMENT) {
52-
return await this.callV1(taskId, environment, body, options);
53-
}
54-
55-
//todo Additional checks
56-
/*
57-
- If the `triggerVersion` is 3.2 or higher AND the project has engine V2, we will use the run engine.
58-
- Add an `engine` column to `Project` in the database.
59-
60-
Add `engine` to the trigger.config file. It would default to "V1" for now, but you can set it to V2.
61-
62-
You run `npx trigger.dev@latest deploy` with config v2.
63-
- Create BackgroundWorker with `engine`: `v2`.
64-
- Set the `project` `engine` column to `v2`.
65-
66-
You run `npx trigger.dev@latest dev` with config v2
67-
- Create BackgroundWorker with `engine`: `v2`.
68-
- Set the `project` `engine` column to `v2`.
69-
70-
When triggering
71-
- triggerAndWait we can lookup the BackgroundWorker easily, and get the engine.
72-
- No locked version: lookup the BackgroundWorker via the Deployment/latest dev BW
73-
*/
74-
75-
return await this.callV2(taskId, environment, body, options);
7649
});
7750
}
7851

0 commit comments

Comments
 (0)