@@ -13,6 +13,7 @@ import { Effect, Schedule, Duration, Fiber } from "effect";
1313import { type RuntimeFiber } from "effect/Fiber" ;
1414import { setTimeout } from "timers/promises" ;
1515import { Logger , LogLevel } from "@trigger.dev/core/logger" ;
16+ import type { RunStore } from "@internal/run-store" ;
1617
1718const RUN_UPDATABLE_WINDOW_MS = 60 * 60 * 1000 ; // 1 hour
1819
@@ -24,6 +25,7 @@ type BufferedRunMetadataChangeOperation = {
2425
2526export type UpdateMetadataServiceOptions = {
2627 prisma : PrismaClientOrTransaction ;
28+ runStore : RunStore ;
2729 flushIntervalMs ?: number ;
2830 flushEnabled ?: boolean ;
2931 flushLoggingEnabled ?: boolean ;
@@ -49,6 +51,7 @@ export class UpdateMetadataService {
4951 private _bufferedOperations : Map < string , BufferedRunMetadataChangeOperation [ ] > = new Map ( ) ;
5052 private _flushFiber : RuntimeFiber < void > | null = null ;
5153 private readonly _prisma : PrismaClientOrTransaction ;
54+ private readonly _runStore : RunStore ;
5255 private readonly flushIntervalMs : number ;
5356 private readonly flushEnabled : boolean ;
5457 private readonly flushLoggingEnabled : boolean ;
@@ -57,6 +60,7 @@ export class UpdateMetadataService {
5760
5861 constructor ( private readonly options : UpdateMetadataServiceOptions ) {
5962 this . _prisma = options . prisma ;
63+ this . _runStore = options . runStore ;
6064 this . flushIntervalMs = options . flushIntervalMs ?? 5000 ;
6165 this . flushEnabled = options . flushEnabled ?? true ;
6266 this . flushLoggingEnabled = options . flushLoggingEnabled ?? true ;
@@ -260,17 +264,16 @@ export class UpdateMetadataService {
260264 const writeTime = new Date ( ) ;
261265 const result = yield * _ (
262266 Effect . tryPromise ( ( ) =>
263- this . _prisma . taskRun . updateMany ( {
264- where : {
265- id : runId ,
266- metadataVersion : run . metadataVersion ,
267- } ,
268- data : {
269- metadata : newMetadataPacket . data ,
267+ this . _runStore . updateMetadata (
268+ runId ,
269+ {
270+ metadata : newMetadataPacket . data ! ,
270271 metadataVersion : { increment : 1 } ,
271272 updatedAt : writeTime ,
272273 } ,
273- } )
274+ { expectedMetadataVersion : run . metadataVersion } ,
275+ this . _prisma
276+ )
274277 )
275278 ) ;
276279
@@ -469,20 +472,19 @@ export class UpdateMetadataService {
469472 // Update with optimistic locking; updatedAt stamped explicitly so the caller can
470473 // publish the exact committed watermark without a follow-up read.
471474 const writeTime = new Date ( ) ;
472- const result = await this . _prisma . taskRun . updateMany ( {
473- where : {
474- id : runId ,
475- metadataVersion : run . metadataVersion ,
476- } ,
477- data : {
478- metadata : newMetadataPacket . data ,
475+ const result = await this . _runStore . updateMetadata (
476+ runId ,
477+ {
478+ metadata : newMetadataPacket . data ! ,
479479 metadataType : newMetadataPacket . dataType ,
480480 metadataVersion : {
481481 increment : 1 ,
482482 } ,
483483 updatedAt : writeTime ,
484484 } ,
485- } ) ;
485+ { expectedMetadataVersion : run . metadataVersion } ,
486+ this . _prisma
487+ ) ;
486488
487489 if ( result . count === 0 ) {
488490 if ( this . flushLoggingEnabled ) {
@@ -564,19 +566,19 @@ export class UpdateMetadataService {
564566 // Update the metadata without version check; updatedAt stamped explicitly so the
565567 // caller can publish the exact committed watermark.
566568 const writeTime = new Date ( ) ;
567- await this . _prisma . taskRun . update ( {
568- where : {
569- id : runId ,
570- } ,
571- data : {
572- metadata : metadataPacket ?. data ,
569+ await this . _runStore . updateMetadata (
570+ runId ,
571+ {
572+ metadata : metadataPacket ?. data ! ,
573573 metadataType : metadataPacket ?. dataType ,
574574 metadataVersion : {
575575 increment : 1 ,
576576 } ,
577577 updatedAt : writeTime ,
578578 } ,
579- } ) ;
579+ { } ,
580+ this . _prisma
581+ ) ;
580582 updatedAtMs = writeTime . getTime ( ) ;
581583 }
582584
0 commit comments