@@ -13,50 +13,55 @@ export class ResumeBatchRunService extends BaseService {
13
13
id : batchRunId ,
14
14
} ,
15
15
include : {
16
- dependentTaskAttempt : {
16
+ runtimeEnvironment : {
17
17
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 ,
25
26
} ,
26
27
} ,
27
- items : true ,
28
28
} ,
29
29
} ) ;
30
30
31
- if ( ! batchRun || ! batchRun . dependentTaskAttempt ) {
31
+ if ( ! batchRun ) {
32
32
logger . error (
33
33
"ResumeBatchRunService: Batch run doesn't exist or doesn't have a dependent attempt" ,
34
34
{
35
- batchRun ,
35
+ batchRunId ,
36
36
}
37
37
) ;
38
38
return ;
39
39
}
40
40
41
41
if ( batchRun . status === "COMPLETED" ) {
42
42
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
+ } ,
44
48
} ) ;
45
49
return ;
46
50
}
47
51
48
52
if ( batchRun . items . some ( ( item ) => ! finishedBatchRunStatuses . includes ( item . status ) ) ) {
49
53
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
+ } ,
51
59
} ) ;
52
60
return ;
53
61
}
54
62
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 ) {
60
65
// We need to update the batchRun status so we don't resume it again
61
66
await this . _prisma . batchTaskRun . update ( {
62
67
where : {
@@ -69,12 +74,42 @@ export class ResumeBatchRunService extends BaseService {
69
74
return ;
70
75
}
71
76
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 ;
73
108
74
- if ( batchRun . dependentTaskAttempt . status === "PAUSED" && batchRun . checkpointEventId ) {
109
+ if ( dependentTaskAttempt . status === "PAUSED" && batchRun . checkpointEventId ) {
75
110
logger . debug ( "ResumeBatchRunService: Attempt is paused and has a checkpoint event" , {
76
111
batchRunId : batchRun . id ,
77
- dependentTaskAttempt : batchRun . dependentTaskAttempt ,
112
+ dependentTaskAttempt : dependentTaskAttempt ,
78
113
checkpointEventId : batchRun . checkpointEventId ,
79
114
} ) ;
80
115
@@ -83,7 +118,7 @@ export class ResumeBatchRunService extends BaseService {
83
118
if ( wasUpdated ) {
84
119
logger . debug ( "ResumeBatchRunService: Resuming dependent run with checkpoint" , {
85
120
batchRunId : batchRun . id ,
86
- dependentTaskAttemptId : batchRun . dependentTaskAttempt . id ,
121
+ dependentTaskAttemptId : dependentTaskAttempt . id ,
87
122
} ) ;
88
123
await marqs ?. enqueueMessage (
89
124
environment ,
@@ -92,37 +127,37 @@ export class ResumeBatchRunService extends BaseService {
92
127
{
93
128
type : "RESUME" ,
94
129
completedAttemptIds : [ ] ,
95
- resumableAttemptId : batchRun . dependentTaskAttempt . id ,
130
+ resumableAttemptId : dependentTaskAttempt . id ,
96
131
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 ,
101
136
} ,
102
137
dependentRun . concurrencyKey ?? undefined
103
138
) ;
104
139
} else {
105
140
logger . debug ( "ResumeBatchRunService: with checkpoint was already completed" , {
106
141
batchRunId : batchRun . id ,
107
- dependentTaskAttempt : batchRun . dependentTaskAttempt ,
142
+ dependentTaskAttempt : dependentTaskAttempt ,
108
143
checkpointEventId : batchRun . checkpointEventId ,
109
144
hasCheckpointEvent : ! ! batchRun . checkpointEventId ,
110
145
} ) ;
111
146
}
112
147
} else {
113
148
logger . debug ( "ResumeBatchRunService: attempt is not paused or there's no checkpoint event" , {
114
149
batchRunId : batchRun . id ,
115
- dependentTaskAttempt : batchRun . dependentTaskAttempt ,
150
+ dependentTaskAttempt : dependentTaskAttempt ,
116
151
checkpointEventId : batchRun . checkpointEventId ,
117
152
hasCheckpointEvent : ! ! batchRun . checkpointEventId ,
118
153
} ) ;
119
154
120
- if ( batchRun . dependentTaskAttempt . status === "PAUSED" && ! batchRun . checkpointEventId ) {
155
+ if ( dependentTaskAttempt . status === "PAUSED" && ! batchRun . checkpointEventId ) {
121
156
// In case of race conditions the status can be PAUSED without a checkpoint event
122
157
// When the checkpoint is created, it will continue the run
123
158
logger . error ( "ResumeBatchRunService: attempt is paused but there's no checkpoint event" , {
124
159
batchRunId : batchRun . id ,
125
- dependentTaskAttempt : batchRun . dependentTaskAttempt ,
160
+ dependentTaskAttempt : dependentTaskAttempt ,
126
161
checkpointEventId : batchRun . checkpointEventId ,
127
162
hasCheckpointEvent : ! ! batchRun . checkpointEventId ,
128
163
} ) ;
@@ -134,24 +169,24 @@ export class ResumeBatchRunService extends BaseService {
134
169
if ( wasUpdated ) {
135
170
logger . debug ( "ResumeBatchRunService: Resuming dependent run without checkpoint" , {
136
171
batchRunId : batchRun . id ,
137
- dependentTaskAttempt : batchRun . dependentTaskAttempt ,
172
+ dependentTaskAttempt : dependentTaskAttempt ,
138
173
checkpointEventId : batchRun . checkpointEventId ,
139
174
hasCheckpointEvent : ! ! batchRun . checkpointEventId ,
140
175
} ) ;
141
176
await marqs ?. replaceMessage ( dependentRun . id , {
142
177
type : "RESUME" ,
143
178
completedAttemptIds : batchRun . items . map ( ( item ) => item . taskRunAttemptId ) . filter ( Boolean ) ,
144
- resumableAttemptId : batchRun . dependentTaskAttempt . id ,
179
+ resumableAttemptId : dependentTaskAttempt . id ,
145
180
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 ,
150
185
} ) ;
151
186
} else {
152
187
logger . debug ( "ResumeBatchRunService: without checkpoint was already completed" , {
153
188
batchRunId : batchRun . id ,
154
- dependentTaskAttempt : batchRun . dependentTaskAttempt ,
189
+ dependentTaskAttempt : dependentTaskAttempt ,
155
190
checkpointEventId : batchRun . checkpointEventId ,
156
191
hasCheckpointEvent : ! ! batchRun . checkpointEventId ,
157
192
} ) ;
0 commit comments