Skip to content

Commit

Permalink
chore: add context to build functions (#922)
Browse files Browse the repository at this point in the history
  • Loading branch information
plyr4 committed Aug 11, 2023
1 parent 516d4fe commit 439455c
Show file tree
Hide file tree
Showing 67 changed files with 322 additions and 190 deletions.
10 changes: 8 additions & 2 deletions api/admin/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,16 @@ import (
// AllBuildsQueue represents the API handler to
// captures all running and pending builds stored in the database.
func AllBuildsQueue(c *gin.Context) {
// capture middleware values
ctx := c.Request.Context()

logrus.Info("Admin: reading running and pending builds")

// default timestamp to 24 hours ago if user did not provide it as query parameter
after := c.DefaultQuery("after", strconv.FormatInt(time.Now().UTC().Add(-24*time.Hour).Unix(), 10))

// send API call to capture pending and running builds
b, err := database.FromContext(c).ListPendingAndRunningBuilds(after)
b, err := database.FromContext(c).ListPendingAndRunningBuilds(ctx, after)
if err != nil {
retErr := fmt.Errorf("unable to capture all running and pending builds: %w", err)

Expand Down Expand Up @@ -103,6 +106,9 @@ func AllBuildsQueue(c *gin.Context) {
func UpdateBuild(c *gin.Context) {
logrus.Info("Admin: updating build in database")

// capture middleware values
ctx := c.Request.Context()

// capture body from API request
input := new(library.Build)

Expand All @@ -116,7 +122,7 @@ func UpdateBuild(c *gin.Context) {
}

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

Expand Down
5 changes: 4 additions & 1 deletion api/admin/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ import (
// CleanResources represents the API handler to
// update any user stored in the database.
func CleanResources(c *gin.Context) {
// capture middleware values
u := user.Retrieve(c)
ctx := c.Request.Context()

logrus.Infof("platform admin %s: updating pending resources in database", u.GetName())

// default error message
Expand Down Expand Up @@ -96,7 +99,7 @@ func CleanResources(c *gin.Context) {
}

// send API call to clean builds
builds, err := database.FromContext(c).CleanBuilds(msg, before)
builds, err := database.FromContext(c).CleanBuilds(ctx, msg, before)
if err != nil {
retErr := fmt.Errorf("unable to update builds: %w", err)

Expand Down
4 changes: 3 additions & 1 deletion api/badge.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ func GetBadge(c *gin.Context) {
// capture middleware values
o := org.Retrieve(c)
r := repo.Retrieve(c)
ctx := c.Request.Context()

branch := util.QueryParameter(c, "branch", r.GetBranch())

// update engine logger with API metadata
Expand All @@ -57,7 +59,7 @@ func GetBadge(c *gin.Context) {
}).Infof("creating latest build badge for repo %s on branch %s", r.GetFullName(), branch)

// send API call to capture the last build for the repo and branch
b, err := database.FromContext(c).LastBuildForRepo(r, branch)
b, err := database.FromContext(c).LastBuildForRepo(ctx, r, branch)
if err != nil {
c.String(http.StatusOK, constants.BadgeUnknown)
return
Expand Down
3 changes: 2 additions & 1 deletion api/build/cancel.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ func CancelBuild(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 @@ -190,7 +191,7 @@ func CancelBuild(c *gin.Context) {
// update the status in the build table
b.SetStatus(constants.StatusCanceled)

b, err := database.FromContext(c).UpdateBuild(b)
b, err := database.FromContext(c).UpdateBuild(ctx, b)
if err != nil {
retErr := fmt.Errorf("unable to update status for build %s: %w", entry, err)
util.HandleError(c, http.StatusInternalServerError, retErr)
Expand Down
5 changes: 3 additions & 2 deletions api/build/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package build

import (
"context"
"fmt"
"time"

Expand All @@ -18,14 +19,14 @@ import (
// without execution. This will kill all resources,
// like steps and services, for the build in the
// configured backend.
func CleanBuild(database database.Interface, b *library.Build, services []*library.Service, steps []*library.Step, e error) {
func CleanBuild(ctx context.Context, database database.Interface, b *library.Build, services []*library.Service, steps []*library.Step, e error) {
// update fields in build object
b.SetError(fmt.Sprintf("unable to publish to queue: %s", e.Error()))
b.SetStatus(constants.StatusError)
b.SetFinished(time.Now().UTC().Unix())

// send API call to update the build
b, err := database.UpdateBuild(b)
b, err := database.UpdateBuild(ctx, b)
if err != nil {
logrus.Errorf("unable to kill build %d: %v", b.GetNumber(), err)
}
Expand Down
8 changes: 5 additions & 3 deletions api/build/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ func CreateBuild(c *gin.Context) {
o := org.Retrieve(c)
r := repo.Retrieve(c)
u := user.Retrieve(c)
ctx := c.Request.Context()

// update engine logger with API metadata
//
Expand Down Expand Up @@ -137,7 +138,7 @@ func CreateBuild(c *gin.Context) {
}

// send API call to capture the number of pending or running builds for the repo
builds, err := database.FromContext(c).CountBuildsForRepo(r, filters)
builds, err := database.FromContext(c).CountBuildsForRepo(ctx, r, filters)
if err != nil {
retErr := fmt.Errorf("unable to create new build: unable to get count of builds for repo %s", r.GetFullName())

Expand Down Expand Up @@ -321,7 +322,7 @@ func CreateBuild(c *gin.Context) {
input.SetPipelineID(pipeline.GetID())

// create the objects from the pipeline in the database
err = PlanBuild(database.FromContext(c), p, input, r)
err = PlanBuild(ctx, database.FromContext(c), p, input, r)
if err != nil {
util.HandleError(c, http.StatusInternalServerError, err)

Expand All @@ -339,7 +340,7 @@ func CreateBuild(c *gin.Context) {
}

// send API call to capture the created build
input, _ = database.FromContext(c).GetBuildForRepo(r, input.GetNumber())
input, _ = database.FromContext(c).GetBuildForRepo(ctx, r, input.GetNumber())

c.JSON(http.StatusCreated, input)

Expand All @@ -351,6 +352,7 @@ func CreateBuild(c *gin.Context) {

// publish the build to the queue
go PublishToQueue(
ctx,
queue.FromGinContext(c),
database.FromContext(c),
p,
Expand Down
3 changes: 2 additions & 1 deletion api/build/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func DeleteBuild(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 All @@ -79,7 +80,7 @@ func DeleteBuild(c *gin.Context) {
}).Infof("deleting build %s", entry)

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

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

// Capture user from middleware
u := user.Retrieve(c)
ctx := c.Request.Context()

// Parse build ID from path
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
Expand All @@ -79,7 +80,7 @@ func GetBuildByID(c *gin.Context) {
}).Infof("reading build %d", id)

// Get build from database
b, err = database.FromContext(c).GetBuild(id)
b, err = database.FromContext(c).GetBuild(ctx, id)
if err != nil {
retErr := fmt.Errorf("unable to get build: %w", err)

Expand Down
3 changes: 2 additions & 1 deletion api/build/list_org.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ func ListBuildsForOrg(c *gin.Context) {
// capture middleware values
o := org.Retrieve(c)
u := user.Retrieve(c)
ctx := c.Request.Context()

// update engine logger with API metadata
//
Expand Down Expand Up @@ -199,7 +200,7 @@ func ListBuildsForOrg(c *gin.Context) {
}

// send API call to capture the list of builds for the org (and event type if passed in)
b, t, err = database.FromContext(c).ListBuildsForOrg(o, filters, page, perPage)
b, t, err = database.FromContext(c).ListBuildsForOrg(ctx, o, filters, page, perPage)

if err != nil {
retErr := fmt.Errorf("unable to list builds for org %s: %w", o, err)
Expand Down
3 changes: 2 additions & 1 deletion api/build/list_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ func ListBuildsForRepo(c *gin.Context) {
o := org.Retrieve(c)
r := repo.Retrieve(c)
u := user.Retrieve(c)
ctx := c.Request.Context()

// update engine logger with API metadata
//
Expand Down Expand Up @@ -238,7 +239,7 @@ func ListBuildsForRepo(c *gin.Context) {
return
}

b, t, err = database.FromContext(c).ListBuildsForRepo(r, filters, before, after, page, perPage)
b, t, err = database.FromContext(c).ListBuildsForRepo(ctx, r, filters, before, after, page, perPage)
if err != nil {
retErr := fmt.Errorf("unable to list builds for repo %s: %w", r.GetFullName(), err)

Expand Down
11 changes: 6 additions & 5 deletions api/build/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package build

import (
"context"
"fmt"
"time"

Expand All @@ -20,13 +21,13 @@ import (
// and services, for the build in the configured backend.
// TODO:
// - return build and error.
func PlanBuild(database database.Interface, p *pipeline.Build, b *library.Build, r *library.Repo) error {
func PlanBuild(ctx context.Context, database database.Interface, p *pipeline.Build, b *library.Build, r *library.Repo) error {
// update fields in build object
b.SetCreated(time.Now().UTC().Unix())

// send API call to create the build
// TODO: return created build and error instead of just error
b, err := database.CreateBuild(b)
b, err := database.CreateBuild(ctx, b)
if err != nil {
// clean up the objects from the pipeline in the database
// TODO:
Expand All @@ -35,7 +36,7 @@ func PlanBuild(database database.Interface, p *pipeline.Build, b *library.Build,
// of UPDATE-ing the existing build - which results in
// a constraint error (repo_id, number)
// - do we want to update the build or just delete it?
CleanBuild(database, b, nil, nil, err)
CleanBuild(ctx, database, b, nil, nil, err)

return fmt.Errorf("unable to create new build for %s: %w", r.GetFullName(), err)
}
Expand All @@ -44,7 +45,7 @@ func PlanBuild(database database.Interface, p *pipeline.Build, b *library.Build,
services, err := service.PlanServices(database, p, b)
if err != nil {
// clean up the objects from the pipeline in the database
CleanBuild(database, b, services, nil, err)
CleanBuild(ctx, database, b, services, nil, err)

return err
}
Expand All @@ -53,7 +54,7 @@ func PlanBuild(database database.Interface, p *pipeline.Build, b *library.Build,
steps, err := step.PlanSteps(database, p, b)
if err != nil {
// clean up the objects from the pipeline in the database
CleanBuild(database, b, services, steps, err)
CleanBuild(ctx, database, b, services, steps, err)

return err
}
Expand Down
10 changes: 5 additions & 5 deletions api/build/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (

// PublishToQueue is a helper function that creates
// a build item and publishes it to the queue.
func PublishToQueue(queue queue.Service, db database.Interface, p *pipeline.Build, b *library.Build, r *library.Repo, u *library.User) {
func PublishToQueue(ctx context.Context, queue queue.Service, db database.Interface, p *pipeline.Build, b *library.Build, r *library.Repo, u *library.User) {
item := types.ToItem(p, b, r, u)

logrus.Infof("Converting queue item to json for build %d for %s", b.GetNumber(), r.GetFullName())
Expand All @@ -29,7 +29,7 @@ func PublishToQueue(queue queue.Service, db database.Interface, p *pipeline.Buil
logrus.Errorf("Failed to convert item to json for build %d for %s: %v", b.GetNumber(), r.GetFullName(), err)

// error out the build
CleanBuild(db, b, nil, nil, err)
CleanBuild(ctx, db, b, nil, nil, err)

return
}
Expand All @@ -41,7 +41,7 @@ func PublishToQueue(queue queue.Service, db database.Interface, p *pipeline.Buil
logrus.Errorf("unable to set route for build %d for %s: %v", b.GetNumber(), r.GetFullName(), err)

// error out the build
CleanBuild(db, b, nil, nil, err)
CleanBuild(ctx, db, b, nil, nil, err)

return
}
Expand All @@ -57,7 +57,7 @@ func PublishToQueue(queue queue.Service, db database.Interface, p *pipeline.Buil
logrus.Errorf("Failed to publish build %d for %s: %v", b.GetNumber(), r.GetFullName(), err)

// error out the build
CleanBuild(db, b, nil, nil, err)
CleanBuild(ctx, db, b, nil, nil, err)

return
}
Expand All @@ -67,7 +67,7 @@ func PublishToQueue(queue queue.Service, db database.Interface, p *pipeline.Buil
b.SetEnqueued(time.Now().UTC().Unix())

// update the build in the db to reflect the time it was enqueued
_, err = db.UpdateBuild(b)
_, err = db.UpdateBuild(ctx, b)
if err != nil {
logrus.Errorf("Failed to update build %d during publish to queue for %s: %v", b.GetNumber(), r.GetFullName(), err)
}
Expand Down
8 changes: 5 additions & 3 deletions api/build/restart.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ func RestartBuild(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 @@ -117,7 +118,7 @@ func RestartBuild(c *gin.Context) {
}

// send API call to capture the number of pending or running builds for the repo
builds, err := database.FromContext(c).CountBuildsForRepo(r, filters)
builds, err := database.FromContext(c).CountBuildsForRepo(ctx, r, filters)
if err != nil {
retErr := fmt.Errorf("unable to restart build: unable to get count of builds for repo %s", r.GetFullName())

Expand Down Expand Up @@ -312,7 +313,7 @@ func RestartBuild(c *gin.Context) {
b.SetPipelineID(pipeline.GetID())

// create the objects from the pipeline in the database
err = PlanBuild(database.FromContext(c), p, b, r)
err = PlanBuild(ctx, database.FromContext(c), p, b, r)
if err != nil {
util.HandleError(c, http.StatusInternalServerError, err)

Expand All @@ -329,7 +330,7 @@ func RestartBuild(c *gin.Context) {
}

// send API call to capture the restarted build
b, _ = database.FromContext(c).GetBuildForRepo(r, b.GetNumber())
b, _ = database.FromContext(c).GetBuildForRepo(ctx, r, b.GetNumber())

c.JSON(http.StatusCreated, b)

Expand All @@ -341,6 +342,7 @@ func RestartBuild(c *gin.Context) {

// publish the build to the queue
go PublishToQueue(
ctx,
queue.FromGinContext(c),
database.FromContext(c),
p,
Expand Down
3 changes: 2 additions & 1 deletion api/build/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ func UpdateBuild(c *gin.Context) {
b := build.Retrieve(c)
o := org.Retrieve(c)
r := repo.Retrieve(c)
ctx := c.Request.Context()

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

Expand Down Expand Up @@ -151,7 +152,7 @@ func UpdateBuild(c *gin.Context) {
}

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

Expand Down
Loading

0 comments on commit 439455c

Please sign in to comment.