@@ -194,7 +194,7 @@ func updateRepoRunsNumbers(ctx context.Context, repo *repo_model.Repository) err
194194
195195// CancelPreviousJobs cancels all previous jobs of the same repository, reference, workflow, and event.
196196// 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 ) {
198198 // Find all runs in the specified repository, reference, and workflow with non-final status
199199 runs , total , err := db .FindAndCount [ActionRun ](ctx , FindRunOptions {
200200 RepoID : repoID ,
@@ -204,22 +204,24 @@ func CancelPreviousJobs(ctx context.Context, repoID int64, ref, workflowID strin
204204 Status : []Status {StatusRunning , StatusWaiting , StatusBlocked },
205205 })
206206 if err != nil {
207- return err
207+ return nil , err
208208 }
209209
210210 // If there are no runs found, there's no need to proceed with cancellation, so return nil.
211211 if total == 0 {
212- return nil
212+ return nil , nil
213213 }
214214
215+ cancelledJobs := make ([]* ActionRunJob , 0 , total )
216+
215217 // Iterate over each found run and cancel its associated jobs.
216218 for _ , run := range runs {
217219 // Find all jobs associated with the current run.
218220 jobs , err := db .Find [ActionRunJob ](ctx , FindRunJobOptions {
219221 RunID : run .ID ,
220222 })
221223 if err != nil {
222- return err
224+ return cancelledJobs , err
223225 }
224226
225227 // Iterate over each job and attempt to cancel it.
@@ -238,27 +240,29 @@ func CancelPreviousJobs(ctx context.Context, repoID int64, ref, workflowID strin
238240 // Update the job's status and stopped time in the database.
239241 n , err := UpdateRunJob (ctx , job , builder.Eq {"task_id" : 0 }, "status" , "stopped" )
240242 if err != nil {
241- return err
243+ return cancelledJobs , err
242244 }
243245
244246 // If the update affected 0 rows, it means the job has changed in the meantime, so we need to try again.
245247 if n == 0 {
246- return fmt .Errorf ("job has changed, try again" )
248+ return cancelledJobs , fmt .Errorf ("job has changed, try again" )
247249 }
248250
251+ cancelledJobs = append (cancelledJobs , job )
249252 // Continue with the next job.
250253 continue
251254 }
252255
253256 // If the job has an associated task, try to stop the task, effectively cancelling the job.
254257 if err := StopTask (ctx , job .TaskID , StatusCancelled ); err != nil {
255- return err
258+ return cancelledJobs , err
256259 }
260+ cancelledJobs = append (cancelledJobs , job )
257261 }
258262 }
259263
260264 // Return nil to indicate successful cancellation of all running and waiting jobs.
261- return nil
265+ return cancelledJobs , nil
262266}
263267
264268// InsertRun inserts a run
0 commit comments