Skip to content

Commit b321979

Browse files
committed
Aborting tx if prebuild creation/deletion fails
Signed-off-by: Danny Kopping <danny@coder.com>
1 parent 8cd6a6f commit b321979

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

coderd/prebuilds/controller.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"strings"
1111
"time"
1212

13+
"github.com/hashicorp/go-multierror"
1314
"golang.org/x/exp/slices"
1415

1516
"github.com/coder/coder/v2/coderd/audit"
@@ -297,6 +298,7 @@ func (c Controller) reconcileTemplate(ctx context.Context, template database.Tem
297298
return xerrors.Errorf("failed to retrieve template's prebuild states: %w", err)
298299
}
299300

301+
var lastErr multierror.Error
300302
for _, state := range versionStates {
301303
vlogger := logger.With(slog.F("template_version_id", state.TemplateVersionID))
302304

@@ -329,20 +331,24 @@ func (c Controller) reconcileTemplate(ctx context.Context, template database.Tem
329331
// i.e. we hold the advisory lock until all reconciliatory actions have been taken.
330332
// TODO: max per reconciliation iteration?
331333

334+
// TODO: probably need to split these to have a transaction each... rolling back would lead to an
335+
// inconsistent state if 1 of n creations/deletions fail.
332336
for _, id := range actions.createIDs {
333337
if err := c.createPrebuild(ownerCtx, db, id, template); err != nil {
334338
vlogger.Error(ctx, "failed to create prebuild", slog.Error(err))
339+
lastErr.Errors = append(lastErr.Errors, err)
335340
}
336341
}
337342

338343
for _, id := range actions.deleteIDs {
339344
if err := c.deletePrebuild(ownerCtx, db, id, template); err != nil {
340345
vlogger.Error(ctx, "failed to delete prebuild", slog.Error(err))
346+
lastErr.Errors = append(lastErr.Errors, err)
341347
}
342348
}
343349
}
344350

345-
return nil
351+
return lastErr.ErrorOrNil()
346352
}, &database.TxOptions{
347353
// TODO: isolation
348354
TxIdentifier: "template_prebuilds",

0 commit comments

Comments
 (0)