Skip to content

Commit

Permalink
Revert "Fix the issue of missing container information caused by the …
Browse files Browse the repository at this point in the history
…event sequence when docker compose is repeatedly up. (#1875)"

This reverts commit 9005b10.
  • Loading branch information
linrunqi08 committed Dec 12, 2024
1 parent 13ab38a commit 814638b
Showing 1 changed file with 23 additions and 32 deletions.
55 changes: 23 additions & 32 deletions pkg/helper/docker_center.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package helper

import (
"context"
"errors"
"hash/fnv"
"path"
"regexp"
Expand Down Expand Up @@ -1023,35 +1022,6 @@ func (dc *DockerCenter) updateContainer(id string, container *DockerInfoDetail)
dc.refreshLastUpdateMapTime()
}

func (dc *DockerCenter) inspectOneContainer(containerID string) (types.ContainerJSON, error) {
var err error
var containerDetail types.ContainerJSON
for idx := 0; idx < 3; idx++ {
if containerDetail, err = dc.client.ContainerInspect(context.Background(), containerID); err == nil {
break
}
time.Sleep(time.Second * 5)
}
if err != nil {
dc.setLastError(err, "inspect container error "+containerID)
return types.ContainerJSON{}, err
}
if !ContainerProcessAlive(containerDetail.State.Pid) {
containerDetail.State.Status = ContainerStatusExited
finishedAt := containerDetail.State.FinishedAt
finishedAtTime, _ := time.Parse(time.RFC3339, finishedAt)
now := time.Now()
duration := now.Sub(finishedAtTime)
if duration >= ContainerInfoDeletedTimeout {
errMsg := "inspect time out container " + containerID
err = errors.New(errMsg)
dc.setLastError(err, errMsg)
return types.ContainerJSON{}, err
}
}
return containerDetail, nil
}

func (dc *DockerCenter) fetchAll() error {
dc.containerStateLock.Lock()
defer dc.containerStateLock.Unlock()
Expand All @@ -1065,9 +1035,26 @@ func (dc *DockerCenter) fetchAll() error {

for _, container := range containers {
var containerDetail types.ContainerJSON
containerDetail, err = dc.inspectOneContainer(container.ID)
for idx := 0; idx < 3; idx++ {
if containerDetail, err = dc.client.ContainerInspect(context.Background(), container.ID); err == nil {
break
}
time.Sleep(time.Second * 5)
}
if err == nil {
if !ContainerProcessAlive(containerDetail.State.Pid) {
containerDetail.State.Status = ContainerStatusExited
finishedAt := containerDetail.State.FinishedAt
finishedAtTime, _ := time.Parse(time.RFC3339, finishedAt)
now := time.Now()
duration := now.Sub(finishedAtTime)
if duration >= ContainerInfoDeletedTimeout {
continue
}
}
containerMap[container.ID] = dc.CreateInfoDetail(containerDetail, envConfigPrefix, false)
} else {
dc.setLastError(err, "inspect container error "+container.ID)
}
}
dc.updateContainers(containerMap)
Expand All @@ -1078,10 +1065,14 @@ func (dc *DockerCenter) fetchAll() error {
func (dc *DockerCenter) fetchOne(containerID string, tryFindSandbox bool) error {
dc.containerStateLock.Lock()
defer dc.containerStateLock.Unlock()
containerDetail, err := dc.inspectOneContainer(containerID)
containerDetail, err := dc.client.ContainerInspect(context.Background(), containerID)
if err != nil {
dc.setLastError(err, "inspect container error "+containerID)
return err
}
if containerDetail.State.Status == ContainerStatusRunning && !ContainerProcessAlive(containerDetail.State.Pid) {
containerDetail.State.Status = ContainerStatusExited
}
// docker 场景下
// tryFindSandbox如果是false, 那么fetchOne的地方应该会调用两次,一次是sandbox的id,一次是业务容器的id
// tryFindSandbox如果是true, 调用的地方只会有一个业务容器的id,然后依赖fetchOne内部把sandbox信息补全
Expand Down

0 comments on commit 814638b

Please sign in to comment.