Skip to content

Commit

Permalink
fix: log container errors before container removal
Browse files Browse the repository at this point in the history
  • Loading branch information
davemooreuws committed Dec 12, 2024
1 parent a03f47b commit ddcca59
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
15 changes: 13 additions & 2 deletions pkg/project/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,6 @@ func (s *Batch) RunContainer(stop <-chan bool, updates chan<- ServiceRunUpdate,
}

hostConfig := &container.HostConfig{
// TODO: make this configurable through an cmd param
AutoRemove: true,
// LogConfig: *f.ce.Logger(f.runCtx).Config(),
LogConfig: container.LogConfig{
Type: "json-file",
Expand Down Expand Up @@ -252,6 +250,19 @@ func (s *Batch) RunContainer(stop <-chan bool, updates chan<- ServiceRunUpdate,
return nil
}

// defer removing container so logs can be retrieved, used instead of AutoRemove
defer func() {
err = dockerClient.ContainerRemove(context.Background(), containerId, container.RemoveOptions{})
if err != nil {
updates <- ServiceRunUpdate{
ServiceName: s.Name,
Label: s.GetFilePath(),
Status: ServiceRunStatus_Error,
Err: err,
}
}
}()

err = dockerClient.ContainerStart(context.TODO(), containerId, container.StartOptions{})
if err != nil {
updates <- ServiceRunUpdate{
Expand Down
15 changes: 13 additions & 2 deletions pkg/project/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,8 +336,6 @@ func (s *Service) RunContainer(stop <-chan bool, updates chan<- ServiceRunUpdate
}

hostConfig := &container.HostConfig{
// TODO: make this configurable through an cmd param
AutoRemove: true,
// LogConfig: *f.ce.Logger(f.runCtx).Config(),
LogConfig: container.LogConfig{
Type: "json-file",
Expand Down Expand Up @@ -424,6 +422,19 @@ func (s *Service) RunContainer(stop <-chan bool, updates chan<- ServiceRunUpdate
return nil
}

// defer removing container so logs can be retrieved, used instead of AutoRemove
defer func() {
err = dockerClient.ContainerRemove(context.Background(), containerId, container.RemoveOptions{})
if err != nil {
updates <- ServiceRunUpdate{
ServiceName: s.Name,
Label: s.GetFilePath(),
Status: ServiceRunStatus_Error,
Err: err,
}
}
}()

err = dockerClient.ContainerStart(context.TODO(), containerId, container.StartOptions{})
if err != nil {
updates <- ServiceRunUpdate{
Expand Down

0 comments on commit ddcca59

Please sign in to comment.