diff --git a/api/schedule/create.go b/api/schedule/create.go index f03fa0cea..3c5c4790a 100644 --- a/api/schedule/create.go +++ b/api/schedule/create.go @@ -77,6 +77,7 @@ func CreateSchedule(c *gin.Context) { // capture middleware values u := user.Retrieve(c) r := repo.Retrieve(c) + ctx := c.Request.Context() allowlist := c.Value("allowlistschedule").([]string) minimumFrequency := c.Value("scheduleminimumfrequency").(time.Duration) @@ -145,7 +146,7 @@ func CreateSchedule(c *gin.Context) { } // send API call to capture the schedule from the database - dbSchedule, err := database.FromContext(c).GetScheduleForRepo(r, input.GetName()) + dbSchedule, err := database.FromContext(c).GetScheduleForRepo(ctx, r, input.GetName()) if err == nil && dbSchedule.GetActive() { retErr := fmt.Errorf("unable to create schedule: %s is already active", input.GetName()) @@ -170,7 +171,7 @@ func CreateSchedule(c *gin.Context) { dbSchedule.SetActive(true) // send API call to update the schedule - err = database.FromContext(c).UpdateSchedule(dbSchedule, true) + err = database.FromContext(c).UpdateSchedule(ctx, dbSchedule, true) if err != nil { retErr := fmt.Errorf("unable to set schedule %s to active: %w", dbSchedule.GetName(), err) @@ -180,10 +181,10 @@ func CreateSchedule(c *gin.Context) { } // send API call to capture the updated schedule - s, _ = database.FromContext(c).GetScheduleForRepo(r, dbSchedule.GetName()) + s, _ = database.FromContext(c).GetScheduleForRepo(ctx, r, dbSchedule.GetName()) } else { // send API call to create the schedule - err = database.FromContext(c).CreateSchedule(s) + err = database.FromContext(c).CreateSchedule(ctx, s) if err != nil { retErr := fmt.Errorf("unable to create new schedule %s: %w", r.GetName(), err) @@ -193,7 +194,7 @@ func CreateSchedule(c *gin.Context) { } // send API call to capture the created schedule - s, _ = database.FromContext(c).GetScheduleForRepo(r, input.GetName()) + s, _ = database.FromContext(c).GetScheduleForRepo(ctx, r, input.GetName()) } c.JSON(http.StatusCreated, s) diff --git a/api/schedule/delete.go b/api/schedule/delete.go index a697954d8..9c6f1dda8 100644 --- a/api/schedule/delete.go +++ b/api/schedule/delete.go @@ -65,6 +65,7 @@ func DeleteSchedule(c *gin.Context) { r := repo.Retrieve(c) u := user.Retrieve(c) s := schedule.Retrieve(c) + ctx := c.Request.Context() // update engine logger with API metadata // @@ -75,7 +76,7 @@ func DeleteSchedule(c *gin.Context) { "user": u.GetName(), }).Infof("deleting schedule %s", s.GetName()) - err := database.FromContext(c).DeleteSchedule(s) + err := database.FromContext(c).DeleteSchedule(ctx, s) if err != nil { retErr := fmt.Errorf("unable to delete schedule %s: %w", s.GetName(), err) diff --git a/api/schedule/list.go b/api/schedule/list.go index cb64ada74..a3d6d6e14 100644 --- a/api/schedule/list.go +++ b/api/schedule/list.go @@ -76,6 +76,7 @@ import ( func ListSchedules(c *gin.Context) { // capture middleware values r := repo.Retrieve(c) + ctx := c.Request.Context() // update engine logger with API metadata // @@ -109,7 +110,7 @@ func ListSchedules(c *gin.Context) { perPage = util.MaxInt(1, util.MinInt(100, perPage)) // send API call to capture the list of schedules for the repo - s, t, err := database.FromContext(c).ListSchedulesForRepo(r, page, perPage) + s, t, err := database.FromContext(c).ListSchedulesForRepo(ctx, r, page, perPage) if err != nil { retErr := fmt.Errorf("unable to get schedules for repo %s: %w", r.GetFullName(), err) diff --git a/api/schedule/update.go b/api/schedule/update.go index 5221d222c..a58990f33 100644 --- a/api/schedule/update.go +++ b/api/schedule/update.go @@ -74,6 +74,7 @@ func UpdateSchedule(c *gin.Context) { // capture middleware values r := repo.Retrieve(c) s := schedule.Retrieve(c) + ctx := c.Request.Context() u := user.Retrieve(c) scheduleName := util.PathParameter(c, "schedule") minimumFrequency := c.Value("scheduleminimumfrequency").(time.Duration) @@ -128,7 +129,7 @@ func UpdateSchedule(c *gin.Context) { s.SetUpdatedBy(u.GetName()) // update the schedule within the database - err = database.FromContext(c).UpdateSchedule(s, true) + err = database.FromContext(c).UpdateSchedule(ctx, s, true) if err != nil { retErr := fmt.Errorf("unable to update scheduled %s: %w", scheduleName, err) @@ -138,7 +139,7 @@ func UpdateSchedule(c *gin.Context) { } // capture the updated scheduled - s, _ = database.FromContext(c).GetScheduleForRepo(r, scheduleName) + s, _ = database.FromContext(c).GetScheduleForRepo(ctx, r, scheduleName) c.JSON(http.StatusOK, s) } diff --git a/cmd/vela-server/schedule.go b/cmd/vela-server/schedule.go index e5b1a78c1..aa7bed207 100644 --- a/cmd/vela-server/schedule.go +++ b/cmd/vela-server/schedule.go @@ -5,6 +5,7 @@ package main import ( + "context" "fmt" "strings" "time" @@ -30,11 +31,11 @@ const ( scheduleWait = "waiting to trigger build for schedule" ) -func processSchedules(start time.Time, compiler compiler.Engine, database database.Interface, metadata *types.Metadata, queue queue.Service, scm scm.Service) error { +func processSchedules(ctx context.Context, start time.Time, compiler compiler.Engine, database database.Interface, metadata *types.Metadata, queue queue.Service, scm scm.Service) error { logrus.Infof("processing active schedules to create builds") // send API call to capture the list of active schedules - schedules, err := database.ListActiveSchedules() + schedules, err := database.ListActiveSchedules(ctx) if err != nil { return err } @@ -53,7 +54,7 @@ func processSchedules(start time.Time, compiler compiler.Engine, database databa // This is needed to ensure we are not dealing with a stale schedule since we fetch // all schedules once and iterate through that list which can take a significant // amount of time to get to the end of the list. - schedule, err := database.GetSchedule(s.GetID()) + schedule, err := database.GetSchedule(ctx, s.GetID()) if err != nil { logrus.WithError(err).Warnf("%s %s", scheduleErr, schedule.GetName()) @@ -115,7 +116,7 @@ func processSchedules(start time.Time, compiler compiler.Engine, database databa schedule.SetScheduledAt(time.Now().UTC().Unix()) // send API call to update schedule for ensuring scheduled_at field is set - err = database.UpdateSchedule(schedule, false) + err = database.UpdateSchedule(ctx, schedule, false) if err != nil { logrus.WithError(err).Warnf("%s %s", scheduleErr, schedule.GetName()) @@ -123,7 +124,7 @@ func processSchedules(start time.Time, compiler compiler.Engine, database databa } // process the schedule and trigger a new build - err = processSchedule(schedule, compiler, database, metadata, queue, scm) + err = processSchedule(ctx, schedule, compiler, database, metadata, queue, scm) if err != nil { logrus.WithError(err).Warnf("%s %s", scheduleErr, schedule.GetName()) @@ -135,7 +136,7 @@ func processSchedules(start time.Time, compiler compiler.Engine, database databa } //nolint:funlen // ignore function length and number of statements -func processSchedule(s *library.Schedule, compiler compiler.Engine, database database.Interface, metadata *types.Metadata, queue queue.Service, scm scm.Service) error { +func processSchedule(ctx context.Context, s *library.Schedule, compiler compiler.Engine, database database.Interface, metadata *types.Metadata, queue queue.Service, scm scm.Service) error { // send API call to capture the repo for the schedule r, err := database.GetRepo(s.GetRepoID()) if err != nil { diff --git a/cmd/vela-server/server.go b/cmd/vela-server/server.go index 689ba03c2..d18a74278 100644 --- a/cmd/vela-server/server.go +++ b/cmd/vela-server/server.go @@ -199,7 +199,7 @@ func server(c *cli.Context) error { // sleep for a duration of time before processing schedules time.Sleep(jitter) - err = processSchedules(start, compiler, database, metadata, queue, scm) + err = processSchedules(ctx, start, compiler, database, metadata, queue, scm) if err != nil { logrus.WithError(err).Warn("unable to process schedules") } else { diff --git a/database/database.go b/database/database.go index c612ba3aa..d0317e3ff 100644 --- a/database/database.go +++ b/database/database.go @@ -5,6 +5,7 @@ package database import ( + "context" "fmt" "time" @@ -54,6 +55,8 @@ type ( client *gorm.DB // engine configuration settings used in database functions config *config + // engine context used in database functions + ctx context.Context // sirupsen/logrus logger used in database functions logger *logrus.Entry @@ -85,6 +88,7 @@ func New(opts ...EngineOpt) (Interface, error) { e.client = new(gorm.DB) e.config = new(config) e.logger = new(logrus.Entry) + e.ctx = context.TODO() // apply all provided configuration options for _, opt := range opts { @@ -143,7 +147,7 @@ func New(opts ...EngineOpt) (Interface, error) { } // create database agnostic engines for resources - err = e.NewResources() + err = e.NewResources(e.ctx) if err != nil { return nil, err } diff --git a/database/integration_test.go b/database/integration_test.go index 2cebb815a..9741c764f 100644 --- a/database/integration_test.go +++ b/database/integration_test.go @@ -5,6 +5,7 @@ package database import ( + "context" "os" "reflect" "strings" @@ -923,9 +924,11 @@ func testSchedules(t *testing.T, db Interface, resources *Resources) { methods[element.Method(i).Name] = false } + ctx := context.TODO() + // create the schedules for _, schedule := range resources.Schedules { - err := db.CreateSchedule(schedule) + err := db.CreateSchedule(ctx, schedule) if err != nil { t.Errorf("unable to create schedule %d: %v", schedule.GetID(), err) } @@ -933,7 +936,7 @@ func testSchedules(t *testing.T, db Interface, resources *Resources) { methods["CreateSchedule"] = true // count the schedules - count, err := db.CountSchedules() + count, err := db.CountSchedules(ctx) if err != nil { t.Errorf("unable to count schedules: %v", err) } @@ -943,7 +946,7 @@ func testSchedules(t *testing.T, db Interface, resources *Resources) { methods["CountSchedules"] = true // count the schedules for a repo - count, err = db.CountSchedulesForRepo(resources.Repos[0]) + count, err = db.CountSchedulesForRepo(ctx, resources.Repos[0]) if err != nil { t.Errorf("unable to count schedules for repo %d: %v", resources.Repos[0].GetID(), err) } @@ -953,7 +956,7 @@ func testSchedules(t *testing.T, db Interface, resources *Resources) { methods["CountSchedulesForRepo"] = true // list the schedules - list, err := db.ListSchedules() + list, err := db.ListSchedules(ctx) if err != nil { t.Errorf("unable to list schedules: %v", err) } @@ -963,7 +966,7 @@ func testSchedules(t *testing.T, db Interface, resources *Resources) { methods["ListSchedules"] = true // list the active schedules - list, err = db.ListActiveSchedules() + list, err = db.ListActiveSchedules(ctx) if err != nil { t.Errorf("unable to list schedules: %v", err) } @@ -973,7 +976,7 @@ func testSchedules(t *testing.T, db Interface, resources *Resources) { methods["ListActiveSchedules"] = true // list the schedules for a repo - list, count, err = db.ListSchedulesForRepo(resources.Repos[0], 1, 10) + list, count, err = db.ListSchedulesForRepo(ctx, resources.Repos[0], 1, 10) if err != nil { t.Errorf("unable to count schedules for repo %d: %v", resources.Repos[0].GetID(), err) } @@ -988,7 +991,7 @@ func testSchedules(t *testing.T, db Interface, resources *Resources) { // lookup the schedules by name for _, schedule := range resources.Schedules { repo := resources.Repos[schedule.GetRepoID()-1] - got, err := db.GetScheduleForRepo(repo, schedule.GetName()) + got, err := db.GetScheduleForRepo(ctx, repo, schedule.GetName()) if err != nil { t.Errorf("unable to get schedule %d for repo %d: %v", schedule.GetID(), repo.GetID(), err) } @@ -1001,13 +1004,13 @@ func testSchedules(t *testing.T, db Interface, resources *Resources) { // update the schedules for _, schedule := range resources.Schedules { schedule.SetUpdatedAt(time.Now().UTC().Unix()) - err = db.UpdateSchedule(schedule, true) + err = db.UpdateSchedule(ctx, schedule, true) if err != nil { t.Errorf("unable to update schedule %d: %v", schedule.GetID(), err) } // lookup the schedule by ID - got, err := db.GetSchedule(schedule.GetID()) + got, err := db.GetSchedule(ctx, schedule.GetID()) if err != nil { t.Errorf("unable to get schedule %d by ID: %v", schedule.GetID(), err) } @@ -1020,7 +1023,7 @@ func testSchedules(t *testing.T, db Interface, resources *Resources) { // delete the schedules for _, schedule := range resources.Schedules { - err = db.DeleteSchedule(schedule) + err = db.DeleteSchedule(ctx, schedule) if err != nil { t.Errorf("unable to delete schedule %d: %v", schedule.GetID(), err) } diff --git a/database/opts.go b/database/opts.go index 3714989c5..42ab3d033 100644 --- a/database/opts.go +++ b/database/opts.go @@ -4,7 +4,10 @@ package database -import "time" +import ( + "context" + "time" +) // EngineOpt represents a configuration option to initialize the database engine. type EngineOpt func(*engine) error @@ -88,3 +91,12 @@ func WithSkipCreation(skipCreation bool) EngineOpt { return nil } } + +// WithContext sets the context in the database engine. +func WithContext(ctx context.Context) EngineOpt { + return func(e *engine) error { + e.ctx = ctx + + return nil + } +} diff --git a/database/resource.go b/database/resource.go index 1ff9c929c..f9643b229 100644 --- a/database/resource.go +++ b/database/resource.go @@ -5,6 +5,7 @@ package database import ( + "context" "github.com/go-vela/server/database/build" "github.com/go-vela/server/database/hook" "github.com/go-vela/server/database/log" @@ -19,7 +20,7 @@ import ( ) // NewResources creates and returns the database agnostic engines for resources. -func (e *engine) NewResources() error { +func (e *engine) NewResources(ctx context.Context) error { var err error // create the database agnostic engine for builds @@ -77,6 +78,7 @@ func (e *engine) NewResources() error { // create the database agnostic engine for schedules e.ScheduleInterface, err = schedule.New( + schedule.WithContext(e.ctx), schedule.WithClient(e.client), schedule.WithLogger(e.logger), schedule.WithSkipCreation(e.config.SkipCreation), diff --git a/database/resource_test.go b/database/resource_test.go index 36a4694e6..233dbb800 100644 --- a/database/resource_test.go +++ b/database/resource_test.go @@ -5,6 +5,7 @@ package database import ( + "context" "testing" "github.com/DATA-DOG/go-sqlmock" @@ -94,7 +95,7 @@ func TestDatabase_Engine_NewResources(t *testing.T) { // run tests for _, test := range tests { t.Run(test.name, func(t *testing.T) { - err := test.database.NewResources() + err := test.database.NewResources(context.TODO()) if test.failure { if err == nil { diff --git a/database/schedule/count.go b/database/schedule/count.go index c7ce5f3aa..7a0d8c1ee 100644 --- a/database/schedule/count.go +++ b/database/schedule/count.go @@ -5,11 +5,12 @@ package schedule import ( + "context" "github.com/go-vela/types/constants" ) // CountSchedules gets the count of all schedules from the database. -func (e *engine) CountSchedules() (int64, error) { +func (e *engine) CountSchedules(ctx context.Context) (int64, error) { e.logger.Tracef("getting count of all schedules from the database") // variable to store query results diff --git a/database/schedule/count_active.go b/database/schedule/count_active.go index f3240f2f8..d65315186 100644 --- a/database/schedule/count_active.go +++ b/database/schedule/count_active.go @@ -5,11 +5,12 @@ package schedule import ( + "context" "github.com/go-vela/types/constants" ) // CountActiveSchedules gets the count of all active schedules from the database. -func (e *engine) CountActiveSchedules() (int64, error) { +func (e *engine) CountActiveSchedules(ctx context.Context) (int64, error) { e.logger.Tracef("getting count of all active schedules from the database") // variable to store query results diff --git a/database/schedule/count_active_test.go b/database/schedule/count_active_test.go index aa417efaa..866e11425 100644 --- a/database/schedule/count_active_test.go +++ b/database/schedule/count_active_test.go @@ -5,6 +5,7 @@ package schedule import ( + "context" "reflect" "testing" @@ -46,12 +47,12 @@ func TestSchedule_Engine_CountActiveSchedules(t *testing.T) { _sqlite := testSqlite(t) defer func() { _sql, _ := _sqlite.client.DB(); _sql.Close() }() - err := _sqlite.CreateSchedule(_scheduleOne) + err := _sqlite.CreateSchedule(context.TODO(), _scheduleOne) if err != nil { t.Errorf("unable to create test schedule for sqlite: %v", err) } - err = _sqlite.CreateSchedule(_scheduleTwo) + err = _sqlite.CreateSchedule(context.TODO(), _scheduleTwo) if err != nil { t.Errorf("unable to create test schedule for sqlite: %v", err) } @@ -80,7 +81,7 @@ func TestSchedule_Engine_CountActiveSchedules(t *testing.T) { // run tests for _, test := range tests { t.Run(test.name, func(t *testing.T) { - got, err := test.database.CountActiveSchedules() + got, err := test.database.CountActiveSchedules(context.TODO()) if test.failure { if err == nil { diff --git a/database/schedule/count_repo.go b/database/schedule/count_repo.go index 1ec5177cb..da806151c 100644 --- a/database/schedule/count_repo.go +++ b/database/schedule/count_repo.go @@ -5,13 +5,14 @@ package schedule import ( + "context" "github.com/go-vela/types/constants" "github.com/go-vela/types/library" "github.com/sirupsen/logrus" ) // CountSchedulesForRepo gets the count of schedules by repo ID from the database. -func (e *engine) CountSchedulesForRepo(r *library.Repo) (int64, error) { +func (e *engine) CountSchedulesForRepo(ctx context.Context, r *library.Repo) (int64, error) { e.logger.WithFields(logrus.Fields{ "org": r.GetOrg(), "repo": r.GetName(), diff --git a/database/schedule/count_repo_test.go b/database/schedule/count_repo_test.go index 792c21b1d..7bfbbe9bd 100644 --- a/database/schedule/count_repo_test.go +++ b/database/schedule/count_repo_test.go @@ -5,6 +5,7 @@ package schedule import ( + "context" "reflect" "testing" @@ -50,12 +51,12 @@ func TestSchedule_Engine_CountSchedulesForRepo(t *testing.T) { _sqlite := testSqlite(t) defer func() { _sql, _ := _sqlite.client.DB(); _sql.Close() }() - err := _sqlite.CreateSchedule(_scheduleOne) + err := _sqlite.CreateSchedule(context.TODO(), _scheduleOne) if err != nil { t.Errorf("unable to create test schedule for sqlite: %v", err) } - err = _sqlite.CreateSchedule(_scheduleTwo) + err = _sqlite.CreateSchedule(context.TODO(), _scheduleTwo) if err != nil { t.Errorf("unable to create test schedule for sqlite: %v", err) } @@ -84,7 +85,7 @@ func TestSchedule_Engine_CountSchedulesForRepo(t *testing.T) { // run tests for _, test := range tests { t.Run(test.name, func(t *testing.T) { - got, err := test.database.CountSchedulesForRepo(_repo) + got, err := test.database.CountSchedulesForRepo(context.TODO(), _repo) if test.failure { if err == nil { diff --git a/database/schedule/count_test.go b/database/schedule/count_test.go index 01cfbbae4..d8523b784 100644 --- a/database/schedule/count_test.go +++ b/database/schedule/count_test.go @@ -5,6 +5,7 @@ package schedule import ( + "context" "reflect" "testing" @@ -44,12 +45,12 @@ func TestSchedule_Engine_CountSchedules(t *testing.T) { _sqlite := testSqlite(t) defer func() { _sql, _ := _sqlite.client.DB(); _sql.Close() }() - err := _sqlite.CreateSchedule(_scheduleOne) + err := _sqlite.CreateSchedule(context.TODO(), _scheduleOne) if err != nil { t.Errorf("unable to create test schedule for sqlite: %v", err) } - err = _sqlite.CreateSchedule(_scheduleTwo) + err = _sqlite.CreateSchedule(context.TODO(), _scheduleTwo) if err != nil { t.Errorf("unable to create test schedule for sqlite: %v", err) } @@ -78,7 +79,7 @@ func TestSchedule_Engine_CountSchedules(t *testing.T) { // run tests for _, test := range tests { t.Run(test.name, func(t *testing.T) { - got, err := test.database.CountSchedules() + got, err := test.database.CountSchedules(context.TODO()) if test.failure { if err == nil { diff --git a/database/schedule/create.go b/database/schedule/create.go index a6e532e8c..f04b95508 100644 --- a/database/schedule/create.go +++ b/database/schedule/create.go @@ -6,6 +6,7 @@ package schedule import ( + "context" "github.com/go-vela/types/constants" "github.com/go-vela/types/database" "github.com/go-vela/types/library" @@ -13,7 +14,7 @@ import ( ) // CreateSchedule creates a new schedule in the database. -func (e *engine) CreateSchedule(s *library.Schedule) error { +func (e *engine) CreateSchedule(ctx context.Context, s *library.Schedule) error { e.logger.WithFields(logrus.Fields{ "schedule": s.GetName(), }).Tracef("creating schedule %s in the database", s.GetName()) diff --git a/database/schedule/create_test.go b/database/schedule/create_test.go index 015654151..426115915 100644 --- a/database/schedule/create_test.go +++ b/database/schedule/create_test.go @@ -5,6 +5,7 @@ package schedule import ( + "context" "testing" "github.com/DATA-DOG/go-sqlmock" @@ -58,7 +59,7 @@ VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10) RETURNING "id"`). // run tests for _, test := range tests { t.Run(test.name, func(t *testing.T) { - err := test.database.CreateSchedule(_schedule) + err := test.database.CreateSchedule(context.TODO(), _schedule) if test.failure { if err == nil { diff --git a/database/schedule/delete.go b/database/schedule/delete.go index 42ee4988d..765b31120 100644 --- a/database/schedule/delete.go +++ b/database/schedule/delete.go @@ -5,6 +5,7 @@ package schedule import ( + "context" "github.com/go-vela/types/constants" "github.com/go-vela/types/database" "github.com/go-vela/types/library" @@ -12,7 +13,7 @@ import ( ) // DeleteSchedule deletes an existing schedule from the database. -func (e *engine) DeleteSchedule(s *library.Schedule) error { +func (e *engine) DeleteSchedule(ctx context.Context, s *library.Schedule) error { e.logger.WithFields(logrus.Fields{ "schedule": s.GetName(), }).Tracef("deleting schedule %s in the database", s.GetName()) diff --git a/database/schedule/delete_test.go b/database/schedule/delete_test.go index 3d1a3cd48..0e958de52 100644 --- a/database/schedule/delete_test.go +++ b/database/schedule/delete_test.go @@ -5,6 +5,7 @@ package schedule import ( + "context" "testing" "github.com/DATA-DOG/go-sqlmock" @@ -32,7 +33,7 @@ func TestSchedule_Engine_DeleteSchedule(t *testing.T) { _sqlite := testSqlite(t) defer func() { _sql, _ := _sqlite.client.DB(); _sql.Close() }() - err := _sqlite.CreateSchedule(_schedule) + err := _sqlite.CreateSchedule(context.TODO(), _schedule) if err != nil { t.Errorf("unable to create test schedule for sqlite: %v", err) } @@ -58,7 +59,7 @@ func TestSchedule_Engine_DeleteSchedule(t *testing.T) { // run tests for _, test := range tests { t.Run(test.name, func(t *testing.T) { - err = test.database.DeleteSchedule(_schedule) + err = test.database.DeleteSchedule(context.TODO(), _schedule) if test.failure { if err == nil { diff --git a/database/schedule/get.go b/database/schedule/get.go index 73fcc162e..e4540f0f0 100644 --- a/database/schedule/get.go +++ b/database/schedule/get.go @@ -5,13 +5,14 @@ package schedule import ( + "context" "github.com/go-vela/types/constants" "github.com/go-vela/types/database" "github.com/go-vela/types/library" ) // GetSchedule gets a schedule by ID from the database. -func (e *engine) GetSchedule(id int64) (*library.Schedule, error) { +func (e *engine) GetSchedule(ctx context.Context, id int64) (*library.Schedule, error) { e.logger.Tracef("getting schedule %d from the database", id) // variable to store query results diff --git a/database/schedule/get_repo.go b/database/schedule/get_repo.go index ff5ab40e5..d0ddc4271 100644 --- a/database/schedule/get_repo.go +++ b/database/schedule/get_repo.go @@ -5,6 +5,7 @@ package schedule import ( + "context" "github.com/go-vela/types/constants" "github.com/go-vela/types/database" "github.com/go-vela/types/library" @@ -12,7 +13,7 @@ import ( ) // GetScheduleForRepo gets a schedule by repo ID and name from the database. -func (e *engine) GetScheduleForRepo(r *library.Repo, name string) (*library.Schedule, error) { +func (e *engine) GetScheduleForRepo(ctx context.Context, r *library.Repo, name string) (*library.Schedule, error) { e.logger.WithFields(logrus.Fields{ "org": r.GetOrg(), "repo": r.GetName(), diff --git a/database/schedule/get_repo_test.go b/database/schedule/get_repo_test.go index fb29bea37..56b010e97 100644 --- a/database/schedule/get_repo_test.go +++ b/database/schedule/get_repo_test.go @@ -5,6 +5,7 @@ package schedule import ( + "context" "reflect" "testing" @@ -43,7 +44,7 @@ func TestSchedule_Engine_GetScheduleForRepo(t *testing.T) { _sqlite := testSqlite(t) defer func() { _sql, _ := _sqlite.client.DB(); _sql.Close() }() - err := _sqlite.CreateSchedule(_schedule) + err := _sqlite.CreateSchedule(context.TODO(), _schedule) if err != nil { t.Errorf("unable to create test schedule for sqlite: %v", err) } @@ -72,7 +73,7 @@ func TestSchedule_Engine_GetScheduleForRepo(t *testing.T) { // run tests for _, test := range tests { t.Run(test.name, func(t *testing.T) { - got, err := test.database.GetScheduleForRepo(_repo, "nightly") + got, err := test.database.GetScheduleForRepo(context.TODO(), _repo, "nightly") if test.failure { if err == nil { diff --git a/database/schedule/get_test.go b/database/schedule/get_test.go index acea35b2a..6fc0ed11e 100644 --- a/database/schedule/get_test.go +++ b/database/schedule/get_test.go @@ -5,6 +5,7 @@ package schedule import ( + "context" "reflect" "testing" @@ -37,7 +38,7 @@ func TestSchedule_Engine_GetSchedule(t *testing.T) { _sqlite := testSqlite(t) defer func() { _sql, _ := _sqlite.client.DB(); _sql.Close() }() - err := _sqlite.CreateSchedule(_schedule) + err := _sqlite.CreateSchedule(context.TODO(), _schedule) if err != nil { t.Errorf("unable to create test schedule for sqlite: %v", err) } @@ -66,7 +67,7 @@ func TestSchedule_Engine_GetSchedule(t *testing.T) { // run tests for _, test := range tests { t.Run(test.name, func(t *testing.T) { - got, err := test.database.GetSchedule(1) + got, err := test.database.GetSchedule(context.TODO(), 1) if test.failure { if err == nil { diff --git a/database/schedule/index.go b/database/schedule/index.go index f14082cee..6b88199fa 100644 --- a/database/schedule/index.go +++ b/database/schedule/index.go @@ -4,6 +4,8 @@ package schedule +import "context" + const ( // CreateRepoIDIndex represents a query to create an // index on the schedules table for the repo_id column. @@ -16,7 +18,7 @@ ON schedules (repo_id); ) // CreateScheduleIndexes creates the indexes for the schedules table in the database. -func (e *engine) CreateScheduleIndexes() error { +func (e *engine) CreateScheduleIndexes(ctx context.Context) error { e.logger.Tracef("creating indexes for schedules table in the database") // create the repo_id column index for the schedules table diff --git a/database/schedule/index_test.go b/database/schedule/index_test.go index b8d334366..145fd5ac7 100644 --- a/database/schedule/index_test.go +++ b/database/schedule/index_test.go @@ -5,6 +5,7 @@ package schedule import ( + "context" "testing" "github.com/DATA-DOG/go-sqlmock" @@ -41,7 +42,7 @@ func TestSchedule_Engine_CreateScheduleIndexes(t *testing.T) { // run tests for _, test := range tests { t.Run(test.name, func(t *testing.T) { - err := test.database.CreateScheduleIndexes() + err := test.database.CreateScheduleIndexes(context.TODO()) if test.failure { if err == nil { diff --git a/database/schedule/interface.go b/database/schedule/interface.go index afa163063..6f7309d16 100644 --- a/database/schedule/interface.go +++ b/database/schedule/interface.go @@ -5,6 +5,7 @@ package schedule import ( + "context" "github.com/go-vela/types/library" ) @@ -18,32 +19,32 @@ type ScheduleInterface interface { // https://en.wikipedia.org/wiki/Data_definition_language // CreateScheduleIndexes defines a function that creates the indexes for the schedules table. - CreateScheduleIndexes() error + CreateScheduleIndexes(context.Context) error // CreateScheduleTable defines a function that creates the schedules table. - CreateScheduleTable(string) error + CreateScheduleTable(context.Context, string) error // Schedule Data Manipulation Language Functions // // https://en.wikipedia.org/wiki/Data_manipulation_language // CountSchedules defines a function that gets the count of all schedules. - CountSchedules() (int64, error) + CountSchedules(context.Context) (int64, error) // CountSchedulesForRepo defines a function that gets the count of schedules by repo ID. - CountSchedulesForRepo(*library.Repo) (int64, error) + CountSchedulesForRepo(context.Context, *library.Repo) (int64, error) // CreateSchedule defines a function that creates a new schedule. - CreateSchedule(*library.Schedule) error + CreateSchedule(context.Context, *library.Schedule) error // DeleteSchedule defines a function that deletes an existing schedule. - DeleteSchedule(*library.Schedule) error + DeleteSchedule(context.Context, *library.Schedule) error // GetSchedule defines a function that gets a schedule by ID. - GetSchedule(int64) (*library.Schedule, error) + GetSchedule(context.Context, int64) (*library.Schedule, error) // GetScheduleForRepo defines a function that gets a schedule by repo ID and name. - GetScheduleForRepo(*library.Repo, string) (*library.Schedule, error) + GetScheduleForRepo(context.Context, *library.Repo, string) (*library.Schedule, error) // ListActiveSchedules defines a function that gets a list of all active schedules. - ListActiveSchedules() ([]*library.Schedule, error) + ListActiveSchedules(context.Context) ([]*library.Schedule, error) // ListSchedules defines a function that gets a list of all schedules. - ListSchedules() ([]*library.Schedule, error) + ListSchedules(context.Context) ([]*library.Schedule, error) // ListSchedulesForRepo defines a function that gets a list of schedules by repo ID. - ListSchedulesForRepo(*library.Repo, int, int) ([]*library.Schedule, int64, error) + ListSchedulesForRepo(context.Context, *library.Repo, int, int) ([]*library.Schedule, int64, error) // UpdateSchedule defines a function that updates an existing schedule. - UpdateSchedule(*library.Schedule, bool) error + UpdateSchedule(context.Context, *library.Schedule, bool) error } diff --git a/database/schedule/list.go b/database/schedule/list.go index f4c5aa5e7..fbba87ca2 100644 --- a/database/schedule/list.go +++ b/database/schedule/list.go @@ -5,13 +5,14 @@ package schedule import ( + "context" "github.com/go-vela/types/constants" "github.com/go-vela/types/database" "github.com/go-vela/types/library" ) // ListSchedules gets a list of all schedules from the database. -func (e *engine) ListSchedules() ([]*library.Schedule, error) { +func (e *engine) ListSchedules(ctx context.Context) ([]*library.Schedule, error) { e.logger.Trace("listing all schedules from the database") // variables to store query results and return value @@ -20,7 +21,7 @@ func (e *engine) ListSchedules() ([]*library.Schedule, error) { schedules := []*library.Schedule{} // count the results - count, err := e.CountSchedules() + count, err := e.CountSchedules(ctx) if err != nil { return nil, err } diff --git a/database/schedule/list_active.go b/database/schedule/list_active.go index d0efa5dde..0afd4ddc5 100644 --- a/database/schedule/list_active.go +++ b/database/schedule/list_active.go @@ -5,13 +5,14 @@ package schedule import ( + "context" "github.com/go-vela/types/constants" "github.com/go-vela/types/database" "github.com/go-vela/types/library" ) // ListActiveSchedules gets a list of all active schedules from the database. -func (e *engine) ListActiveSchedules() ([]*library.Schedule, error) { +func (e *engine) ListActiveSchedules(ctx context.Context) ([]*library.Schedule, error) { e.logger.Trace("listing all active schedules from the database") // variables to store query results and return value @@ -20,7 +21,7 @@ func (e *engine) ListActiveSchedules() ([]*library.Schedule, error) { schedules := []*library.Schedule{} // count the results - count, err := e.CountActiveSchedules() + count, err := e.CountActiveSchedules(ctx) if err != nil { return nil, err } diff --git a/database/schedule/list_active_test.go b/database/schedule/list_active_test.go index 69c690def..35591a4ad 100644 --- a/database/schedule/list_active_test.go +++ b/database/schedule/list_active_test.go @@ -5,6 +5,7 @@ package schedule import ( + "context" "reflect" "testing" @@ -55,12 +56,12 @@ func TestSchedule_Engine_ListActiveSchedules(t *testing.T) { _sqlite := testSqlite(t) defer func() { _sql, _ := _sqlite.client.DB(); _sql.Close() }() - err := _sqlite.CreateSchedule(_scheduleOne) + err := _sqlite.CreateSchedule(context.TODO(), _scheduleOne) if err != nil { t.Errorf("unable to create test schedule for sqlite: %v", err) } - err = _sqlite.CreateSchedule(_scheduleTwo) + err = _sqlite.CreateSchedule(context.TODO(), _scheduleTwo) if err != nil { t.Errorf("unable to create test schedule for sqlite: %v", err) } @@ -89,7 +90,7 @@ func TestSchedule_Engine_ListActiveSchedules(t *testing.T) { // run tests for _, test := range tests { t.Run(test.name, func(t *testing.T) { - got, err := test.database.ListActiveSchedules() + got, err := test.database.ListActiveSchedules(context.TODO()) if test.failure { if err == nil { diff --git a/database/schedule/list_repo.go b/database/schedule/list_repo.go index 1137e0c4b..9d0cfed5e 100644 --- a/database/schedule/list_repo.go +++ b/database/schedule/list_repo.go @@ -5,6 +5,7 @@ package schedule import ( + "context" "github.com/go-vela/types/constants" "github.com/go-vela/types/database" "github.com/go-vela/types/library" @@ -12,7 +13,7 @@ import ( ) // ListSchedulesForRepo gets a list of schedules by repo ID from the database. -func (e *engine) ListSchedulesForRepo(r *library.Repo, page, perPage int) ([]*library.Schedule, int64, error) { +func (e *engine) ListSchedulesForRepo(ctx context.Context, r *library.Repo, page, perPage int) ([]*library.Schedule, int64, error) { e.logger.WithFields(logrus.Fields{ "org": r.GetOrg(), "repo": r.GetName(), @@ -24,7 +25,7 @@ func (e *engine) ListSchedulesForRepo(r *library.Repo, page, perPage int) ([]*li schedules := []*library.Schedule{} // count the results - count, err := e.CountSchedulesForRepo(r) + count, err := e.CountSchedulesForRepo(ctx, r) if err != nil { return nil, 0, err } diff --git a/database/schedule/list_repo_test.go b/database/schedule/list_repo_test.go index df56c0ee4..425e5dddb 100644 --- a/database/schedule/list_repo_test.go +++ b/database/schedule/list_repo_test.go @@ -5,6 +5,7 @@ package schedule import ( + "context" "reflect" "testing" @@ -59,12 +60,12 @@ func TestSchedule_Engine_ListSchedulesForRepo(t *testing.T) { _sqlite := testSqlite(t) defer func() { _sql, _ := _sqlite.client.DB(); _sql.Close() }() - err := _sqlite.CreateSchedule(_scheduleOne) + err := _sqlite.CreateSchedule(context.TODO(), _scheduleOne) if err != nil { t.Errorf("unable to create test schedule for sqlite: %v", err) } - err = _sqlite.CreateSchedule(_scheduleTwo) + err = _sqlite.CreateSchedule(context.TODO(), _scheduleTwo) if err != nil { t.Errorf("unable to create test schedule for sqlite: %v", err) } @@ -93,7 +94,7 @@ func TestSchedule_Engine_ListSchedulesForRepo(t *testing.T) { // run tests for _, test := range tests { t.Run(test.name, func(t *testing.T) { - got, _, err := test.database.ListSchedulesForRepo(_repo, 1, 10) + got, _, err := test.database.ListSchedulesForRepo(context.TODO(), _repo, 1, 10) if test.failure { if err == nil { diff --git a/database/schedule/list_test.go b/database/schedule/list_test.go index b0c3bb417..cdb13f756 100644 --- a/database/schedule/list_test.go +++ b/database/schedule/list_test.go @@ -5,6 +5,7 @@ package schedule import ( + "context" "reflect" "testing" @@ -54,12 +55,12 @@ func TestSchedule_Engine_ListSchedules(t *testing.T) { _sqlite := testSqlite(t) defer func() { _sql, _ := _sqlite.client.DB(); _sql.Close() }() - err := _sqlite.CreateSchedule(_scheduleOne) + err := _sqlite.CreateSchedule(context.TODO(), _scheduleOne) if err != nil { t.Errorf("unable to create test schedule for sqlite: %v", err) } - err = _sqlite.CreateSchedule(_scheduleTwo) + err = _sqlite.CreateSchedule(context.TODO(), _scheduleTwo) if err != nil { t.Errorf("unable to create test schedule for sqlite: %v", err) } @@ -88,7 +89,7 @@ func TestSchedule_Engine_ListSchedules(t *testing.T) { // run tests for _, test := range tests { t.Run(test.name, func(t *testing.T) { - got, err := test.database.ListSchedules() + got, err := test.database.ListSchedules(context.TODO()) if test.failure { if err == nil { diff --git a/database/schedule/opts.go b/database/schedule/opts.go index 7ac87bea1..cb2c89cfe 100644 --- a/database/schedule/opts.go +++ b/database/schedule/opts.go @@ -5,6 +5,7 @@ package schedule import ( + "context" "github.com/sirupsen/logrus" "gorm.io/gorm" @@ -42,3 +43,12 @@ func WithSkipCreation(skipCreation bool) EngineOpt { return nil } } + +// WithContext sets the context in the database engine for Schedules. +func WithContext(ctx context.Context) EngineOpt { + return func(e *engine) error { + e.ctx = ctx + + return nil + } +} diff --git a/database/schedule/schedule.go b/database/schedule/schedule.go index 0517dcae9..269278f52 100644 --- a/database/schedule/schedule.go +++ b/database/schedule/schedule.go @@ -5,6 +5,7 @@ package schedule import ( + "context" "fmt" "github.com/go-vela/types/constants" @@ -25,6 +26,8 @@ type ( // engine configuration settings used in schedule functions config *config + ctx context.Context + // gorm.io/gorm database client used in schedule functions // // https://pkg.go.dev/gorm.io/gorm#DB @@ -65,13 +68,13 @@ func New(opts ...EngineOpt) (*engine, error) { } // create the schedules table - err := e.CreateScheduleTable(e.client.Config.Dialector.Name()) + err := e.CreateScheduleTable(e.ctx, e.client.Config.Dialector.Name()) if err != nil { return nil, fmt.Errorf("unable to create %s table: %w", constants.TableSchedule, err) } // create the indexes for the schedules table - err = e.CreateScheduleIndexes() + err = e.CreateScheduleIndexes(e.ctx) if err != nil { return nil, fmt.Errorf("unable to create indexes for %s table: %w", constants.TableSchedule, err) } diff --git a/database/schedule/schedule_test.go b/database/schedule/schedule_test.go index fc4b4c6e1..aae0403a5 100644 --- a/database/schedule/schedule_test.go +++ b/database/schedule/schedule_test.go @@ -5,6 +5,7 @@ package schedule import ( + "context" "database/sql/driver" "reflect" "testing" @@ -63,6 +64,7 @@ func TestSchedule_New(t *testing.T) { logger: logger, skipCreation: false, want: &engine{ + ctx: context.TODO(), client: _postgres, config: &config{SkipCreation: false}, logger: logger, @@ -75,6 +77,7 @@ func TestSchedule_New(t *testing.T) { logger: logger, skipCreation: false, want: &engine{ + ctx: context.TODO(), client: _sqlite, config: &config{SkipCreation: false}, logger: logger, @@ -86,6 +89,7 @@ func TestSchedule_New(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { got, err := New( + WithContext(context.TODO()), WithClient(test.client), WithLogger(test.logger), WithSkipCreation(test.skipCreation), @@ -135,6 +139,7 @@ func testPostgres(t *testing.T) (*engine, sqlmock.Sqlmock) { } _engine, err := New( + WithContext(context.TODO()), WithClient(_postgres), WithLogger(logrus.NewEntry(logrus.StandardLogger())), WithSkipCreation(false), @@ -157,6 +162,7 @@ func testSqlite(t *testing.T) *engine { } _engine, err := New( + WithContext(context.TODO()), WithClient(_sqlite), WithLogger(logrus.NewEntry(logrus.StandardLogger())), WithSkipCreation(false), diff --git a/database/schedule/table.go b/database/schedule/table.go index 05ffe8bad..7ea6eb873 100644 --- a/database/schedule/table.go +++ b/database/schedule/table.go @@ -5,6 +5,7 @@ package schedule import ( + "context" "github.com/go-vela/types/constants" ) @@ -49,7 +50,7 @@ schedules ( ) // CreateScheduleTable creates the schedules table in the database. -func (e *engine) CreateScheduleTable(driver string) error { +func (e *engine) CreateScheduleTable(ctx context.Context, driver string) error { e.logger.Tracef("creating schedules table in the database") // handle the driver provided to create the table diff --git a/database/schedule/table_test.go b/database/schedule/table_test.go index c4dbf4e40..c0d785ed1 100644 --- a/database/schedule/table_test.go +++ b/database/schedule/table_test.go @@ -5,6 +5,7 @@ package schedule import ( + "context" "testing" "github.com/DATA-DOG/go-sqlmock" @@ -41,7 +42,7 @@ func TestSchedule_Engine_CreateScheduleTable(t *testing.T) { // run tests for _, test := range tests { t.Run(test.name, func(t *testing.T) { - err := test.database.CreateScheduleTable(test.name) + err := test.database.CreateScheduleTable(context.TODO(), test.name) if test.failure { if err == nil { diff --git a/database/schedule/update.go b/database/schedule/update.go index dd03f62c5..934b50c59 100644 --- a/database/schedule/update.go +++ b/database/schedule/update.go @@ -5,6 +5,7 @@ package schedule import ( + "context" "github.com/go-vela/types/constants" "github.com/go-vela/types/database" "github.com/go-vela/types/library" @@ -12,7 +13,7 @@ import ( ) // UpdateSchedule updates an existing schedule in the database. -func (e *engine) UpdateSchedule(s *library.Schedule, fields bool) error { +func (e *engine) UpdateSchedule(ctx context.Context, s *library.Schedule, fields bool) error { e.logger.WithFields(logrus.Fields{ "schedule": s.GetName(), }).Tracef("updating schedule %s in the database", s.GetName()) diff --git a/database/schedule/update_test.go b/database/schedule/update_test.go index 07cbfbc58..345c7b2db 100644 --- a/database/schedule/update_test.go +++ b/database/schedule/update_test.go @@ -5,6 +5,7 @@ package schedule import ( + "context" "testing" "github.com/DATA-DOG/go-sqlmock" @@ -40,7 +41,7 @@ WHERE "id" = $10`). _sqlite := testSqlite(t) defer func() { _sql, _ := _sqlite.client.DB(); _sql.Close() }() - err := _sqlite.CreateSchedule(_schedule) + err := _sqlite.CreateSchedule(context.TODO(), _schedule) if err != nil { t.Errorf("unable to create test schedule for sqlite: %v", err) } @@ -66,7 +67,7 @@ WHERE "id" = $10`). // run tests for _, test := range tests { t.Run(test.name, func(t *testing.T) { - err = test.database.UpdateSchedule(_schedule, true) + err = test.database.UpdateSchedule(context.TODO(), _schedule, true) if test.failure { if err == nil { @@ -112,7 +113,7 @@ func TestSchedule_Engine_UpdateSchedule_NotConfig(t *testing.T) { _sqlite := testSqlite(t) defer func() { _sql, _ := _sqlite.client.DB(); _sql.Close() }() - err := _sqlite.CreateSchedule(_schedule) + err := _sqlite.CreateSchedule(context.TODO(), _schedule) if err != nil { t.Errorf("unable to create test schedule for sqlite: %v", err) } @@ -138,7 +139,7 @@ func TestSchedule_Engine_UpdateSchedule_NotConfig(t *testing.T) { // run tests for _, test := range tests { t.Run(test.name, func(t *testing.T) { - err = test.database.UpdateSchedule(_schedule, false) + err = test.database.UpdateSchedule(context.TODO(), _schedule, false) if test.failure { if err == nil { diff --git a/router/middleware/schedule/schedule.go b/router/middleware/schedule/schedule.go index de06e0651..65f78c4e0 100644 --- a/router/middleware/schedule/schedule.go +++ b/router/middleware/schedule/schedule.go @@ -27,6 +27,7 @@ func Establish() gin.HandlerFunc { return func(c *gin.Context) { r := repo.Retrieve(c) u := user.Retrieve(c) + ctx := c.Request.Context() sParam := util.PathParameter(c, "schedule") if len(sParam) == 0 { @@ -45,7 +46,7 @@ func Establish() gin.HandlerFunc { "user": u.GetName(), }).Debugf("reading schedule %s for repo %s", sParam, r.GetFullName()) - s, err := database.FromContext(c).GetScheduleForRepo(r, sParam) + s, err := database.FromContext(c).GetScheduleForRepo(ctx, r, sParam) if err != nil { retErr := fmt.Errorf("unable to read schedule %s for repo %s: %w", sParam, r.GetFullName(), err) util.HandleError(c, http.StatusNotFound, retErr)