11import type { Prettify } from "@trigger.dev/core" ;
2- import { BackgroundWorker , RunEngineVersion , WorkerDeployment } from "@trigger.dev/database" ;
2+ import { BackgroundWorker , RunEngineVersion , WorkerDeploymentType } from "@trigger.dev/database" ;
33import {
44 CURRENT_DEPLOYMENT_LABEL ,
55 CURRENT_UNMANAGED_DEPLOYMENT_LABEL ,
@@ -56,10 +56,23 @@ type WorkerDeploymentWithWorkerTasks = Prisma.WorkerDeploymentGetPayload<{
5656 } ;
5757} > ;
5858
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 > {
6376 const promotion = await prisma . workerDeploymentPromotion . findFirst ( {
6477 where : {
6578 environmentId,
@@ -93,16 +106,19 @@ export async function findCurrentWorkerDeployment(
93106 return undefined ;
94107 }
95108
96- if ( promotion . deployment . type === "V1" ) {
97- // This is a run engine v1 deployment, so return it
109+ if ( ! type ) {
98110 return promotion . deployment ;
99111 }
100112
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 ( {
103119 where : {
104120 environmentId,
105- type : "V1" ,
121+ type,
106122 } ,
107123 orderBy : {
108124 id : "desc" ,
@@ -127,11 +143,11 @@ export async function findCurrentWorkerDeployment(
127143 } ,
128144 } ) ;
129145
130- if ( ! latestV1Deployment ) {
146+ if ( ! latestDeployment ) {
131147 return undefined ;
132148 }
133149
134- return latestV1Deployment ;
150+ return latestDeployment ;
135151}
136152
137153export async function getCurrentWorkerDeploymentEngineVersion (
@@ -162,7 +178,11 @@ export async function getCurrentWorkerDeploymentEngineVersion(
162178export async function findCurrentUnmanagedWorkerDeployment (
163179 environmentId : string
164180) : 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+ } ) ;
166186}
167187
168188export async function findCurrentWorkerFromEnvironment (
@@ -183,7 +203,10 @@ export async function findCurrentWorkerFromEnvironment(
183203 } ) ;
184204 return latestDevWorker ;
185205 } else {
186- const deployment = await findCurrentWorkerDeployment ( environment . id , label ) ;
206+ const deployment = await findCurrentWorkerDeployment ( {
207+ environmentId : environment . id ,
208+ label,
209+ } ) ;
187210 return deployment ?. worker ?? null ;
188211 }
189212}
0 commit comments