Skip to content

Commit f797bc7

Browse files
committed
Move rerunJob logic to shared actions_service.RerunJob function for reuse across API and WEBUI
1 parent b8ab911 commit f797bc7

File tree

3 files changed

+63
-82
lines changed

3 files changed

+63
-82
lines changed

routers/api/v1/repo/actions_run.go

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,12 @@ func RerunWorkflowRun(ctx *context.APIContext) {
141141
for _, job := range jobs {
142142
// If the job has needs, it should be set to "blocked" status to wait for other jobs
143143
shouldBlock := len(job.Needs) > 0
144-
if err := rerunJob(ctx, job, shouldBlock); err != nil {
144+
if err := actions_service.RerunJob(ctx, job, shouldBlock); err != nil {
145145
ctx.APIErrorInternal(err)
146146
return
147147
}
148+
actions_service.NotifyWorkflowRunStatusUpdateWithReload(ctx, job)
149+
notify_service.WorkflowJobStatusUpdate(ctx, job.Run.Repo, job.Run.TriggerUser, job, nil)
148150
}
149151

150152
ctx.Status(200)
@@ -431,10 +433,12 @@ func RerunWorkflowJob(ctx *context.APIContext) {
431433
for _, j := range rerunJobs {
432434
// Jobs other than the specified one should be set to "blocked" status
433435
shouldBlock := j.JobID != job.JobID
434-
if err := rerunJob(ctx, j, shouldBlock); err != nil {
436+
if err := actions_service.RerunJob(ctx, j, shouldBlock); err != nil {
435437
ctx.APIErrorInternal(err)
436438
return
437439
}
440+
actions_service.NotifyWorkflowRunStatusUpdateWithReload(ctx, j)
441+
notify_service.WorkflowJobStatusUpdate(ctx, j.Run.Repo, j.Run.TriggerUser, j, nil)
438442
}
439443

440444
ctx.Status(200)
@@ -487,38 +491,7 @@ func getRunJobsAndCurrent(ctx *context.APIContext, runID, jobIndex int64) (*acti
487491
return jobs[0], jobs, nil
488492
}
489493

490-
func rerunJob(ctx *context.APIContext, job *actions_model.ActionRunJob, shouldBlock bool) error {
491-
if job.Run == nil {
492-
if err := job.LoadRun(ctx); err != nil {
493-
return err
494-
}
495-
}
496-
status := job.Status
497-
if !status.IsDone() || !job.Run.Status.IsDone() {
498-
return nil
499-
}
500494

501-
job.TaskID = 0
502-
job.Status = actions_model.StatusWaiting
503-
if shouldBlock {
504-
job.Status = actions_model.StatusBlocked
505-
}
506-
job.Started = 0
507-
job.Stopped = 0
508-
509-
if err := db.WithTx(ctx, func(ctx stdCtx.Context) error {
510-
_, err := actions_model.UpdateRunJob(ctx, job, nil, "task_id", "status", "started", "stopped")
511-
return err
512-
}); err != nil {
513-
return err
514-
}
515-
516-
actions_service.CreateCommitStatusForRunJobs(ctx, job.Run, job)
517-
actions_service.NotifyWorkflowRunStatusUpdateWithReload(ctx, job)
518-
notify_service.WorkflowJobStatusUpdate(ctx, job.Run.Repo, job.Run.TriggerUser, job, nil)
519-
520-
return nil
521-
}
522495

523496
// LogCursor represents a log cursor position
524497
type LogCursor struct {

routers/web/repo/actions/view.go

Lines changed: 4 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -476,10 +476,11 @@ func Rerun(ctx *context_module.Context) {
476476
for _, j := range jobs {
477477
// if the job has needs, it should be set to "blocked" status to wait for other jobs
478478
shouldBlockJob := len(j.Needs) > 0 || isRunBlocked
479-
if err := rerunJob(ctx, j, shouldBlockJob); err != nil {
479+
if err := actions_service.RerunJob(ctx, j, shouldBlockJob); err != nil {
480480
ctx.ServerError("RerunJob", err)
481481
return
482482
}
483+
notify_service.WorkflowJobStatusUpdate(ctx, j.Run.Repo, j.Run.TriggerUser, j, nil)
483484
}
484485
ctx.JSONOK()
485486
return
@@ -490,63 +491,17 @@ func Rerun(ctx *context_module.Context) {
490491
for _, j := range rerunJobs {
491492
// jobs other than the specified one should be set to "blocked" status
492493
shouldBlockJob := j.JobID != job.JobID || isRunBlocked
493-
if err := rerunJob(ctx, j, shouldBlockJob); err != nil {
494+
if err := actions_service.RerunJob(ctx, j, shouldBlockJob); err != nil {
494495
ctx.ServerError("RerunJob", err)
495496
return
496497
}
498+
notify_service.WorkflowJobStatusUpdate(ctx, j.Run.Repo, j.Run.TriggerUser, j, nil)
497499
}
498500

499501
ctx.JSONOK()
500502
}
501503

502-
func rerunJob(ctx *context_module.Context, job *actions_model.ActionRunJob, shouldBlock bool) error {
503-
status := job.Status
504-
if !status.IsDone() || !job.Run.Status.IsDone() {
505-
return nil
506-
}
507-
508-
job.TaskID = 0
509-
job.Status = util.Iif(shouldBlock, actions_model.StatusBlocked, actions_model.StatusWaiting)
510-
job.Started = 0
511-
job.Stopped = 0
512-
513-
job.ConcurrencyGroup = ""
514-
job.ConcurrencyCancel = false
515-
job.IsConcurrencyEvaluated = false
516-
if err := job.LoadRun(ctx); err != nil {
517-
return err
518-
}
519-
520-
vars, err := actions_model.GetVariablesOfRun(ctx, job.Run)
521-
if err != nil {
522-
return fmt.Errorf("get run %d variables: %w", job.Run.ID, err)
523-
}
524-
525-
if job.RawConcurrency != "" && !shouldBlock {
526-
err = actions_service.EvaluateJobConcurrencyFillModel(ctx, job.Run, job, vars)
527-
if err != nil {
528-
return fmt.Errorf("evaluate job concurrency: %w", err)
529-
}
530504

531-
job.Status, err = actions_service.PrepareToStartJobWithConcurrency(ctx, job)
532-
if err != nil {
533-
return err
534-
}
535-
}
536-
537-
if err := db.WithTx(ctx, func(ctx context.Context) error {
538-
updateCols := []string{"task_id", "status", "started", "stopped", "concurrency_group", "concurrency_cancel", "is_concurrency_evaluated"}
539-
_, err := actions_model.UpdateRunJob(ctx, job, builder.Eq{"status": status}, updateCols...)
540-
return err
541-
}); err != nil {
542-
return err
543-
}
544-
545-
actions_service.CreateCommitStatusForRunJobs(ctx, job.Run, job)
546-
notify_service.WorkflowJobStatusUpdate(ctx, job.Run.Repo, job.Run.TriggerUser, job, nil)
547-
548-
return nil
549-
}
550505

551506
func Logs(ctx *context_module.Context) {
552507
runIndex := getRunIndex(ctx)

services/actions/rerun.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@ package actions
55

66
import (
77
"context"
8+
"fmt"
89

910
actions_model "code.gitea.io/gitea/models/actions"
11+
"code.gitea.io/gitea/models/db"
1012
"code.gitea.io/gitea/modules/container"
13+
"code.gitea.io/gitea/modules/util"
14+
"xorm.io/builder"
1115
)
1216

1317
// ResetRunTimes resets the start and stop times for a run when it is done, for rerun
@@ -21,6 +25,55 @@ func ResetRunTimes(ctx context.Context, run *actions_model.ActionRun) error {
2125
return nil
2226
}
2327

28+
// RerunJob reruns a job, handling concurrency and status updates
29+
func RerunJob(ctx context.Context, job *actions_model.ActionRunJob, shouldBlock bool) error {
30+
status := job.Status
31+
if !status.IsDone() || !job.Run.Status.IsDone() {
32+
return nil
33+
}
34+
35+
job.TaskID = 0
36+
job.Status = util.Iif(shouldBlock, actions_model.StatusBlocked, actions_model.StatusWaiting)
37+
job.Started = 0
38+
job.Stopped = 0
39+
40+
job.ConcurrencyGroup = ""
41+
job.ConcurrencyCancel = false
42+
job.IsConcurrencyEvaluated = false
43+
if err := job.LoadRun(ctx); err != nil {
44+
return err
45+
}
46+
47+
vars, err := actions_model.GetVariablesOfRun(ctx, job.Run)
48+
if err != nil {
49+
return fmt.Errorf("get run %d variables: %w", job.Run.ID, err)
50+
}
51+
52+
if job.RawConcurrency != "" && !shouldBlock {
53+
err = EvaluateJobConcurrencyFillModel(ctx, job.Run, job, vars)
54+
if err != nil {
55+
return fmt.Errorf("evaluate job concurrency: %w", err)
56+
}
57+
58+
job.Status, err = PrepareToStartJobWithConcurrency(ctx, job)
59+
if err != nil {
60+
return err
61+
}
62+
}
63+
64+
if err := db.WithTx(ctx, func(ctx context.Context) error {
65+
updateCols := []string{"task_id", "status", "started", "stopped", "concurrency_group", "concurrency_cancel", "is_concurrency_evaluated"}
66+
_, err := actions_model.UpdateRunJob(ctx, job, builder.Eq{"status": status}, updateCols...)
67+
return err
68+
}); err != nil {
69+
return err
70+
}
71+
72+
CreateCommitStatusForRunJobs(ctx, job.Run, job)
73+
74+
return nil
75+
}
76+
2477
// GetAllRerunJobs get all jobs that need to be rerun when job should be rerun
2578
func GetAllRerunJobs(job *actions_model.ActionRunJob, allJobs []*actions_model.ActionRunJob) []*actions_model.ActionRunJob {
2679
rerunJobs := []*actions_model.ActionRunJob{job}

0 commit comments

Comments
 (0)