diff options
author | Jason Song <i@wolfogre.com> | 2023-08-21 22:07:52 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-08-21 14:07:52 +0000 |
commit | 8cf3b61fb9e43c773f3ca975874d0f0e69210d44 (patch) | |
tree | 01aa1fb5ca4d545fb4665ebe32532f64b60b92a4 /models/actions/run.go | |
parent | 42cbe6005adfe6e1440cdc9c15a05996f065bf83 (diff) | |
download | gitea-8cf3b61fb9e43c773f3ca975874d0f0e69210d44.tar.gz gitea-8cf3b61fb9e43c773f3ca975874d0f0e69210d44.zip |
Add optimistic lock to ActionRun table (#26563)
Should fix #26559.
How xorm works: https://xorm.io/docs/chapter-06/1.lock/
---------
Co-authored-by: Giteabot <teabot@gitea.io>
Diffstat (limited to 'models/actions/run.go')
-rw-r--r-- | models/actions/run.go | 15 |
1 files changed, 13 insertions, 2 deletions
diff --git a/models/actions/run.go b/models/actions/run.go index ab6e319b1c..18ed447e80 100644 --- a/models/actions/run.go +++ b/models/actions/run.go @@ -43,6 +43,7 @@ type ActionRun struct { EventPayload string `xorm:"LONGTEXT"` TriggerEvent string // the trigger event defined in the `on` configuration of the triggered workflow Status Status `xorm:"index"` + Version int `xorm:"version default 0"` // Status could be updated concomitantly, so an optimistic lock is needed Started timeutil.TimeStamp Stopped timeutil.TimeStamp Created timeutil.TimeStamp `xorm:"created"` @@ -332,12 +333,22 @@ func GetRunByIndex(ctx context.Context, repoID, index int64) (*ActionRun, error) return run, nil } +// UpdateRun updates a run. +// It requires the inputted run has Version set. +// It will return error if the version is not matched (it means the run has been changed after loaded). func UpdateRun(ctx context.Context, run *ActionRun, cols ...string) error { sess := db.GetEngine(ctx).ID(run.ID) if len(cols) > 0 { sess.Cols(cols...) } - _, err := sess.Update(run) + affected, err := sess.Update(run) + if err != nil { + return err + } + if affected == 0 { + return fmt.Errorf("run has changed") + // It's impossible that the run is not found, since Gitea never deletes runs. + } if run.Status != 0 || util.SliceContains(cols, "status") { if run.RepoID == 0 { @@ -358,7 +369,7 @@ func UpdateRun(ctx context.Context, run *ActionRun, cols ...string) error { } } - return err + return nil } type ActionRunIndex db.ResourceIndex |