Skip to content

Commit

Permalink
chore(steps): add context to step functions (#1044)
Browse files Browse the repository at this point in the history
* chore(steps): add context to step functions

* test(steps): add more tests to steps opts

---------

Co-authored-by: Easton Crupper <[email protected]>
Co-authored-by: david may <[email protected]>
  • Loading branch information
3 people authored Jan 26, 2024
1 parent aa78e03 commit 3e37509
Show file tree
Hide file tree
Showing 45 changed files with 215 additions and 91 deletions.
2 changes: 1 addition & 1 deletion api/admin/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func CleanResources(c *gin.Context) {
logrus.Infof("platform admin %s: cleaned %d services in database", u.GetName(), services)

// clean steps
steps, err := database.FromContext(c).CleanSteps(msg, before)
steps, err := database.FromContext(c).CleanSteps(ctx, msg, before)
if err != nil {
retErr := fmt.Errorf("%d builds cleaned. %d executables cleaned. %d services cleaned. unable to update steps: %w", builds, executables, services, err)

Expand Down
3 changes: 2 additions & 1 deletion api/admin/step.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func UpdateStep(c *gin.Context) {

// capture body from API request
input := new(library.Step)
ctx := c.Request.Context()

err := c.Bind(input)
if err != nil {
Expand All @@ -63,7 +64,7 @@ func UpdateStep(c *gin.Context) {
}

// send API call to update the step
s, err := database.FromContext(c).UpdateStep(input)
s, err := database.FromContext(c).UpdateStep(ctx, input)
if err != nil {
retErr := fmt.Errorf("unable to update step %d: %w", input.GetID(), err)

Expand Down
4 changes: 2 additions & 2 deletions api/build/cancel.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ func CancelBuild(c *gin.Context) {

for page > 0 {
// retrieve build steps (per page) from the database
stepsPart, _, err := database.FromContext(c).ListStepsForBuild(b, map[string]interface{}{}, page, perPage)
stepsPart, _, err := database.FromContext(c).ListStepsForBuild(ctx, b, map[string]interface{}{}, page, perPage)
if err != nil {
retErr := fmt.Errorf("unable to retrieve steps for build %s: %w", entry, err)
util.HandleError(c, http.StatusNotFound, retErr)
Expand All @@ -249,7 +249,7 @@ func CancelBuild(c *gin.Context) {
if step.GetStatus() == constants.StatusRunning || step.GetStatus() == constants.StatusPending {
step.SetStatus(constants.StatusCanceled)

_, err = database.FromContext(c).UpdateStep(step)
_, err = database.FromContext(c).UpdateStep(ctx, step)
if err != nil {
retErr := fmt.Errorf("unable to update step %s for build %s: %w", step.GetName(), entry, err)
util.HandleError(c, http.StatusNotFound, retErr)
Expand Down
2 changes: 1 addition & 1 deletion api/build/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func CleanBuild(ctx context.Context, database database.Interface, b *library.Bui
s.SetFinished(time.Now().UTC().Unix())

// send API call to update the step
_, err := database.UpdateStep(s)
_, err := database.UpdateStep(ctx, s)
if err != nil {
logrus.Errorf("unable to kill step %s for build %d: %v", s.GetName(), b.GetNumber(), err)
}
Expand Down
2 changes: 1 addition & 1 deletion api/build/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ func GetBuildGraph(c *gin.Context) {
if len(p.Stages) > 0 || len(p.Steps) > 0 {
for page > 0 {
// retrieve build steps (per page) from the database
stepsPart, _, err := database.FromContext(c).ListStepsForBuild(b, nil, page, perPage)
stepsPart, _, err := database.FromContext(c).ListStepsForBuild(ctx, b, nil, page, perPage)
if err != nil {
retErr := fmt.Errorf("unable to retrieve steps for build %s: %w", entry, err)

Expand Down
4 changes: 2 additions & 2 deletions api/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ func recordGauges(c *gin.Context) {
// step_image_count
if q.StepImageCount {
// send API call to capture the total number of step images
stepImageMap, err := database.FromContext(c).ListStepImageCount()
stepImageMap, err := database.FromContext(c).ListStepImageCount(ctx)
if err != nil {
logrus.Errorf("unable to get count of all step images: %v", err)
}
Expand All @@ -371,7 +371,7 @@ func recordGauges(c *gin.Context) {
// step_status_count
if q.StepStatusCount {
// send API call to capture the total number of step statuses
stepStatusMap, err := database.FromContext(c).ListStepStatusCount()
stepStatusMap, err := database.FromContext(c).ListStepStatusCount(ctx)
if err != nil {
logrus.Errorf("unable to get count of all step statuses: %v", err)
}
Expand Down
3 changes: 2 additions & 1 deletion api/step/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ func CreateStep(c *gin.Context) {
o := org.Retrieve(c)
r := repo.Retrieve(c)
u := user.Retrieve(c)
ctx := c.Request.Context()

entry := fmt.Sprintf("%s/%d", r.GetFullName(), b.GetNumber())

Expand Down Expand Up @@ -110,7 +111,7 @@ func CreateStep(c *gin.Context) {
}

// send API call to create the step
s, err := database.FromContext(c).CreateStep(input)
s, err := database.FromContext(c).CreateStep(ctx, input)
if err != nil {
retErr := fmt.Errorf("unable to create step for build %s: %w", entry, err)

Expand Down
3 changes: 2 additions & 1 deletion api/step/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ func DeleteStep(c *gin.Context) {
r := repo.Retrieve(c)
s := step.Retrieve(c)
u := user.Retrieve(c)
ctx := c.Request.Context()

entry := fmt.Sprintf("%s/%d/%d", r.GetFullName(), b.GetNumber(), s.GetNumber())

Expand All @@ -81,7 +82,7 @@ func DeleteStep(c *gin.Context) {
}).Infof("deleting step %s", entry)

// send API call to remove the step
err := database.FromContext(c).DeleteStep(s)
err := database.FromContext(c).DeleteStep(ctx, s)
if err != nil {
retErr := fmt.Errorf("unable to delete step %s: %w", entry, err)

Expand Down
3 changes: 2 additions & 1 deletion api/step/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ func ListSteps(c *gin.Context) {
o := org.Retrieve(c)
r := repo.Retrieve(c)
u := user.Retrieve(c)
ctx := c.Request.Context()

entry := fmt.Sprintf("%s/%d", r.GetFullName(), b.GetNumber())

Expand Down Expand Up @@ -122,7 +123,7 @@ func ListSteps(c *gin.Context) {
perPage = util.MaxInt(1, util.MinInt(100, perPage))

// send API call to capture the list of steps for the build
s, t, err := database.FromContext(c).ListStepsForBuild(b, map[string]interface{}{}, page, perPage)
s, t, err := database.FromContext(c).ListStepsForBuild(ctx, b, map[string]interface{}{}, page, perPage)
if err != nil {
retErr := fmt.Errorf("unable to list steps for build %s: %w", entry, err)

Expand Down
2 changes: 1 addition & 1 deletion api/step/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func planStep(ctx context.Context, database database.Interface, b *library.Build
s.SetCreated(time.Now().UTC().Unix())

// send API call to create the step
s, err := database.CreateStep(s)
s, err := database.CreateStep(ctx, s)
if err != nil {
return nil, fmt.Errorf("unable to create step %s: %w", s.GetName(), err)
}
Expand Down
3 changes: 2 additions & 1 deletion api/step/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ func UpdateStep(c *gin.Context) {
r := repo.Retrieve(c)
s := step.Retrieve(c)
u := user.Retrieve(c)
ctx := c.Request.Context()

entry := fmt.Sprintf("%s/%d/%d", r.GetFullName(), b.GetNumber(), s.GetNumber())

Expand Down Expand Up @@ -145,7 +146,7 @@ func UpdateStep(c *gin.Context) {
}

// send API call to update the step
s, err = database.FromContext(c).UpdateStep(s)
s, err = database.FromContext(c).UpdateStep(ctx, s)
if err != nil {
retErr := fmt.Errorf("unable to update step %s: %w", entry, err)

Expand Down
23 changes: 12 additions & 11 deletions database/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1637,18 +1637,19 @@ func testSteps(t *testing.T, db Interface, resources *Resources) {
// add the method name to the list of functions
methods[element.Method(i).Name] = false
}
ctx := context.TODO()

// create the steps
for _, step := range resources.Steps {
_, err := db.CreateStep(step)
_, err := db.CreateStep(ctx, step)
if err != nil {
t.Errorf("unable to create step %d: %v", step.GetID(), err)
}
}
methods["CreateStep"] = true

// count the steps
count, err := db.CountSteps()
count, err := db.CountSteps(ctx)
if err != nil {
t.Errorf("unable to count steps: %v", err)
}
Expand All @@ -1658,7 +1659,7 @@ func testSteps(t *testing.T, db Interface, resources *Resources) {
methods["CountSteps"] = true

// count the steps for a build
count, err = db.CountStepsForBuild(resources.Builds[0], nil)
count, err = db.CountStepsForBuild(ctx, resources.Builds[0], nil)
if err != nil {
t.Errorf("unable to count steps for build %d: %v", resources.Builds[0].GetID(), err)
}
Expand All @@ -1668,7 +1669,7 @@ func testSteps(t *testing.T, db Interface, resources *Resources) {
methods["CountStepsForBuild"] = true

// list the steps
list, err := db.ListSteps()
list, err := db.ListSteps(ctx)
if err != nil {
t.Errorf("unable to list steps: %v", err)
}
Expand All @@ -1678,7 +1679,7 @@ func testSteps(t *testing.T, db Interface, resources *Resources) {
methods["ListSteps"] = true

// list the steps for a build
list, count, err = db.ListStepsForBuild(resources.Builds[0], nil, 1, 10)
list, count, err = db.ListStepsForBuild(ctx, resources.Builds[0], nil, 1, 10)
if err != nil {
t.Errorf("unable to list steps for build %d: %v", resources.Builds[0].GetID(), err)
}
Expand All @@ -1694,7 +1695,7 @@ func testSteps(t *testing.T, db Interface, resources *Resources) {
"#init": 1,
"target/vela-git:v0.3.0": 1,
}
images, err := db.ListStepImageCount()
images, err := db.ListStepImageCount(ctx)
if err != nil {
t.Errorf("unable to list step image count: %v", err)
}
Expand All @@ -1710,7 +1711,7 @@ func testSteps(t *testing.T, db Interface, resources *Resources) {
"running": 1,
"success": 0,
}
statuses, err := db.ListStepStatusCount()
statuses, err := db.ListStepStatusCount(ctx)
if err != nil {
t.Errorf("unable to list step status count: %v", err)
}
Expand All @@ -1722,7 +1723,7 @@ func testSteps(t *testing.T, db Interface, resources *Resources) {
// lookup the steps by name
for _, step := range resources.Steps {
build := resources.Builds[step.GetBuildID()-1]
got, err := db.GetStepForBuild(build, step.GetNumber())
got, err := db.GetStepForBuild(ctx, build, step.GetNumber())
if err != nil {
t.Errorf("unable to get step %d for build %d: %v", step.GetID(), build.GetID(), err)
}
Expand All @@ -1733,7 +1734,7 @@ func testSteps(t *testing.T, db Interface, resources *Resources) {
methods["GetStepForBuild"] = true

// clean the steps
count, err = db.CleanSteps("integration testing", 1563474090)
count, err = db.CleanSteps(ctx, "integration testing", 1563474090)
if err != nil {
t.Errorf("unable to clean steps: %v", err)
}
Expand All @@ -1745,7 +1746,7 @@ func testSteps(t *testing.T, db Interface, resources *Resources) {
// update the steps
for _, step := range resources.Steps {
step.SetStatus("success")
got, err := db.UpdateStep(step)
got, err := db.UpdateStep(ctx, step)
if err != nil {
t.Errorf("unable to update step %d: %v", step.GetID(), err)
}
Expand All @@ -1759,7 +1760,7 @@ func testSteps(t *testing.T, db Interface, resources *Resources) {

// delete the steps
for _, step := range resources.Steps {
err = db.DeleteStep(step)
err = db.DeleteStep(ctx, step)
if err != nil {
t.Errorf("unable to delete step %d: %v", step.GetID(), err)
}
Expand Down
1 change: 1 addition & 0 deletions database/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ func (e *engine) NewResources(ctx context.Context) error {

// create the database agnostic engine for steps
e.StepInterface, err = step.New(
step.WithContext(e.ctx),
step.WithClient(e.client),
step.WithLogger(e.logger),
step.WithSkipCreation(e.config.SkipCreation),
Expand Down
3 changes: 2 additions & 1 deletion database/step/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package step

import (
"context"
"time"

"github.com/go-vela/types/constants"
Expand All @@ -12,7 +13,7 @@ import (
)

// CleanSteps updates steps to an error with a created timestamp prior to a defined moment.
func (e *engine) CleanSteps(msg string, before int64) (int64, error) {
func (e *engine) CleanSteps(ctx context.Context, msg string, before int64) (int64, error) {
logrus.Tracef("cleaning pending or running steps in the database created prior to %d", before)

s := new(library.Step)
Expand Down
13 changes: 8 additions & 5 deletions database/step/clean_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package step

import (
"context"
"reflect"
"testing"

Expand Down Expand Up @@ -52,6 +53,8 @@ func TestStep_Engine_CleanStep(t *testing.T) {
_stepFour.SetStatus("pending")

_postgres, _mock := testPostgres(t)

ctx := context.TODO()
defer func() { _sql, _ := _postgres.client.DB(); _sql.Close() }()

// ensure the mock expects the name query
Expand All @@ -62,22 +65,22 @@ func TestStep_Engine_CleanStep(t *testing.T) {
_sqlite := testSqlite(t)
defer func() { _sql, _ := _sqlite.client.DB(); _sql.Close() }()

_, err := _sqlite.CreateStep(_stepOne)
_, err := _sqlite.CreateStep(ctx, _stepOne)
if err != nil {
t.Errorf("unable to create test step for sqlite: %v", err)
}

_, err = _sqlite.CreateStep(_stepTwo)
_, err = _sqlite.CreateStep(ctx, _stepTwo)
if err != nil {
t.Errorf("unable to create test step for sqlite: %v", err)
}

_, err = _sqlite.CreateStep(_stepThree)
_, err = _sqlite.CreateStep(ctx, _stepThree)
if err != nil {
t.Errorf("unable to create test step for sqlite: %v", err)
}

_, err = _sqlite.CreateStep(_stepFour)
_, err = _sqlite.CreateStep(ctx, _stepFour)
if err != nil {
t.Errorf("unable to create test step for sqlite: %v", err)
}
Expand Down Expand Up @@ -106,7 +109,7 @@ func TestStep_Engine_CleanStep(t *testing.T) {
// run tests
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got, err := test.database.CleanSteps("msg", 3)
got, err := test.database.CleanSteps(ctx, "msg", 3)

if test.failure {
if err == nil {
Expand Down
3 changes: 2 additions & 1 deletion database/step/count.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
package step

import (
"context"
"github.com/go-vela/types/constants"
)

// CountSteps gets the count of all steps from the database.
func (e *engine) CountSteps() (int64, error) {
func (e *engine) CountSteps(ctx context.Context) (int64, error) {
e.logger.Tracef("getting count of all steps from the database")

// variable to store query results
Expand Down
3 changes: 2 additions & 1 deletion database/step/count_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
package step

import (
"context"
"github.com/go-vela/types/constants"
"github.com/go-vela/types/library"
"github.com/sirupsen/logrus"
)

// CountStepsForBuild gets the count of steps by build ID from the database.
func (e *engine) CountStepsForBuild(b *library.Build, filters map[string]interface{}) (int64, error) {
func (e *engine) CountStepsForBuild(ctx context.Context, b *library.Build, filters map[string]interface{}) (int64, error) {
e.logger.WithFields(logrus.Fields{
"build": b.GetNumber(),
}).Tracef("getting count of steps for build %d from the database", b.GetNumber())
Expand Down
Loading

0 comments on commit 3e37509

Please sign in to comment.