Aborting tx if prebuild creation/deletion fails

Signed-off-by: Danny Kopping <danny@coder.com>
This commit is contained in:
Danny Kopping
2025-02-11 11:38:56 +02:00
parent 8cd6a6fb35
commit b32197937f

View File

@ -10,6 +10,7 @@ import (
"strings" "strings"
"time" "time"
"github.com/hashicorp/go-multierror"
"golang.org/x/exp/slices" "golang.org/x/exp/slices"
"github.com/coder/coder/v2/coderd/audit" "github.com/coder/coder/v2/coderd/audit"
@ -297,6 +298,7 @@ func (c Controller) reconcileTemplate(ctx context.Context, template database.Tem
return xerrors.Errorf("failed to retrieve template's prebuild states: %w", err) return xerrors.Errorf("failed to retrieve template's prebuild states: %w", err)
} }
var lastErr multierror.Error
for _, state := range versionStates { for _, state := range versionStates {
vlogger := logger.With(slog.F("template_version_id", state.TemplateVersionID)) vlogger := logger.With(slog.F("template_version_id", state.TemplateVersionID))
@ -329,20 +331,24 @@ func (c Controller) reconcileTemplate(ctx context.Context, template database.Tem
// i.e. we hold the advisory lock until all reconciliatory actions have been taken. // i.e. we hold the advisory lock until all reconciliatory actions have been taken.
// TODO: max per reconciliation iteration? // TODO: max per reconciliation iteration?
// TODO: probably need to split these to have a transaction each... rolling back would lead to an
// inconsistent state if 1 of n creations/deletions fail.
for _, id := range actions.createIDs { for _, id := range actions.createIDs {
if err := c.createPrebuild(ownerCtx, db, id, template); err != nil { if err := c.createPrebuild(ownerCtx, db, id, template); err != nil {
vlogger.Error(ctx, "failed to create prebuild", slog.Error(err)) vlogger.Error(ctx, "failed to create prebuild", slog.Error(err))
lastErr.Errors = append(lastErr.Errors, err)
} }
} }
for _, id := range actions.deleteIDs { for _, id := range actions.deleteIDs {
if err := c.deletePrebuild(ownerCtx, db, id, template); err != nil { if err := c.deletePrebuild(ownerCtx, db, id, template); err != nil {
vlogger.Error(ctx, "failed to delete prebuild", slog.Error(err)) vlogger.Error(ctx, "failed to delete prebuild", slog.Error(err))
lastErr.Errors = append(lastErr.Errors, err)
} }
} }
} }
return nil return lastErr.ErrorOrNil()
}, &database.TxOptions{ }, &database.TxOptions{
// TODO: isolation // TODO: isolation
TxIdentifier: "template_prebuilds", TxIdentifier: "template_prebuilds",