66 parsePacket ,
77} from "@trigger.dev/core/v3" ;
88import { BatchTaskRun , Prisma , TaskRunAttempt } from "@trigger.dev/database" ;
9- import { $transaction , PrismaClientOrTransaction } from "~/db.server" ;
9+ import { $transaction , prisma , PrismaClientOrTransaction } from "~/db.server" ;
1010import { env } from "~/env.server" ;
1111import { batchTaskRunItemStatusForRunStatus } from "~/models/taskRun.server" ;
1212import { AuthenticatedEnvironment } from "~/services/apiAuth.server" ;
@@ -25,12 +25,10 @@ import { z } from "zod";
2525
2626const PROCESSING_BATCH_SIZE = 50 ;
2727const ASYNC_BATCH_PROCESS_SIZE_THRESHOLD = 20 ;
28+ const MAX_ATTEMPTS = 10 ;
2829
29- const BatchProcessingStrategy = z . enum ( [ "sequential" , "parallel" ] ) ;
30-
31- type BatchProcessingStrategy = z . infer < typeof BatchProcessingStrategy > ;
32-
33- const CURRENT_STRATEGY : BatchProcessingStrategy = "parallel" ;
30+ export const BatchProcessingStrategy = z . enum ( [ "sequential" , "parallel" ] ) ;
31+ export type BatchProcessingStrategy = z . infer < typeof BatchProcessingStrategy > ;
3432
3533export const BatchProcessingOptions = z . object ( {
3634 batchId : z . string ( ) ,
@@ -52,6 +50,17 @@ export type BatchTriggerTaskServiceOptions = {
5250} ;
5351
5452export class BatchTriggerV2Service extends BaseService {
53+ private _batchProcessingStrategy : BatchProcessingStrategy ;
54+
55+ constructor (
56+ batchProcessingStrategy ?: BatchProcessingStrategy ,
57+ protected readonly _prisma : PrismaClientOrTransaction = prisma
58+ ) {
59+ super ( _prisma ) ;
60+
61+ this . _batchProcessingStrategy = batchProcessingStrategy ?? "parallel" ;
62+ }
63+
5564 public async call (
5665 environment : AuthenticatedEnvironment ,
5766 body : BatchTriggerTaskV2RequestBody ,
@@ -452,14 +461,14 @@ export class BatchTriggerV2Service extends BaseService {
452461 } ,
453462 } ) ;
454463
455- switch ( CURRENT_STRATEGY ) {
464+ switch ( this . _batchProcessingStrategy ) {
456465 case "sequential" : {
457466 await this . #enqueueBatchTaskRun( {
458467 batchId : batch . id ,
459468 processingId : batchId ,
460469 range : { start : 0 , count : PROCESSING_BATCH_SIZE } ,
461470 attemptCount : 0 ,
462- strategy : CURRENT_STRATEGY ,
471+ strategy : this . _batchProcessingStrategy ,
463472 } ) ;
464473
465474 break ;
@@ -480,7 +489,7 @@ export class BatchTriggerV2Service extends BaseService {
480489 processingId : `${ index } ` ,
481490 range,
482491 attemptCount : 0 ,
483- strategy : CURRENT_STRATEGY ,
492+ strategy : this . _batchProcessingStrategy ,
484493 } ,
485494 tx
486495 )
@@ -539,6 +548,16 @@ export class BatchTriggerV2Service extends BaseService {
539548
540549 const $attemptCount = options . attemptCount + 1 ;
541550
551+ // Add early return if max attempts reached
552+ if ( $attemptCount > MAX_ATTEMPTS ) {
553+ logger . error ( "[BatchTriggerV2][processBatchTaskRun] Max attempts reached" , {
554+ options,
555+ attemptCount : $attemptCount ,
556+ } ) ;
557+ // You might want to update the batch status to failed here
558+ return ;
559+ }
560+
542561 const batch = await this . _prisma . batchTaskRun . findFirst ( {
543562 where : { id : options . batchId } ,
544563 include : {
0 commit comments