@@ -2,7 +2,10 @@ import { sanitizeQueueName } from "@trigger.dev/core/v3/isomorphic";
22import type { PrismaClientOrTransaction } from "@trigger.dev/database" ;
33import type { AuthenticatedEnvironment } from "~/services/apiAuth.server" ;
44import { logger } from "~/services/logger.server" ;
5- import { findCurrentWorkerFromEnvironment } from "~/v3/models/workerDeployment.server" ;
5+ import {
6+ findCurrentWorkerDeployment ,
7+ findCurrentWorkerFromEnvironment ,
8+ } from "~/v3/models/workerDeployment.server" ;
69import type {
710 LockedBackgroundWorker ,
811 QueueManager ,
@@ -101,6 +104,7 @@ export class DefaultQueueManager implements QueueManager {
101104 let taskTtl : string | null | undefined ;
102105 let taskKind : string | undefined ;
103106 let taskGates : TaskMetadataGate [ ] | null | undefined ;
107+ let templateKey : string | null | undefined ;
104108
105109 // Determine queue name based on lockToVersion and provided options
106110 if ( lockedBackgroundWorker ) {
@@ -153,6 +157,7 @@ export class DefaultQueueManager implements QueueManager {
153157 }
154158 taskKind = lockedMeta ?. triggerSource ;
155159 taskGates = lockedMeta ?. gates ;
160+ templateKey = lockedMeta ?. templateKey ;
156161 } else {
157162 // No queue override - resolve default queue + TTL + triggerSource via cache,
158163 // falling back to a single BackgroundWorkerTask lookup on miss.
@@ -192,6 +197,7 @@ export class DefaultQueueManager implements QueueManager {
192197 lockedQueueId = lockedMeta . queueId ?? undefined ;
193198 taskKind = lockedMeta . triggerSource ;
194199 taskGates = lockedMeta . gates ;
200+ templateKey = lockedMeta . templateKey ;
195201 }
196202 } else {
197203 // Task is not locked to a specific version, use regular logic
@@ -208,6 +214,7 @@ export class DefaultQueueManager implements QueueManager {
208214 taskTtl = taskInfo . taskTtl ;
209215 taskKind = taskInfo . taskKind ;
210216 taskGates = taskInfo . taskGates ;
217+ templateKey = taskInfo . templateKey ;
211218 }
212219
213220 // Sanitize the final determined queue name once
@@ -292,6 +299,7 @@ export class DefaultQueueManager implements QueueManager {
292299 taskTtl,
293300 taskKind,
294301 gates : gates && gates . length > 0 ? gates : undefined ,
302+ templateKey,
295303 } ;
296304 }
297305
@@ -300,6 +308,7 @@ export class DefaultQueueManager implements QueueManager {
300308 taskTtl ?: string | null ;
301309 taskKind ?: string | undefined ;
302310 taskGates ?: TaskMetadataGate [ ] | null ;
311+ templateKey ?: string | null ;
303312 } > {
304313 const { taskId, environment, body } = request ;
305314 const { queue } = body . options ?? { } ;
@@ -323,6 +332,7 @@ export class DefaultQueueManager implements QueueManager {
323332 taskTtl : meta ?. ttl ?? undefined ,
324333 taskKind : meta ?. triggerSource ,
325334 taskGates : meta ?. gates ,
335+ templateKey : meta ?. templateKey ,
326336 } ;
327337 }
328338
@@ -344,6 +354,7 @@ export class DefaultQueueManager implements QueueManager {
344354 taskTtl : meta . ttl ,
345355 taskKind : meta . triggerSource ,
346356 taskGates : meta . gates ,
357+ templateKey : meta . templateKey ,
347358 } ;
348359 }
349360
@@ -352,6 +363,7 @@ export class DefaultQueueManager implements QueueManager {
352363 taskTtl : meta . ttl ,
353364 taskKind : meta . triggerSource ,
354365 taskGates : meta . gates ,
366+ templateKey : meta . templateKey ,
355367 } ;
356368 }
357369
@@ -404,6 +416,7 @@ export class DefaultQueueManager implements QueueManager {
404416
405417 recordTaskMetaResolve ( "locked" , source ) ;
406418
419+ const templateKey = row . worker . deployment ?. templateKey ?? null ;
407420 const entry : TaskMetadataEntry = {
408421 slug,
409422 ttl : row . ttl ,
@@ -415,9 +428,9 @@ export class DefaultQueueManager implements QueueManager {
415428
416429 // Fire-and-forget back-fill — `setByWorker` upserts the single field and
417430 // refreshes the hash TTL. Errors are logged inside the cache and swallowed.
418- void this . taskMetaCache . setByWorker ( workerId , entry ) ;
431+ void this . taskMetaCache . setByWorker ( workerId , entry , templateKey ) ;
419432
420- return entry ;
433+ return { ... entry , templateKey } ;
421434 }
422435
423436 private findLockedTaskRow (
@@ -433,6 +446,7 @@ export class DefaultQueueManager implements QueueManager {
433446 triggerSource : true ,
434447 gates : true ,
435448 queue : { select : { id : true , name : true } } ,
449+ worker : { select : { deployment : { select : { templateKey : true } } } } ,
436450 } ,
437451 } ) ;
438452 }
@@ -459,7 +473,18 @@ export class DefaultQueueManager implements QueueManager {
459473 // the adjacent BackgroundWorkerTask lookup below uses `replicaPrisma` too
460474 // (replica lag for "just deployed" is bounded the same way for both
461475 // queries; reading from the writer here would only widen the window).
462- const worker = await findCurrentWorkerFromEnvironment ( environment , this . replicaPrisma ) ;
476+ let worker : { id : string } | null ;
477+ let templateKey : string | null = null ;
478+ if ( environment . type === "DEVELOPMENT" ) {
479+ worker = await findCurrentWorkerFromEnvironment ( environment , this . replicaPrisma ) ;
480+ } else {
481+ const deployment = await findCurrentWorkerDeployment ( {
482+ environmentId : environment . id ,
483+ prismaClient : this . replicaPrisma ,
484+ } ) ;
485+ worker = deployment ?. worker ?? null ;
486+ templateKey = deployment ?. templateKey ?? null ;
487+ }
463488 if ( ! worker ) {
464489 recordTaskMetaResolve ( "current" , "miss" ) ;
465490 return null ;
@@ -495,9 +520,9 @@ export class DefaultQueueManager implements QueueManager {
495520 // keyspaces so a subsequent locked-or-not trigger hits the cache. The
496521 // env-keyspace TTL is preserved (promotion owns it); the by-worker TTL
497522 // is refreshed (sliding window keeps active workers warm).
498- void this . taskMetaCache . setByCurrentWorker ( environment . id , worker . id , entry ) ;
523+ void this . taskMetaCache . setByCurrentWorker ( environment . id , worker . id , entry , templateKey ) ;
499524
500- return entry ;
525+ return { ... entry , templateKey } ;
501526 }
502527
503528 async validateQueueLimits (
0 commit comments