1
1
import type { Prettify } from "@trigger.dev/core" ;
2
- import { BackgroundWorker , RunEngineVersion , WorkerDeployment } from "@trigger.dev/database" ;
2
+ import { BackgroundWorker , RunEngineVersion , WorkerDeploymentType } from "@trigger.dev/database" ;
3
3
import {
4
4
CURRENT_DEPLOYMENT_LABEL ,
5
5
CURRENT_UNMANAGED_DEPLOYMENT_LABEL ,
@@ -56,10 +56,23 @@ type WorkerDeploymentWithWorkerTasks = Prisma.WorkerDeploymentGetPayload<{
56
56
} ;
57
57
} > ;
58
58
59
- export async function findCurrentWorkerDeployment (
60
- environmentId : string ,
61
- label = CURRENT_DEPLOYMENT_LABEL
62
- ) : Promise < WorkerDeploymentWithWorkerTasks | undefined > {
59
+ /**
60
+ * Finds the current worker deployment for a given environment.
61
+ *
62
+ * @param environmentId - The ID of the environment to find the current worker deployment for.
63
+ * @param label - The label of the current worker deployment to find.
64
+ * @param type - The type of worker deployment to find. If the current deployment is NOT of this type,
65
+ * we will return the latest deployment of the given type.
66
+ */
67
+ export async function findCurrentWorkerDeployment ( {
68
+ environmentId,
69
+ label = CURRENT_DEPLOYMENT_LABEL ,
70
+ type,
71
+ } : {
72
+ environmentId : string ;
73
+ label ?: string ;
74
+ type ?: WorkerDeploymentType ;
75
+ } ) : Promise < WorkerDeploymentWithWorkerTasks | undefined > {
63
76
const promotion = await prisma . workerDeploymentPromotion . findFirst ( {
64
77
where : {
65
78
environmentId,
@@ -93,16 +106,19 @@ export async function findCurrentWorkerDeployment(
93
106
return undefined ;
94
107
}
95
108
96
- if ( promotion . deployment . type === "V1" ) {
97
- // This is a run engine v1 deployment, so return it
109
+ if ( ! type ) {
98
110
return promotion . deployment ;
99
111
}
100
112
101
- // We need to get the latest run engine v1 deployment
102
- const latestV1Deployment = await prisma . workerDeployment . findFirst ( {
113
+ if ( promotion . deployment . type === type ) {
114
+ return promotion . deployment ;
115
+ }
116
+
117
+ // We need to get the latest deployment of the given type
118
+ const latestDeployment = await prisma . workerDeployment . findFirst ( {
103
119
where : {
104
120
environmentId,
105
- type : "V1" ,
121
+ type,
106
122
} ,
107
123
orderBy : {
108
124
id : "desc" ,
@@ -127,11 +143,11 @@ export async function findCurrentWorkerDeployment(
127
143
} ,
128
144
} ) ;
129
145
130
- if ( ! latestV1Deployment ) {
146
+ if ( ! latestDeployment ) {
131
147
return undefined ;
132
148
}
133
149
134
- return latestV1Deployment ;
150
+ return latestDeployment ;
135
151
}
136
152
137
153
export async function getCurrentWorkerDeploymentEngineVersion (
@@ -162,7 +178,11 @@ export async function getCurrentWorkerDeploymentEngineVersion(
162
178
export async function findCurrentUnmanagedWorkerDeployment (
163
179
environmentId : string
164
180
) : Promise < WorkerDeploymentWithWorkerTasks | undefined > {
165
- return await findCurrentWorkerDeployment ( environmentId , CURRENT_UNMANAGED_DEPLOYMENT_LABEL ) ;
181
+ return await findCurrentWorkerDeployment ( {
182
+ environmentId,
183
+ label : CURRENT_UNMANAGED_DEPLOYMENT_LABEL ,
184
+ type : "UNMANAGED" ,
185
+ } ) ;
166
186
}
167
187
168
188
export async function findCurrentWorkerFromEnvironment (
@@ -183,7 +203,10 @@ export async function findCurrentWorkerFromEnvironment(
183
203
} ) ;
184
204
return latestDevWorker ;
185
205
} else {
186
- const deployment = await findCurrentWorkerDeployment ( environment . id , label ) ;
206
+ const deployment = await findCurrentWorkerDeployment ( {
207
+ environmentId : environment . id ,
208
+ label,
209
+ } ) ;
187
210
return deployment ?. worker ?? null ;
188
211
}
189
212
}
0 commit comments