Skip to content

Commit

Permalink
fix(spanner): use correct logger when stopping emulator
Browse files Browse the repository at this point in the history
Fixes a bug introduced in b1b5e7e
(PR #594) where an empty context is used, which removes the logger.

Could be done cleaner with Go >=1.21 as can be seen in a TODO comment.

Before:
```
❯ make go-test
[sage] building binary and generating Makefiles...
[go-test] running Go tests...
[go-test] starting Cloud Spanner emulator...
[go-test] running Cloud Spanner emulator on 0.0.0.0:44611
[sage] stopping down Cloud Spanner emulator...
```

After:
```
❯ make go-test
[sage] building binary and generating Makefiles...
[go-test] running Go tests...
[go-test] starting Cloud Spanner emulator...
[go-test] running Cloud Spanner emulator on 0.0.0.0:46023
[go-test] stopping down Cloud Spanner emulator...
```
radhus committed Oct 31, 2024
1 parent 1d904cc commit 23d5113
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions tools/sgcloudspanner/emulator.go
Original file line number Diff line number Diff line change
@@ -44,21 +44,24 @@ func RunEmulator(ctx context.Context) (_ func(), err error) {
}
containerID := strings.TrimSpace(dockerRunStdout.String())
cleanup := func() {
// TODO(radhus): when bumping to Go >=1.21, we should define a context like this instead and avoid storing a logger.
// ctx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 10*time.Second)
logger := sg.Logger(ctx)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
sg.Logger(ctx).Println("stopping down Cloud Spanner emulator...")
logger.Println("stopping down Cloud Spanner emulator...")
cmd := sgdocker.Command(ctx, "kill", containerID)
cmd.Stdout, cmd.Stderr = nil, nil
if err := cmd.Run(); err != nil {
sg.Logger(ctx).Printf("failed to kill emulator container: %v", err)
logger.Printf("failed to kill emulator container: %v", err)
}
cmd = sgdocker.Command(ctx, "rm", "-v", containerID)
cmd.Stdout, cmd.Stderr = nil, nil
if err := cmd.Run(); err != nil {
sg.Logger(ctx).Printf("failed to remove emulator container: %v", err)
logger.Printf("failed to remove emulator container: %v", err)
}
if err := os.Unsetenv("SPANNER_EMULATOR_HOST"); err != nil {
sg.Logger(ctx).Printf("failed to unset SPANNER_EMULATOR_HOST: %v", err)
logger.Printf("failed to unset SPANNER_EMULATOR_HOST: %v", err)
}
}
emulatorHost, err := inspectPortAddress(ctx, containerID, "9010/tcp")

0 comments on commit 23d5113

Please sign in to comment.