Skip to content

Commit

Permalink
Lock inside filestate.apply instead of Update/Delete/Import (#8806)
Browse files Browse the repository at this point in the history
This makes Delete/Update/Import/Preview the same for filestate and
httpstate. They could just be plain functions now except that `apply` is
hidden.

This also catches that when Watch did an update it wouldn't take the
lock for the filestate backend because Watch called `apply` directly not
`Update`.
  • Loading branch information
Frassle committed Jan 21, 2022
1 parent 831956d commit 8b4da4e
Showing 1 changed file with 9 additions and 28 deletions.
37 changes: 9 additions & 28 deletions pkg/backend/filestate/backend.go
Expand Up @@ -467,50 +467,22 @@ func (b *localBackend) Preview(ctx context.Context, stack backend.Stack,

func (b *localBackend) Update(ctx context.Context, stack backend.Stack,
op backend.UpdateOperation) (engine.ResourceChanges, result.Result) {

err := b.Lock(ctx, stack.Ref())
if err != nil {
return nil, result.FromError(err)
}
defer b.Unlock(ctx, stack.Ref())

return backend.PreviewThenPromptThenExecute(ctx, apitype.UpdateUpdate, stack, op, b.apply)
}

func (b *localBackend) Import(ctx context.Context, stack backend.Stack,
op backend.UpdateOperation, imports []deploy.Import) (engine.ResourceChanges, result.Result) {

err := b.Lock(ctx, stack.Ref())
if err != nil {
return nil, result.FromError(err)
}
defer b.Unlock(ctx, stack.Ref())

op.Imports = imports
return backend.PreviewThenPromptThenExecute(ctx, apitype.ResourceImportUpdate, stack, op, b.apply)
}

func (b *localBackend) Refresh(ctx context.Context, stack backend.Stack,
op backend.UpdateOperation) (engine.ResourceChanges, result.Result) {

err := b.Lock(ctx, stack.Ref())
if err != nil {
return nil, result.FromError(err)
}
defer b.Unlock(ctx, stack.Ref())

return backend.PreviewThenPromptThenExecute(ctx, apitype.RefreshUpdate, stack, op, b.apply)
}

func (b *localBackend) Destroy(ctx context.Context, stack backend.Stack,
op backend.UpdateOperation) (engine.ResourceChanges, result.Result) {

err := b.Lock(ctx, stack.Ref())
if err != nil {
return nil, result.FromError(err)
}
defer b.Unlock(ctx, stack.Ref())

return backend.PreviewThenPromptThenExecute(ctx, apitype.DestroyUpdate, stack, op, b.apply)
}

Expand Down Expand Up @@ -540,6 +512,15 @@ func (b *localBackend) apply(
colors.SpecHeadline+"%s (%s):"+colors.Reset+"\n"), actionLabel, stackRef)
}

// Take a lock unless this is just a preview
if kind != apitype.PreviewUpdate {
err := b.Lock(ctx, stack.Ref())
if err != nil {
return nil, result.FromError(err)
}
defer b.Unlock(ctx, stack.Ref())
}

// Start the update.
update, err := b.newUpdate(stackName, op)
if err != nil {
Expand Down

0 comments on commit 8b4da4e

Please sign in to comment.