Skip to content

Commit

Permalink
♻️ Make containerId explicit
Browse files Browse the repository at this point in the history
  • Loading branch information
elgohr committed Dec 25, 2023
1 parent 72e7377 commit 4caf851
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions localstack.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,19 +268,20 @@ func (i *Instance) start(ctx context.Context, services ...Service) error {
}
}

if err := i.startLocalstack(ctx, services...); err != nil {
containerId, err := i.startLocalstack(ctx, services...)
if err != nil {
return err
}

i.log.Info("waiting for localstack to start...")
return i.waitToBeAvailable(ctx)
return i.waitToBeAvailable(ctx, containerId)
}

const imageName = "go-localstack"

func (i *Instance) startLocalstack(ctx context.Context, services ...Service) error {
func (i *Instance) startLocalstack(ctx context.Context, services ...Service) (string, error) {
if err := i.buildLocalImage(ctx); err != nil {
return fmt.Errorf("localstack: could not build image: %w", err)
return "", fmt.Errorf("localstack: could not build image: %w", err)
}

pm := nat.PortMap{}
Expand Down Expand Up @@ -316,22 +317,22 @@ func (i *Instance) startLocalstack(ctx context.Context, services ...Service) err
AutoRemove: true,
}, nil, nil, "")
if err != nil {
return fmt.Errorf("localstack: could not create container: %w", err)
return "", fmt.Errorf("localstack: could not create container: %w", err)
}

i.setContainerId(resp.ID)

i.log.Info("starting localstack")
containerId := resp.ID
if err := i.cli.ContainerStart(ctx, containerId, types.ContainerStartOptions{}); err != nil {
return fmt.Errorf("localstack: could not start container: %w", err)
return "", fmt.Errorf("localstack: could not start container: %w", err)
}

if i.log.Level == logrus.DebugLevel {
go i.writeContainerLogToLogger(ctx, containerId)
}

return i.mapPorts(ctx, services, containerId, 0)
return containerId, i.mapPorts(ctx, services, containerId, 0)
}

//go:embed Dockerfile
Expand Down Expand Up @@ -415,7 +416,7 @@ func (i *Instance) stop() error {
if containerId == "" {
return nil
}
timeout := int(time.Second.Seconds())
timeout := 1
if err := i.cli.ContainerStop(context.Background(), containerId, container.StopOptions{Timeout: &timeout}); err != nil {
return err
}
Expand All @@ -424,15 +425,15 @@ func (i *Instance) stop() error {
return nil
}

func (i *Instance) waitToBeAvailable(ctx context.Context) error {
func (i *Instance) waitToBeAvailable(ctx context.Context, containerId string) error {
ticker := time.NewTicker(300 * time.Millisecond)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return ctx.Err()
case <-ticker.C:
if err := i.isRunning(ctx); err != nil {
if err := i.isRunning(ctx, containerId); err != nil {
return err
}
if err := i.checkAvailable(ctx); err != nil {
Expand All @@ -445,8 +446,8 @@ func (i *Instance) waitToBeAvailable(ctx context.Context) error {
}
}

func (i *Instance) isRunning(ctx context.Context) error {
_, err := i.cli.ContainerInspect(ctx, i.getContainerId())
func (i *Instance) isRunning(ctx context.Context, containerId string) error {
_, err := i.cli.ContainerInspect(ctx, containerId)
if err != nil {
i.log.Debug(err)
return errors.New("localstack container has been stopped")
Expand Down

0 comments on commit 4caf851

Please sign in to comment.