Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement auto-cancellation of concurrent jobs if the event is push #25716

Merged
merged 28 commits into from Jul 25, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
a20c5c3
feat: implement auto-cancellation of concurrent jobs
appleboy Jul 6, 2023
a1b22e2
refactor: refactor status field to support multiple values
appleboy Jul 6, 2023
f066eb0
refactor: optimize job cancellation and action run indexing
appleboy Jul 7, 2023
b64ed17
refactor: refactor job cancellation process
appleboy Jul 7, 2023
e318f28
Merge branch 'main' into cancel
appleboy Jul 7, 2023
251be01
Merge branch 'main' into cancel
appleboy Jul 7, 2023
edeb7af
Merge branch 'main' into cancel
appleboy Jul 8, 2023
2db7378
Update models/actions/run.go
appleboy Jul 12, 2023
f383d9e
Merge branch 'main' into cancel
appleboy Jul 12, 2023
1b16a52
Merge branch 'main' into cancel
lunny Jul 13, 2023
a7ed008
refactor: refactor code to use `WorkflowID` instead of `WorkflowFileN…
appleboy Jul 21, 2023
18cd66f
refactor: refine job cancellation and notification logic
appleboy Jul 21, 2023
05f078d
refactor: refactor job cancellation in workflows
appleboy Jul 21, 2023
6b6b17c
Merge branch 'main' into cancel
appleboy Jul 21, 2023
231e237
refactor: improve code readability and test robustness
appleboy Jul 21, 2023
c542d1b
feat: implement migration to update actions ref index
appleboy Jul 21, 2023
d3edc58
Merge branch 'main' into cancel
appleboy Jul 22, 2023
2412e84
refactor: refactor notifier helper and improve job handling
appleboy Jul 22, 2023
5d78596
refactor: refactor status filter handling in `actions.go`
appleboy Jul 22, 2023
b0ab570
Merge branch 'main' into cancel
appleboy Jul 22, 2023
d7b577f
Apply suggestions from code review
wolfogre Jul 24, 2023
1af0878
Merge branch 'main' into cancel
GiteaBot Jul 24, 2023
e767742
Update models/actions/run.go
appleboy Jul 24, 2023
9ab6ec4
Update models/actions/run_list.go
appleboy Jul 24, 2023
d649e95
Update models/migrations/v1_21/v267.go
appleboy Jul 24, 2023
face538
Update services/actions/notifier_helper.go
wolfogre Jul 25, 2023
8a05c67
chore: update migration version and rename file
appleboy Jul 25, 2023
a79384d
Merge branch 'main' into cancel
appleboy Jul 25, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
50 changes: 49 additions & 1 deletion models/actions/run.go
Expand Up @@ -34,7 +34,7 @@ type ActionRun struct {
Index int64 `xorm:"index unique(repo_index)"` // a unique number for each run of a repository
TriggerUserID int64 `xorm:"index"`
TriggerUser *user_model.User `xorm:"-"`
Ref string
Ref string `xorm:"index"` // the ref of the run
appleboy marked this conversation as resolved.
Show resolved Hide resolved
appleboy marked this conversation as resolved.
Show resolved Hide resolved
CommitSHA string
IsForkPullRequest bool // If this is triggered by a PR from a forked repository or an untrusted user, we need to check if it is approved and limit permissions when running the workflow.
NeedApproval bool // may need approval if it's a fork pull request
Expand Down Expand Up @@ -164,6 +164,54 @@ func updateRepoRunsNumbers(ctx context.Context, repo *repo_model.Repository) err
return err
}

// CancelRunningJobs cancels all running jobs of a run
func CancelRunningJobs(ctx context.Context, run *ActionRun) error {
wolfogre marked this conversation as resolved.
Show resolved Hide resolved
runs, total, err := FindRuns(ctx, FindRunOptions{
RepoID: run.RepoID,
Ref: run.Ref,
Status: []Status{StatusRunning, StatusWaiting},
})
if err != nil {
return err
}

if total == 0 {
return nil
}

for _, run := range runs {
// cancel all running jobs
jobs, _, err := FindRunJobs(ctx, FindRunJobOptions{
RunID: run.ID,
})
if err != nil {
return err
}
for _, job := range jobs {
status := job.Status
if status.IsDone() {
continue
}
if job.TaskID == 0 {
job.Status = StatusCancelled
job.Stopped = timeutil.TimeStampNow()
n, err := UpdateRunJob(ctx, job, builder.Eq{"task_id": 0}, "status", "stopped")
if err != nil {
return err
}
if n == 0 {
return fmt.Errorf("job has changed, try again")
}
continue
}
if err := StopTask(ctx, job.TaskID, StatusCancelled); err != nil {
return err
}
}
}
return nil
}

// InsertRun inserts a run
func InsertRun(ctx context.Context, run *ActionRun, jobs []*jobparser.SingleWorkflow) error {
ctx, commiter, err := db.TxContext(ctx)
Expand Down
10 changes: 7 additions & 3 deletions models/actions/run_list.go
Expand Up @@ -69,9 +69,10 @@ type FindRunOptions struct {
RepoID int64
OwnerID int64
WorkflowFileName string
Ref string
wolfogre marked this conversation as resolved.
Show resolved Hide resolved
TriggerUserID int64
Approved bool // not util.OptionalBool, it works only when it's true
Status Status
Status []Status
}

func (opts FindRunOptions) toConds() builder.Cond {
Expand All @@ -91,8 +92,11 @@ func (opts FindRunOptions) toConds() builder.Cond {
if opts.Approved {
cond = cond.And(builder.Gt{"approved_by": 0})
}
if opts.Status > StatusUnknown {
cond = cond.And(builder.Eq{"status": opts.Status})
if len(opts.Status) > 0 {
cond = cond.And(builder.In("status", opts.Status))
}
if opts.Ref != "" {
cond = cond.And(builder.Eq{"ref": opts.Ref})
}
return cond
}
Expand Down
2 changes: 1 addition & 1 deletion routers/web/repo/actions/actions.go
Expand Up @@ -146,7 +146,7 @@ func List(ctx *context.Context) {
RepoID: ctx.Repo.Repository.ID,
WorkflowFileName: workflow,
TriggerUserID: actorID,
Status: actions_model.Status(status),
Status: []actions_model.Status{actions_model.Status(status)},
}

runs, total, err := actions_model.FindRuns(ctx, opts)
Expand Down
6 changes: 6 additions & 0 deletions services/actions/notifier_helper.go
Expand Up @@ -230,6 +230,12 @@ func notify(ctx context.Context, input *notifyInput) error {
log.Error("jobparser.Parse: %v", err)
continue
}

// auto cancel running jobs in the same workflow
if err := actions_model.CancelRunningJobs(ctx, run); err != nil {
Copy link
Member

@wolfogre wolfogre Jul 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So it will cancel the previous jobs of the same ref by default, I'm OK with this, and we can provide an options later if someone ask for it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wolfogre Yes, move to another NEW PR.

log.Error("CancelRunningJobs: %v", err)
}

if err := actions_model.InsertRun(ctx, run, jobs); err != nil {
log.Error("InsertRun: %v", err)
continue
Expand Down