Skip to content

Commit

Permalink
feat(server): send more container logs
Browse files Browse the repository at this point in the history
Signed-off-by: Quentin Guidée <[email protected]>
  • Loading branch information
quentinguidee committed Mar 9, 2024
1 parent d68bdff commit 268db98
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 6 deletions.
26 changes: 20 additions & 6 deletions server/apps/containers/adapter/runner_docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,9 @@ func (a runnerDockerAdapter) Start(
// Build
stdout, err := a.buildImageFromName(ctx, c.GetImageNameWithTag())
if err != nil {
log.Error(err)
log.Error(err, vlog.String("step", "build"))
setStatus(types.ContainerStatusError)
_, _ = wErr.Write([]byte(err.Error() + "\n"))
return
}

Expand All @@ -105,8 +106,10 @@ func (a runnerDockerAdapter) Start(
err := json.Unmarshal(scanner.Bytes(), &msg)
if err != nil {
log.Error(err,
vlog.String("step", "build"),
vlog.String("text", scanner.Text()),
vlog.String("id", c.ID.String()))
_, _ = wErr.Write([]byte(err.Error() + "\n"))
continue
}

Expand All @@ -123,24 +126,30 @@ func (a runnerDockerAdapter) Start(
progressJSON, err := json.Marshal(progress)
if err != nil {
log.Error(err,
vlog.String("step", "build"),
vlog.String("text", scanner.Text()),
vlog.String("id", c.ID.String()))
_, _ = wErr.Write([]byte(err.Error() + "\n"))
continue
}

_, err = fmt.Fprintf(wOut, "%s %s\n", "DOWNLOAD", progressJSON)
if err != nil {
log.Error(err,
vlog.String("step", "build"),
vlog.String("text", scanner.Text()),
vlog.String("id", c.ID.String()))
setStatus(types.ContainerStatusError)
_, _ = wErr.Write([]byte(err.Error() + "\n"))
return
}
}
if scanner.Err() != nil {
log.Error(scanner.Err(),
vlog.String("step", "build"),
vlog.String("id", c.ID.String()))
setStatus(types.ContainerStatusError)
_, _ = wErr.Write([]byte(err.Error() + "\n"))
return
}
}()
Expand Down Expand Up @@ -182,31 +191,35 @@ func (a runnerDockerAdapter) Start(

id, err = a.createContainer(ctx, opts)
if err != nil {
log.Error(err)
log.Error(err, vlog.String("step", "create"))
setStatus(types.ContainerStatusError)
_, _ = wErr.Write([]byte(err.Error() + "\n"))
return
}
} else if err != nil {
log.Error(err)
log.Error(err, vlog.String("step", "create"))
setStatus(types.ContainerStatusError)
_, _ = wErr.Write([]byte(err.Error() + "\n"))
return
}

// Start
cli := containersapi.NewContainersKernelClient(ctx)
err = cli.StartContainer(context.Background(), id)
if err != nil {
log.Error(err)
log.Error(err, vlog.String("step", "start"))
setStatus(types.ContainerStatusError)
_, _ = wErr.Write([]byte(err.Error() + "\n"))
return
}
setStatus(types.ContainerStatusRunning)

var stderr io.ReadCloser
stdout, stderr, err = a.readLogs(ctx, id)
if err != nil {
log.Error(err)
log.Error(err, vlog.String("step", "read logs"))
setStatus(types.ContainerStatusError)
_, _ = wErr.Write([]byte(err.Error() + "\n"))
return
}

Expand Down Expand Up @@ -234,8 +247,9 @@ func (a runnerDockerAdapter) Start(

err = a.WaitCondition(ctx, c, types.WaitContainerCondition(container.WaitConditionNotRunning))
if err != nil {
log.Error(err)
log.Error(err, vlog.String("step", "wait condition"))
setStatus(types.ContainerStatusError)
_, _ = wErr.Write([]byte(err.Error() + "\n"))
} else {
setStatus(types.ContainerStatusOff)
}
Expand Down
33 changes: 33 additions & 0 deletions server/apps/containers/core/service/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,13 @@ func (s *containerService) Start(ctx context.Context, id uuid.UUID) error {
stdout, stderr, err := s.runner.Start(ctx, c, ports, volumes, env, caps, sysctls, setStatus)
if err != nil {
s.setStatus(c, types.ContainerStatusError)

s.ctx.DispatchEvent(types.EventContainerLog{
ContainerID: id,
Kind: types.LogKindVertexErr,
Message: types.NewLogLineMessageString(err.Error()),
})

return err
}

Expand All @@ -401,6 +408,13 @@ func (s *containerService) Start(ctx context.Context, id uuid.UUID) error {
err := json.Unmarshal([]byte(msg), &downloadProgress)
if err != nil {
log.Error(err)

s.ctx.DispatchEvent(types.EventContainerLog{
ContainerID: id,
Kind: types.LogKindOut,
Message: types.NewLogLineMessageString(err.Error()),
})

continue
}

Expand All @@ -420,6 +434,12 @@ func (s *containerService) Start(ctx context.Context, id uuid.UUID) error {
}
if scanner.Err() != nil {
log.Error(scanner.Err())

s.ctx.DispatchEvent(types.EventContainerLog{
ContainerID: id,
Kind: types.LogKindErr,
Message: types.NewLogLineMessageString(scanner.Err().Error()),
})
}
}()

Expand All @@ -432,8 +452,15 @@ func (s *containerService) Start(ctx context.Context, id uuid.UUID) error {
Message: types.NewLogLineMessageString(scanner.Text()),
})
}

if scanner.Err() != nil {
log.Error(scanner.Err())

s.ctx.DispatchEvent(types.EventContainerLog{
ContainerID: id,
Kind: types.LogKindErr,
Message: types.NewLogLineMessageString(scanner.Err().Error()),
})
}
}()

Expand All @@ -446,6 +473,12 @@ func (s *containerService) Start(ctx context.Context, id uuid.UUID) error {
err := s.WaitStatus(ctx, id, types.ContainerStatusRunning)
if err != nil {
log.Error(err)

s.ctx.DispatchEvent(types.EventContainerLog{
ContainerID: id,
Kind: types.LogKindVertexErr,
Message: types.NewLogLineMessageString(err.Error()),
})
}
}()
wg.Wait()
Expand Down

0 comments on commit 268db98

Please sign in to comment.