@@ -13,50 +13,55 @@ export class ResumeBatchRunService extends BaseService {
1313 id : batchRunId ,
1414 } ,
1515 include : {
16- dependentTaskAttempt : {
16+ runtimeEnvironment : {
1717 include : {
18- runtimeEnvironment : {
19- include : {
20- project : true ,
21- organization : true ,
22- } ,
23- } ,
24- taskRun : true ,
18+ project : true ,
19+ organization : true ,
20+ } ,
21+ } ,
22+ items : {
23+ select : {
24+ status : true ,
25+ taskRunAttemptId : true ,
2526 } ,
2627 } ,
27- items : true ,
2828 } ,
2929 } ) ;
3030
31- if ( ! batchRun || ! batchRun . dependentTaskAttempt ) {
31+ if ( ! batchRun ) {
3232 logger . error (
3333 "ResumeBatchRunService: Batch run doesn't exist or doesn't have a dependent attempt" ,
3434 {
35- batchRun ,
35+ batchRunId ,
3636 }
3737 ) ;
3838 return ;
3939 }
4040
4141 if ( batchRun . status === "COMPLETED" ) {
4242 logger . debug ( "ResumeBatchRunService: Batch run is already completed" , {
43- batchRun : batchRun ,
43+ batchRunId : batchRun . id ,
44+ batchRun : {
45+ id : batchRun . id ,
46+ status : batchRun . status ,
47+ } ,
4448 } ) ;
4549 return ;
4650 }
4751
4852 if ( batchRun . items . some ( ( item ) => ! finishedBatchRunStatuses . includes ( item . status ) ) ) {
4953 logger . debug ( "ResumeBatchRunService: All items aren't yet completed" , {
50- batchRun : batchRun ,
54+ batchRunId : batchRun . id ,
55+ batchRun : {
56+ id : batchRun . id ,
57+ status : batchRun . status ,
58+ } ,
5159 } ) ;
5260 return ;
5361 }
5462
55- // This batch has a dependent attempt and just finalized, we should resume that attempt
56- const environment = batchRun . dependentTaskAttempt . runtimeEnvironment ;
57-
58- // If we are in development, we don't need to resume the dependent task (that will happen automatically)
59- if ( environment . type === "DEVELOPMENT" ) {
63+ // If we are in development, or there is no dependent attempt, we can just mark the batch as completed and return
64+ if ( batchRun . runtimeEnvironment . type === "DEVELOPMENT" || ! batchRun . dependentTaskAttemptId ) {
6065 // We need to update the batchRun status so we don't resume it again
6166 await this . _prisma . batchTaskRun . update ( {
6267 where : {
@@ -69,12 +74,42 @@ export class ResumeBatchRunService extends BaseService {
6974 return ;
7075 }
7176
72- const dependentRun = batchRun . dependentTaskAttempt . taskRun ;
77+ const dependentTaskAttempt = await this . _prisma . taskRunAttempt . findFirst ( {
78+ where : {
79+ id : batchRun . dependentTaskAttemptId ,
80+ } ,
81+ select : {
82+ status : true ,
83+ id : true ,
84+ taskRun : {
85+ select : {
86+ id : true ,
87+ queue : true ,
88+ taskIdentifier : true ,
89+ concurrencyKey : true ,
90+ } ,
91+ } ,
92+ } ,
93+ } ) ;
94+
95+ if ( ! dependentTaskAttempt ) {
96+ logger . error ( "ResumeBatchRunService: Dependent attempt not found" , {
97+ batchRunId : batchRun . id ,
98+ dependentTaskAttemptId : batchRun . dependentTaskAttemptId ,
99+ } ) ;
100+
101+ return ;
102+ }
103+
104+ // This batch has a dependent attempt and just finalized, we should resume that attempt
105+ const environment = batchRun . runtimeEnvironment ;
106+
107+ const dependentRun = dependentTaskAttempt . taskRun ;
73108
74- if ( batchRun . dependentTaskAttempt . status === "PAUSED" && batchRun . checkpointEventId ) {
109+ if ( dependentTaskAttempt . status === "PAUSED" && batchRun . checkpointEventId ) {
75110 logger . debug ( "ResumeBatchRunService: Attempt is paused and has a checkpoint event" , {
76111 batchRunId : batchRun . id ,
77- dependentTaskAttempt : batchRun . dependentTaskAttempt ,
112+ dependentTaskAttempt : dependentTaskAttempt ,
78113 checkpointEventId : batchRun . checkpointEventId ,
79114 } ) ;
80115
@@ -83,7 +118,7 @@ export class ResumeBatchRunService extends BaseService {
83118 if ( wasUpdated ) {
84119 logger . debug ( "ResumeBatchRunService: Resuming dependent run with checkpoint" , {
85120 batchRunId : batchRun . id ,
86- dependentTaskAttemptId : batchRun . dependentTaskAttempt . id ,
121+ dependentTaskAttemptId : dependentTaskAttempt . id ,
87122 } ) ;
88123 await marqs ?. enqueueMessage (
89124 environment ,
@@ -92,37 +127,37 @@ export class ResumeBatchRunService extends BaseService {
92127 {
93128 type : "RESUME" ,
94129 completedAttemptIds : [ ] ,
95- resumableAttemptId : batchRun . dependentTaskAttempt . id ,
130+ resumableAttemptId : dependentTaskAttempt . id ,
96131 checkpointEventId : batchRun . checkpointEventId ,
97- taskIdentifier : batchRun . dependentTaskAttempt . taskRun . taskIdentifier ,
98- projectId : batchRun . dependentTaskAttempt . runtimeEnvironment . projectId ,
99- environmentId : batchRun . dependentTaskAttempt . runtimeEnvironment . id ,
100- environmentType : batchRun . dependentTaskAttempt . runtimeEnvironment . type ,
132+ taskIdentifier : dependentTaskAttempt . taskRun . taskIdentifier ,
133+ projectId : environment . projectId ,
134+ environmentId : environment . id ,
135+ environmentType : environment . type ,
101136 } ,
102137 dependentRun . concurrencyKey ?? undefined
103138 ) ;
104139 } else {
105140 logger . debug ( "ResumeBatchRunService: with checkpoint was already completed" , {
106141 batchRunId : batchRun . id ,
107- dependentTaskAttempt : batchRun . dependentTaskAttempt ,
142+ dependentTaskAttempt : dependentTaskAttempt ,
108143 checkpointEventId : batchRun . checkpointEventId ,
109144 hasCheckpointEvent : ! ! batchRun . checkpointEventId ,
110145 } ) ;
111146 }
112147 } else {
113148 logger . debug ( "ResumeBatchRunService: attempt is not paused or there's no checkpoint event" , {
114149 batchRunId : batchRun . id ,
115- dependentTaskAttempt : batchRun . dependentTaskAttempt ,
150+ dependentTaskAttempt : dependentTaskAttempt ,
116151 checkpointEventId : batchRun . checkpointEventId ,
117152 hasCheckpointEvent : ! ! batchRun . checkpointEventId ,
118153 } ) ;
119154
120- if ( batchRun . dependentTaskAttempt . status === "PAUSED" && ! batchRun . checkpointEventId ) {
155+ if ( dependentTaskAttempt . status === "PAUSED" && ! batchRun . checkpointEventId ) {
121156 // In case of race conditions the status can be PAUSED without a checkpoint event
122157 // When the checkpoint is created, it will continue the run
123158 logger . error ( "ResumeBatchRunService: attempt is paused but there's no checkpoint event" , {
124159 batchRunId : batchRun . id ,
125- dependentTaskAttempt : batchRun . dependentTaskAttempt ,
160+ dependentTaskAttempt : dependentTaskAttempt ,
126161 checkpointEventId : batchRun . checkpointEventId ,
127162 hasCheckpointEvent : ! ! batchRun . checkpointEventId ,
128163 } ) ;
@@ -134,24 +169,24 @@ export class ResumeBatchRunService extends BaseService {
134169 if ( wasUpdated ) {
135170 logger . debug ( "ResumeBatchRunService: Resuming dependent run without checkpoint" , {
136171 batchRunId : batchRun . id ,
137- dependentTaskAttempt : batchRun . dependentTaskAttempt ,
172+ dependentTaskAttempt : dependentTaskAttempt ,
138173 checkpointEventId : batchRun . checkpointEventId ,
139174 hasCheckpointEvent : ! ! batchRun . checkpointEventId ,
140175 } ) ;
141176 await marqs ?. replaceMessage ( dependentRun . id , {
142177 type : "RESUME" ,
143178 completedAttemptIds : batchRun . items . map ( ( item ) => item . taskRunAttemptId ) . filter ( Boolean ) ,
144- resumableAttemptId : batchRun . dependentTaskAttempt . id ,
179+ resumableAttemptId : dependentTaskAttempt . id ,
145180 checkpointEventId : batchRun . checkpointEventId ?? undefined ,
146- taskIdentifier : batchRun . dependentTaskAttempt . taskRun . taskIdentifier ,
147- projectId : batchRun . dependentTaskAttempt . runtimeEnvironment . projectId ,
148- environmentId : batchRun . dependentTaskAttempt . runtimeEnvironment . id ,
149- environmentType : batchRun . dependentTaskAttempt . runtimeEnvironment . type ,
181+ taskIdentifier : dependentTaskAttempt . taskRun . taskIdentifier ,
182+ projectId : environment . projectId ,
183+ environmentId : environment . id ,
184+ environmentType : environment . type ,
150185 } ) ;
151186 } else {
152187 logger . debug ( "ResumeBatchRunService: without checkpoint was already completed" , {
153188 batchRunId : batchRun . id ,
154- dependentTaskAttempt : batchRun . dependentTaskAttempt ,
189+ dependentTaskAttempt : dependentTaskAttempt ,
155190 checkpointEventId : batchRun . checkpointEventId ,
156191 hasCheckpointEvent : ! ! batchRun . checkpointEventId ,
157192 } ) ;
0 commit comments