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

Conversation

appleboy
Copy link
Member

@appleboy appleboy commented Jul 6, 2023

  • cancel running jobs if the event is push
  • Add a new function CancelRunningJobs to cancel all running jobs of a run
  • Update FindRunOptions struct to include Ref field and update its condition in toConds function
  • Implement auto cancellation of running jobs in the same workflow in notify function

related task: #22751

- Add a new function `CancelRunningJobs` to cancel all running jobs of a run
- Update `FindRunOptions` struct to include `Ref` field and update its condition in `toConds` function
- Implement auto cancellation of running jobs in the same workflow in `notify` function

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Jul 6, 2023
@pull-request-size pull-request-size bot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Jul 6, 2023
- Change the `Status` field in `CancelRunningJobs`, `FindRunOptions`, and `List` functions from a single `Status` to a slice of `Status`.
- Update the condition in `toConds` function to check the length of `Status` slice instead of comparing a single `Status` value with `StatusUnknown`.
- Modify the condition to use `builder.In` for matching multiple `Status` values instead of `builder.Eq` for a single `Status` value.

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
models/actions/run_list.go Outdated Show resolved Hide resolved
models/actions/run.go Outdated Show resolved Hide resolved
@@ -232,6 +232,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.

- Add an index to the `Ref` field in the `ActionRun` struct
- Modify the `CancelRunningJobs` function to use `total` instead of `len(runs)` for checking if there are no runs.

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
- Remove the code that sets the status of a run to cancelled and updates the run in `CancelRunningJobs` function.

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
@wolfogre
Copy link
Member

wolfogre commented Jul 7, 2023

Please wait for #25743

Done

models/actions/run.go Outdated Show resolved Hide resolved
@wolfogre
Copy link
Member

Please check #25716 (comment)

@lunny lunny added this to the 1.21.0 milestone Jul 21, 2023
@lunny lunny added type/feature Completely new functionality. Can only be merged if feature freeze is not active. topic/gitea-actions related to the actions of Gitea labels Jul 21, 2023
…ame`

- Add `WorkflowID` to the `CancelRunningJobs` function in `run.go`
- Replace `WorkflowFileName` with `WorkflowID` in `FindRunOptions` struct in `run_list.go`
- Update conditions in `toConds` function to use `WorkflowID` instead of `WorkflowFileName` in `run_list.go`
- Replace `WorkflowFileName` with `WorkflowID` in `List` function in `actions.go`

Signed-off-by: appleboy <appleboy.tw@gmail.com>
- Modify the comment for the `CancelRunningJobs` function to specify that it also marks unstarted jobs as cancelled
- Change the comment in `notify` function to specify that running jobs are cancelled only if the event is a push
- Add a condition in `notify` function to cancel running jobs of the same workflow only if the event is a push

Signed-off-by: appleboy <appleboy.tw@gmail.com>
models/actions/run.go Outdated Show resolved Hide resolved
- Update the `CancelRunningJobs` function to cancel all running and waiting jobs associated with a specific workflow
- Modify the function parameters to include `repoID`, `ref`, and `workflowID`
- Add checks and conditions to handle different job states during cancellation
- Update the call to `CancelRunningJobs` in `notifier_helper.go` to match the new function signature

Signed-off-by: appleboy <appleboy.tw@gmail.com>
@pull-request-size pull-request-size bot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Jul 21, 2023
- Refactor the `CancelRunningJobs` function parameters for better readability.

Signed-off-by: appleboy <appleboy.tw@gmail.com>
- Add a new migration "Update Action Ref" to the migrations list
- Create a new file `v267.go` in `models/migrations/v1_21` directory
- Define a new function `UpdateActionsRefIndex` in `v267.go` to update the index of actions ref field
- The `UpdateActionsRefIndex` function syncs a new instance of `ActionRun` struct which includes fields like `ID`, `Title`, `RepoID`, `OwnerID`, `WorkflowID`, `Index`, `TriggerUserID`, `Ref`, `CommitSHA`, `IsForkPullRequest`, `NeedApproval`, `ApprovedBy`, `Event`, `EventPayload`, `TriggerEvent`, `Status`, `Started`, `Stopped`, `Created`, `Updated`.

Signed-off-by: appleboy <appleboy.tw@gmail.com>
@appleboy appleboy changed the title feat: implement auto-cancellation of concurrent jobs feat: implement auto-cancellation of concurrent jobs if the event is push Jul 21, 2023
@appleboy
Copy link
Member Author

@wolfogre Please help to review.

models/migrations/v1_21/v267.go Outdated Show resolved Hide resolved
models/migrations/v1_21/v267.go Outdated Show resolved Hide resolved
@GiteaBot GiteaBot added lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. and removed lgtm/need 1 This PR needs approval from one additional maintainer to be merged. labels Jul 24, 2023
@lunny lunny added the reviewed/wait-merge This pull request is part of the merge queue. It will be merged soon. label Jul 24, 2023
@lunny lunny enabled auto-merge (squash) July 24, 2023 05:52
@GiteaBot
Copy link
Contributor

@appleboy please fix the merge conflicts. 🍵

@GiteaBot GiteaBot removed the reviewed/wait-merge This pull request is part of the merge queue. It will be merged soon. label Jul 24, 2023
models/migrations/v1_21/v267.go Outdated Show resolved Hide resolved
services/actions/notifier_helper.go Show resolved Hide resolved
models/actions/run_list.go Outdated Show resolved Hide resolved
models/actions/run.go Outdated Show resolved Hide resolved
auto-merge was automatically disabled July 24, 2023 23:23

Head branch was pushed to by a user without write access

appleboy and others added 3 commits July 25, 2023 07:23
Co-authored-by: delvh <dev.lh@web.de>
Co-authored-by: delvh <dev.lh@web.de>
Co-authored-by: delvh <dev.lh@web.de>
wolfogre and others added 3 commits July 25, 2023 10:07
- Update migration version from `v267` to `v269`
- Rename migration file from `v267.go` to `v268.go`

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
@lunny lunny added the reviewed/wait-merge This pull request is part of the merge queue. It will be merged soon. label Jul 25, 2023
@wolfogre wolfogre changed the title feat: implement auto-cancellation of concurrent jobs if the event is push Implement auto-cancellation of concurrent jobs if the event is push Jul 25, 2023
@wolfogre wolfogre merged commit 44781f9 into go-gitea:main Jul 25, 2023
24 checks passed
@appleboy appleboy deleted the cancel branch July 25, 2023 03:16
@GiteaBot GiteaBot removed the reviewed/wait-merge This pull request is part of the merge queue. It will be merged soon. label Jul 25, 2023
zjjhot added a commit to zjjhot/gitea that referenced this pull request Jul 25, 2023
* giteaofficial/main: (23 commits)
  Avoid writing config file if not installed (go-gitea#26107)
  Implement auto-cancellation of concurrent jobs if the event is push (go-gitea#25716)
  [skip ci] Updated translations via Crowdin
  doc guide the user to create the appropriate level runner (go-gitea#26091)
  Fix handling of Debian files with trailing slash (go-gitea#26087)
  fix Missing 404 swagger response docs for /admin/users/{username} (go-gitea#26086)
  Allow the use of alternative net.Listener implementations by downstreams (go-gitea#25855)
  Add missing default value for some Bool cli flags (go-gitea#26082)
  Reduce unnecessary DB queries for Actions tasks (go-gitea#25199)
  Use stderr as fallback if the log file can't be opened (go-gitea#26074)
  Make organization redirect warning more clear (go-gitea#26077)
  Replace gogs/cron with go-co-op/gocron (go-gitea#25977)
  Remove `db.DefaultContext` in `routers/` and `cmd/` (go-gitea#26076)
  Categorize admin settings sidebar panel (go-gitea#26030)
  [skip ci] Updated translations via Crowdin
  Fix duplicated url prefix on issue context menu (go-gitea#26066)
  Add context parameter to some database functions (go-gitea#26055)
  Fix branch list auth (go-gitea#26041)
  Fix the truncate and alignment problem for some admin tables (go-gitea#26042)
  Update secrets.en-us.md (go-gitea#26057)
  ...
@go-gitea go-gitea locked as resolved and limited conversation to collaborators Oct 23, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. topic/gitea-actions related to the actions of Gitea type/feature Completely new functionality. Can only be merged if feature freeze is not active.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants