diff options
Diffstat (limited to 'services/actions/clear_tasks.go')
-rw-r--r-- | services/actions/clear_tasks.go | 39 |
1 files changed, 37 insertions, 2 deletions
diff --git a/services/actions/clear_tasks.go b/services/actions/clear_tasks.go index 67373782d5..274c04aa57 100644 --- a/services/actions/clear_tasks.go +++ b/services/actions/clear_tasks.go @@ -10,10 +10,13 @@ import ( actions_model "code.gitea.io/gitea/models/actions" "code.gitea.io/gitea/models/db" + repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/modules/actions" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/timeutil" + webhook_module "code.gitea.io/gitea/modules/webhook" + notify_service "code.gitea.io/gitea/services/notify" ) // StopZombieTasks stops the task which have running status, but haven't been updated for a long time @@ -32,6 +35,32 @@ func StopEndlessTasks(ctx context.Context) error { }) } +func notifyWorkflowJobStatusUpdate(ctx context.Context, jobs []*actions_model.ActionRunJob) { + if len(jobs) > 0 { + CreateCommitStatus(ctx, jobs...) + for _, job := range jobs { + _ = job.LoadAttributes(ctx) + notify_service.WorkflowJobStatusUpdate(ctx, job.Run.Repo, job.Run.TriggerUser, job, nil) + } + if len(jobs) > 0 { + job := jobs[0] + notify_service.WorkflowRunStatusUpdate(ctx, job.Run.Repo, job.Run.TriggerUser, job.Run) + } + } +} + +func CancelPreviousJobs(ctx context.Context, repoID int64, ref, workflowID string, event webhook_module.HookEventType) error { + jobs, err := actions_model.CancelPreviousJobs(ctx, repoID, ref, workflowID, event) + notifyWorkflowJobStatusUpdate(ctx, jobs) + return err +} + +func CleanRepoScheduleTasks(ctx context.Context, repo *repo_model.Repository) error { + jobs, err := actions_model.CleanRepoScheduleTasks(ctx, repo) + notifyWorkflowJobStatusUpdate(ctx, jobs) + return err +} + func stopTasks(ctx context.Context, opts actions_model.FindTaskOptions) error { tasks, err := db.Find[actions_model.ActionTask](ctx, opts) if err != nil { @@ -67,7 +96,7 @@ func stopTasks(ctx context.Context, opts actions_model.FindTaskOptions) error { remove() } - CreateCommitStatus(ctx, jobs...) + notifyWorkflowJobStatusUpdate(ctx, jobs) return nil } @@ -87,14 +116,20 @@ func CancelAbandonedJobs(ctx context.Context) error { for _, job := range jobs { job.Status = actions_model.StatusCancelled job.Stopped = now + updated := false if err := db.WithTx(ctx, func(ctx context.Context) error { - _, err := actions_model.UpdateRunJob(ctx, job, nil, "status", "stopped") + n, err := actions_model.UpdateRunJob(ctx, job, nil, "status", "stopped") + updated = err == nil && n > 0 return err }); err != nil { log.Warn("cancel abandoned job %v: %v", job.ID, err) // go on } CreateCommitStatus(ctx, job) + if updated { + NotifyWorkflowRunStatusUpdateWithReload(ctx, job) + notify_service.WorkflowJobStatusUpdate(ctx, job.Run.Repo, job.Run.TriggerUser, job, nil) + } } return nil |