From 20db7f7cc92d49120685d4800d67bff7257c8621 Mon Sep 17 00:00:00 2001 From: Trey Ivy Date: Thu, 7 Nov 2024 14:02:19 +0000 Subject: [PATCH 1/2] Wrap SaveSummary in transaction --- pkg/processing/save.go | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/pkg/processing/save.go b/pkg/processing/save.go index a55b624..4df641f 100644 --- a/pkg/processing/save.go +++ b/pkg/processing/save.go @@ -22,14 +22,40 @@ import ( "github.com/buildbarn/bb-portal/pkg/summary/detectors" ) -// SaveActor ...SaveActor The save actor struct with the db client and a blob archiver. +// SaveActor The save actor struct with the db client and a blob archiver. type SaveActor struct { db *ent.Client blobArchiver BlobMultiArchiver } -// SaveSummary saves an invocation summary to the database. +// Rollback a transaction on error +func rollback(tx *ent.Tx, err error) error { + if rerr := tx.Rollback(); rerr != nil { + err = fmt.Errorf("%w: %v", err, rerr) + } + return err +} + +// SaveSummary saves an invocation summary to the database as a transaction or rollsback on failure. func (act SaveActor) SaveSummary(ctx context.Context, summary *summary.Summary) (*ent.BazelInvocation, error) { + tx, err := act.db.Tx(ctx) + if err != nil { + return nil, err + } + act.db = tx.Client() + result, err := act.saveSummary(ctx, summary) + if err != nil { + return nil, rollback(tx, err) + } + err = tx.Commit() + if err != nil { + return nil, err + } + return result, nil +} + +// saveSummary saves an invocation summary to the database. +func (act SaveActor) saveSummary(ctx context.Context, summary *summary.Summary) (*ent.BazelInvocation, error) { // errors := []error{} if summary.InvocationID == "" { slog.ErrorContext(ctx, "No Invocation ID Found on summary", "ctx.Err()", ctx.Err()) From 034877f0eee2765691f70de2179959628ce3f07e Mon Sep 17 00:00:00 2001 From: Trey Ivy Date: Thu, 7 Nov 2024 14:06:40 +0000 Subject: [PATCH 2/2] add log msg --- pkg/processing/save.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/processing/save.go b/pkg/processing/save.go index 4df641f..558b8de 100644 --- a/pkg/processing/save.go +++ b/pkg/processing/save.go @@ -45,6 +45,7 @@ func (act SaveActor) SaveSummary(ctx context.Context, summary *summary.Summary) act.db = tx.Client() result, err := act.saveSummary(ctx, summary) if err != nil { + slog.ErrorContext(ctx, "Error saving the invocation to the database. Rolling back the transaction", "err", err) return nil, rollback(tx, err) } err = tx.Commit()