@@ -13,6 +13,8 @@ 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" ;
17+ import { runStore as defaultRunStore } from "~/v3/runStore.server" ;
1618
1719const RUN_UPDATABLE_WINDOW_MS = 60 * 60 * 1000 ; // 1 hour
1820
@@ -24,6 +26,7 @@ type BufferedRunMetadataChangeOperation = {
2426
2527export type UpdateMetadataServiceOptions = {
2628 prisma : PrismaClientOrTransaction ;
29+ runStore ?: RunStore ;
2730 flushIntervalMs ?: number ;
2831 flushEnabled ?: boolean ;
2932 flushLoggingEnabled ?: boolean ;
@@ -49,6 +52,7 @@ export class UpdateMetadataService {
4952 private _bufferedOperations : Map < string , BufferedRunMetadataChangeOperation [ ] > = new Map ( ) ;
5053 private _flushFiber : RuntimeFiber < void > | null = null ;
5154 private readonly _prisma : PrismaClientOrTransaction ;
55+ private readonly _runStore : RunStore ;
5256 private readonly flushIntervalMs : number ;
5357 private readonly flushEnabled : boolean ;
5458 private readonly flushLoggingEnabled : boolean ;
@@ -57,6 +61,7 @@ export class UpdateMetadataService {
5761
5862 constructor ( private readonly options : UpdateMetadataServiceOptions ) {
5963 this . _prisma = options . prisma ;
64+ this . _runStore = options . runStore ?? defaultRunStore ;
6065 this . flushIntervalMs = options . flushIntervalMs ?? 5000 ;
6166 this . flushEnabled = options . flushEnabled ?? true ;
6267 this . flushLoggingEnabled = options . flushLoggingEnabled ?? true ;
@@ -260,17 +265,16 @@ export class UpdateMetadataService {
260265 const writeTime = new Date ( ) ;
261266 const result = yield * _ (
262267 Effect . tryPromise ( ( ) =>
263- this . _prisma . taskRun . updateMany ( {
264- where : {
265- id : runId ,
266- metadataVersion : run . metadataVersion ,
267- } ,
268- data : {
269- metadata : newMetadataPacket . data ,
268+ this . _runStore . updateMetadata (
269+ runId ,
270+ {
271+ metadata : newMetadataPacket . data ! ,
270272 metadataVersion : { increment : 1 } ,
271273 updatedAt : writeTime ,
272274 } ,
273- } )
275+ { expectedMetadataVersion : run . metadataVersion } ,
276+ this . _prisma
277+ )
274278 )
275279 ) ;
276280
@@ -469,20 +473,19 @@ export class UpdateMetadataService {
469473 // Update with optimistic locking; updatedAt stamped explicitly so the caller can
470474 // publish the exact committed watermark without a follow-up read.
471475 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 ,
476+ const result = await this . _runStore . updateMetadata (
477+ runId ,
478+ {
479+ metadata : newMetadataPacket . data ! ,
479480 metadataType : newMetadataPacket . dataType ,
480481 metadataVersion : {
481482 increment : 1 ,
482483 } ,
483484 updatedAt : writeTime ,
484485 } ,
485- } ) ;
486+ { expectedMetadataVersion : run . metadataVersion } ,
487+ this . _prisma
488+ ) ;
486489
487490 if ( result . count === 0 ) {
488491 if ( this . flushLoggingEnabled ) {
@@ -564,19 +567,19 @@ export class UpdateMetadataService {
564567 // Update the metadata without version check; updatedAt stamped explicitly so the
565568 // caller can publish the exact committed watermark.
566569 const writeTime = new Date ( ) ;
567- await this . _prisma . taskRun . update ( {
568- where : {
569- id : runId ,
570- } ,
571- data : {
572- metadata : metadataPacket ?. data ,
570+ await this . _runStore . updateMetadata (
571+ runId ,
572+ {
573+ metadata : metadataPacket ?. data ! ,
573574 metadataType : metadataPacket ?. dataType ,
574575 metadataVersion : {
575576 increment : 1 ,
576577 } ,
577578 updatedAt : writeTime ,
578579 } ,
579- } ) ;
580+ { } ,
581+ this . _prisma
582+ ) ;
580583 updatedAtMs = writeTime . getTime ( ) ;
581584 }
582585
0 commit comments