@@ -194,7 +194,7 @@ func updateRepoRunsNumbers(ctx context.Context, repo *repo_model.Repository) err
194
194
195
195
// CancelPreviousJobs cancels all previous jobs of the same repository, reference, workflow, and event.
196
196
// It's useful when a new run is triggered, and all previous runs needn't be continued anymore.
197
- func CancelPreviousJobs (ctx context.Context , repoID int64 , ref , workflowID string , event webhook_module.HookEventType ) error {
197
+ func CancelPreviousJobs (ctx context.Context , repoID int64 , ref , workflowID string , event webhook_module.HookEventType ) ([] * ActionRunJob , error ) {
198
198
// Find all runs in the specified repository, reference, and workflow with non-final status
199
199
runs , total , err := db .FindAndCount [ActionRun ](ctx , FindRunOptions {
200
200
RepoID : repoID ,
@@ -204,22 +204,24 @@ func CancelPreviousJobs(ctx context.Context, repoID int64, ref, workflowID strin
204
204
Status : []Status {StatusRunning , StatusWaiting , StatusBlocked },
205
205
})
206
206
if err != nil {
207
- return err
207
+ return nil , err
208
208
}
209
209
210
210
// If there are no runs found, there's no need to proceed with cancellation, so return nil.
211
211
if total == 0 {
212
- return nil
212
+ return nil , nil
213
213
}
214
214
215
+ cancelledJobs := make ([]* ActionRunJob , 0 , total )
216
+
215
217
// Iterate over each found run and cancel its associated jobs.
216
218
for _ , run := range runs {
217
219
// Find all jobs associated with the current run.
218
220
jobs , err := db .Find [ActionRunJob ](ctx , FindRunJobOptions {
219
221
RunID : run .ID ,
220
222
})
221
223
if err != nil {
222
- return err
224
+ return cancelledJobs , err
223
225
}
224
226
225
227
// Iterate over each job and attempt to cancel it.
@@ -238,27 +240,29 @@ func CancelPreviousJobs(ctx context.Context, repoID int64, ref, workflowID strin
238
240
// Update the job's status and stopped time in the database.
239
241
n , err := UpdateRunJob (ctx , job , builder.Eq {"task_id" : 0 }, "status" , "stopped" )
240
242
if err != nil {
241
- return err
243
+ return cancelledJobs , err
242
244
}
243
245
244
246
// If the update affected 0 rows, it means the job has changed in the meantime, so we need to try again.
245
247
if n == 0 {
246
- return fmt .Errorf ("job has changed, try again" )
248
+ return cancelledJobs , fmt .Errorf ("job has changed, try again" )
247
249
}
248
250
251
+ cancelledJobs = append (cancelledJobs , job )
249
252
// Continue with the next job.
250
253
continue
251
254
}
252
255
253
256
// If the job has an associated task, try to stop the task, effectively cancelling the job.
254
257
if err := StopTask (ctx , job .TaskID , StatusCancelled ); err != nil {
255
- return err
258
+ return cancelledJobs , err
256
259
}
260
+ cancelledJobs = append (cancelledJobs , job )
257
261
}
258
262
}
259
263
260
264
// Return nil to indicate successful cancellation of all running and waiting jobs.
261
- return nil
265
+ return cancelledJobs , nil
262
266
}
263
267
264
268
// InsertRun inserts a run
0 commit comments