Skip to content

Commit

Permalink
fix: workspace creation log duplication (#18)
Browse files Browse the repository at this point in the history
Signed-off-by: Toma Puljak <[email protected]>
  • Loading branch information
Tpuljak authored Mar 26, 2024
1 parent a5e6988 commit 2b9e652
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 12 deletions.
9 changes: 8 additions & 1 deletion pkg/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,14 @@ func (p DockerProvider) CreateProject(projectReq *provider.ProjectRequest) (*typ
return new(types.Empty), err
}

err = util.StartContainer(client, projectReq.Project, &logWriter)
go func() {
err := util.GetContainerLogs(client, util.GetContainerName(projectReq.Project), &logWriter)
if err != nil {
logWriter.Write([]byte(err.Error()))
}
}()

err = util.StartContainer(client, projectReq.Project, nil)
if err != nil {
return new(types.Empty), err
}
Expand Down
30 changes: 30 additions & 0 deletions pkg/provider/util/container_logs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package util

import (
"context"
"io"

docker_types "github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"github.com/docker/docker/pkg/stdcopy"
)

func GetContainerLogs(client *client.Client, containerName string, logWriter *io.Writer) error {
if logWriter == nil {
return nil
}

logs, err := client.ContainerLogs(context.Background(), containerName, docker_types.ContainerLogsOptions{
ShowStdout: true,
ShowStderr: true,
Follow: true,
})
if err != nil {
return err
}
defer logs.Close()

_, err = stdcopy.StdCopy(*logWriter, *logWriter, logs)

return err
}
19 changes: 8 additions & 11 deletions pkg/provider/util/start_container.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,19 @@ import (
"github.com/daytonaio/daytona/pkg/types"
docker_types "github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"github.com/docker/docker/pkg/stdcopy"
)

func StartContainer(client *client.Client, project *types.Project, logWriter *io.Writer) error {
containerName := GetContainerName(project)
ctx := context.Background()

err := client.ContainerStart(ctx, containerName, docker_types.ContainerStartOptions{})
inspect, err := client.ContainerInspect(ctx, containerName)

if err == nil && inspect.State.Running {
return nil
}

err = client.ContainerStart(ctx, containerName, docker_types.ContainerStartOptions{})
if err != nil {
return err
}
Expand All @@ -37,18 +42,10 @@ func StartContainer(client *client.Client, project *types.Project, logWriter *io

if logWriter != nil {
go func() {
logs, err := client.ContainerLogs(ctx, containerName, docker_types.ContainerLogsOptions{
ShowStdout: true,
ShowStderr: true,
Follow: true,
})
err := GetContainerLogs(client, containerName, logWriter)
if err != nil {
(*logWriter).Write([]byte(err.Error()))
return
}
defer logs.Close()

stdcopy.StdCopy(*logWriter, *logWriter, logs)
}()
}

Expand Down

0 comments on commit 2b9e652

Please sign in to comment.