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

feat!: protect against stale queue items #292

Merged
merged 5 commits into from
May 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions item.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,29 @@ import (
"github.com/go-vela/types/pipeline"
)

// ItemVersion allows the worker to detect items that were queued before an Vela server
// upgrade or downgrade, so it can handle such stale data gracefully.
// For example, the worker could fail a stale build or ask the server to recompile it.
// This is not a public API and is unrelated to the version key in pipeline yaml.
const ItemVersion uint64 = 1

// Item is the queue representation of an item to publish to the queue.
type Item struct {
Build *library.Build `json:"build"`
Pipeline *pipeline.Build `json:"pipeline"`
Repo *library.Repo `json:"repo"`
User *library.User `json:"user"`
// The 0-value is the implicit ItemVersion for queued Items that pre-date adding the field.
ItemVersion uint64 `json:"item_version"`
Comment on lines +24 to +25
Copy link
Member Author

Choose a reason for hiding this comment

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

NB: This is downgrade safe because if a json object has an unknown field, it is ignored.

}

// ToItem creates a queue item from a pipeline, build, repo and user.
func ToItem(p *pipeline.Build, b *library.Build, r *library.Repo, u *library.User) *Item {
return &Item{
Pipeline: p,
Build: b,
Repo: r,
User: u,
Pipeline: p,
Build: b,
Repo: r,
User: u,
ItemVersion: ItemVersion,
}
}
1 change: 1 addition & 0 deletions item_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ func TestTypes_ToItem(t *testing.T) {
Active: &booL,
Admin: &booL,
},
ItemVersion: ItemVersion,
}

// run test
Expand Down