From 2b04623e002b27fb20c2caf0719cd9d4a2554e8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Thu, 3 Mar 2022 13:46:27 +0100 Subject: [PATCH] Manager: fix DB transaction isolation issue in task scheduler The created transaction wasn't actually used for the should-be-in-the- transaction queries. That's now resolved. --- internal/manager/persistence/jobs.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/internal/manager/persistence/jobs.go b/internal/manager/persistence/jobs.go index aa143d21..875c106c 100644 --- a/internal/manager/persistence/jobs.go +++ b/internal/manager/persistence/jobs.go @@ -114,7 +114,7 @@ func (js *StringStringMap) Scan(value interface{}) error { // StoreJob stores an AuthoredJob and its tasks, and saves it to the database. // The job will be in 'under construction' status. It is up to the caller to transition it to its desired initial status. func (db *DB) StoreAuthoredJob(ctx context.Context, authoredJob job_compilers.AuthoredJob) error { - return db.gormDB.Transaction(func(tx *gorm.DB) error { + return db.gormDB.WithContext(ctx).Transaction(func(tx *gorm.DB) error { // TODO: separate conversion of struct types from storing things in the database. dbJob := Job{ UUID: authoredJob.JobID, @@ -126,7 +126,7 @@ func (db *DB) StoreAuthoredJob(ctx context.Context, authoredJob job_compilers.Au Metadata: StringStringMap(authoredJob.Metadata), } - if err := db.gormDB.WithContext(ctx).Create(&dbJob).Error; err != nil { + if err := tx.Create(&dbJob).Error; err != nil { return fmt.Errorf("error storing job: %v", err) } @@ -150,7 +150,7 @@ func (db *DB) StoreAuthoredJob(ctx context.Context, authoredJob job_compilers.Au Commands: commands, // dependencies are stored below. } - if err := db.gormDB.WithContext(ctx).Create(&dbTask).Error; err != nil { + if err := tx.Create(&dbTask).Error; err != nil { return fmt.Errorf("error storing task: %v", err) } @@ -178,7 +178,7 @@ func (db *DB) StoreAuthoredJob(ctx context.Context, authoredJob job_compilers.Au } dbTask.Dependencies = deps - if err := db.gormDB.WithContext(ctx).Save(dbTask).Error; err != nil { + if err := tx.Save(dbTask).Error; err != nil { return fmt.Errorf("unable to store dependencies of task %q: %w", authoredTask.UUID, err) } }