Skip to content

Commit

Permalink
enhance: add context to Services (#942)
Browse files Browse the repository at this point in the history
  • Loading branch information
plyr4 authored Sep 1, 2023
1 parent 91e26c4 commit 987704f
Show file tree
Hide file tree
Showing 44 changed files with 201 additions and 90 deletions.
2 changes: 1 addition & 1 deletion api/admin/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func CleanResources(c *gin.Context) {
logrus.Infof("platform admin %s: cleaned %d builds in database", u.GetName(), builds)

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

Expand Down
5 changes: 4 additions & 1 deletion api/admin/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ import (
func UpdateService(c *gin.Context) {
logrus.Info("Admin: updating service in database")

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

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

Expand All @@ -67,7 +70,7 @@ func UpdateService(c *gin.Context) {
}

// send API call to update the service
s, err := database.FromContext(c).UpdateService(input)
s, err := database.FromContext(c).UpdateService(ctx, input)
if err != nil {
retErr := fmt.Errorf("unable to update service %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 @@ -247,7 +247,7 @@ func CancelBuild(c *gin.Context) {

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

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

// send API call to update the service
_, err := database.UpdateService(s)
_, err := database.UpdateService(ctx, s)
if err != nil {
logrus.Errorf("unable to kill service %s for build %d: %v", s.GetName(), b.GetNumber(), err)
}
Expand Down
2 changes: 1 addition & 1 deletion api/build/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func PlanBuild(ctx context.Context, database database.Interface, p *pipeline.Bui
}

// plan all services for the build
services, err := service.PlanServices(database, p, b)
services, err := service.PlanServices(ctx, database, p, b)
if err != nil {
// clean up the objects from the pipeline in the database
CleanBuild(ctx, database, b, services, nil, err)
Expand Down
4 changes: 2 additions & 2 deletions api/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ func recordGauges(c *gin.Context) {
// service_image_count
if q.ServiceImageCount {
// send API call to capture the total number of service images
serviceImageMap, err := database.FromContext(c).ListServiceImageCount()
serviceImageMap, err := database.FromContext(c).ListServiceImageCount(ctx)
if err != nil {
logrus.Errorf("unable to get count of all service images: %v", err)
}
Expand All @@ -399,7 +399,7 @@ func recordGauges(c *gin.Context) {
// service_status_count
if q.ServiceStatusCount {
// send API call to capture the total number of service statuses
serviceStatusMap, err := database.FromContext(c).ListServiceStatusCount()
serviceStatusMap, err := database.FromContext(c).ListServiceStatusCount(ctx)
if err != nil {
logrus.Errorf("unable to get count of all service statuses: %v", err)
}
Expand Down
3 changes: 2 additions & 1 deletion api/service/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ func CreateService(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 @@ -112,7 +113,7 @@ func CreateService(c *gin.Context) {
}

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

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

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

Expand All @@ -84,7 +85,7 @@ func DeleteService(c *gin.Context) {
}).Infof("deleting service %s", entry)

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

Expand Down
3 changes: 2 additions & 1 deletion api/service/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ func ListServices(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 @@ -124,7 +125,7 @@ func ListServices(c *gin.Context) {
perPage = util.MaxInt(1, util.MinInt(100, perPage))

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

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

import (
"context"
"fmt"
"time"

Expand All @@ -17,7 +18,7 @@ import (
// PlanServices is a helper function to plan all services
// in the build for execution. This creates the services
// for the build in the configured backend.
func PlanServices(database database.Interface, p *pipeline.Build, b *library.Build) ([]*library.Service, error) {
func PlanServices(ctx context.Context, database database.Interface, p *pipeline.Build, b *library.Build) ([]*library.Service, error) {
// variable to store planned services
services := []*library.Service{}

Expand All @@ -34,7 +35,7 @@ func PlanServices(database database.Interface, p *pipeline.Build, b *library.Bui
s.SetCreated(time.Now().UTC().Unix())

// send API call to create the service
s, err := database.CreateService(s)
s, err := database.CreateService(ctx, s)
if err != nil {
return services, fmt.Errorf("unable to create service %s: %w", s.GetName(), err)
}
Expand Down
3 changes: 2 additions & 1 deletion api/service/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ func UpdateService(c *gin.Context) {
r := repo.Retrieve(c)
s := service.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 @@ -133,7 +134,7 @@ func UpdateService(c *gin.Context) {
}

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

Expand Down
22 changes: 11 additions & 11 deletions database/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1326,15 +1326,15 @@ func testServices(t *testing.T, db Interface, resources *Resources) {

// create the services
for _, service := range resources.Services {
_, err := db.CreateService(service)
_, err := db.CreateService(context.TODO(), service)
if err != nil {
t.Errorf("unable to create service %d: %v", service.GetID(), err)
}
}
methods["CreateService"] = true

// count the services
count, err := db.CountServices()
count, err := db.CountServices(context.TODO())
if err != nil {
t.Errorf("unable to count services: %v", err)
}
Expand All @@ -1344,7 +1344,7 @@ func testServices(t *testing.T, db Interface, resources *Resources) {
methods["CountServices"] = true

// count the services for a build
count, err = db.CountServicesForBuild(resources.Builds[0], nil)
count, err = db.CountServicesForBuild(context.TODO(), resources.Builds[0], nil)
if err != nil {
t.Errorf("unable to count services for build %d: %v", resources.Builds[0].GetID(), err)
}
Expand All @@ -1354,7 +1354,7 @@ func testServices(t *testing.T, db Interface, resources *Resources) {
methods["CountServicesForBuild"] = true

// list the services
list, err := db.ListServices()
list, err := db.ListServices(context.TODO())
if err != nil {
t.Errorf("unable to list services: %v", err)
}
Expand All @@ -1364,7 +1364,7 @@ func testServices(t *testing.T, db Interface, resources *Resources) {
methods["ListServices"] = true

// list the services for a build
list, count, err = db.ListServicesForBuild(resources.Builds[0], nil, 1, 10)
list, count, err = db.ListServicesForBuild(context.TODO(), resources.Builds[0], nil, 1, 10)
if err != nil {
t.Errorf("unable to list services for build %d: %v", resources.Builds[0].GetID(), err)
}
Expand All @@ -1380,7 +1380,7 @@ func testServices(t *testing.T, db Interface, resources *Resources) {
"#init": 1,
"target/vela-git:v0.3.0": 1,
}
images, err := db.ListServiceImageCount()
images, err := db.ListServiceImageCount(context.TODO())
if err != nil {
t.Errorf("unable to list service image count: %v", err)
}
Expand All @@ -1396,7 +1396,7 @@ func testServices(t *testing.T, db Interface, resources *Resources) {
"running": 1,
"success": 0,
}
statuses, err := db.ListServiceStatusCount()
statuses, err := db.ListServiceStatusCount(context.TODO())
if err != nil {
t.Errorf("unable to list service status count: %v", err)
}
Expand All @@ -1408,7 +1408,7 @@ func testServices(t *testing.T, db Interface, resources *Resources) {
// lookup the services by name
for _, service := range resources.Services {
build := resources.Builds[service.GetBuildID()-1]
got, err := db.GetServiceForBuild(build, service.GetNumber())
got, err := db.GetServiceForBuild(context.TODO(), build, service.GetNumber())
if err != nil {
t.Errorf("unable to get service %d for build %d: %v", service.GetID(), build.GetID(), err)
}
Expand All @@ -1419,7 +1419,7 @@ func testServices(t *testing.T, db Interface, resources *Resources) {
methods["GetServiceForBuild"] = true

// clean the services
count, err = db.CleanServices("integration testing", 1563474090)
count, err = db.CleanServices(context.TODO(), "integration testing", 1563474090)
if err != nil {
t.Errorf("unable to clean services: %v", err)
}
Expand All @@ -1431,7 +1431,7 @@ func testServices(t *testing.T, db Interface, resources *Resources) {
// update the services
for _, service := range resources.Services {
service.SetStatus("success")
got, err := db.UpdateService(service)
got, err := db.UpdateService(context.TODO(), service)
if err != nil {
t.Errorf("unable to update service %d: %v", service.GetID(), err)
}
Expand All @@ -1445,7 +1445,7 @@ func testServices(t *testing.T, db Interface, resources *Resources) {

// delete the services
for _, service := range resources.Services {
err = db.DeleteService(service)
err = db.DeleteService(context.TODO(), service)
if err != nil {
t.Errorf("unable to delete service %d: %v", service.GetID(), err)
}
Expand Down
3 changes: 2 additions & 1 deletion database/service/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package service

import (
"context"
"time"

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

// CleanServices updates services to an error with a created timestamp prior to a defined moment.
func (e *engine) CleanServices(msg string, before int64) (int64, error) {
func (e *engine) CleanServices(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.Service)
Expand Down
11 changes: 6 additions & 5 deletions database/service/clean_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package service

import (
"context"
"reflect"
"testing"

Expand Down Expand Up @@ -64,22 +65,22 @@ func TestService_Engine_CleanService(t *testing.T) {
_sqlite := testSqlite(t)
defer func() { _sql, _ := _sqlite.client.DB(); _sql.Close() }()

_, err := _sqlite.CreateService(_serviceOne)
_, err := _sqlite.CreateService(context.TODO(), _serviceOne)
if err != nil {
t.Errorf("unable to create test service for sqlite: %v", err)
}

_, err = _sqlite.CreateService(_serviceTwo)
_, err = _sqlite.CreateService(context.TODO(), _serviceTwo)
if err != nil {
t.Errorf("unable to create test service for sqlite: %v", err)
}

_, err = _sqlite.CreateService(_serviceThree)
_, err = _sqlite.CreateService(context.TODO(), _serviceThree)
if err != nil {
t.Errorf("unable to create test service for sqlite: %v", err)
}

_, err = _sqlite.CreateService(_serviceFour)
_, err = _sqlite.CreateService(context.TODO(), _serviceFour)
if err != nil {
t.Errorf("unable to create test service for sqlite: %v", err)
}
Expand Down Expand Up @@ -108,7 +109,7 @@ func TestService_Engine_CleanService(t *testing.T) {
// run tests
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got, err := test.database.CleanServices("msg", 3)
got, err := test.database.CleanServices(context.TODO(), "msg", 3)

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

import (
"context"

"github.com/go-vela/types/constants"
)

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

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

import (
"context"

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

// CountServicesForBuild gets the count of services by build ID from the database.
func (e *engine) CountServicesForBuild(b *library.Build, filters map[string]interface{}) (int64, error) {
func (e *engine) CountServicesForBuild(ctx context.Context, b *library.Build, filters map[string]interface{}) (int64, error) {
e.logger.WithFields(logrus.Fields{
"build": b.GetNumber(),
}).Tracef("getting count of services for build %d from the database", b.GetNumber())
Expand Down
7 changes: 4 additions & 3 deletions database/service/count_build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package service

import (
"context"
"reflect"
"testing"

Expand Down Expand Up @@ -46,12 +47,12 @@ func TestService_Engine_CountServicesForBuild(t *testing.T) {
_sqlite := testSqlite(t)
defer func() { _sql, _ := _sqlite.client.DB(); _sql.Close() }()

_, err := _sqlite.CreateService(_serviceOne)
_, err := _sqlite.CreateService(context.TODO(), _serviceOne)
if err != nil {
t.Errorf("unable to create test service for sqlite: %v", err)
}

_, err = _sqlite.CreateService(_serviceTwo)
_, err = _sqlite.CreateService(context.TODO(), _serviceTwo)
if err != nil {
t.Errorf("unable to create test service for sqlite: %v", err)
}
Expand Down Expand Up @@ -82,7 +83,7 @@ func TestService_Engine_CountServicesForBuild(t *testing.T) {
// run tests
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got, err := test.database.CountServicesForBuild(_build, filters)
got, err := test.database.CountServicesForBuild(context.TODO(), _build, filters)

if test.failure {
if err == nil {
Expand Down
Loading

0 comments on commit 987704f

Please sign in to comment.