From ccf97c34884570e48117deae15098d3133be2403 Mon Sep 17 00:00:00 2001 From: linrunqi08 Date: Mon, 9 Dec 2024 13:31:10 +0800 Subject: [PATCH] refine code --- pkg/helper/docker_center.go | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/pkg/helper/docker_center.go b/pkg/helper/docker_center.go index c586acac02..13fc22366b 100644 --- a/pkg/helper/docker_center.go +++ b/pkg/helper/docker_center.go @@ -16,6 +16,7 @@ package helper import ( "context" + "errors" "hash/fnv" "io" "path" @@ -1094,10 +1095,19 @@ func (dc *DockerCenter) fetchAll() error { for _, container := range containers { var containerDetail types.ContainerJSON - var isAlive bool - containerDetail, isAlive, err = dc.inspectOneContainer(container.ID) - if err == nil && isAlive { + 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) { + continue + } containerMap[container.ID] = dc.CreateInfoDetail(containerDetail, envConfigPrefix, false) + } else { + dc.setLastError(err, "inspect container error "+container.ID) } } dc.updateContainers(containerMap) @@ -1107,8 +1117,15 @@ func (dc *DockerCenter) fetchAll() error { func (dc *DockerCenter) fetchOne(containerID string, tryFindSandbox bool) error { dc.containerStateLock.Lock() defer dc.containerStateLock.Unlock() - containerDetail, isAlive, err := dc.inspectOneContainer(containerID) - if err != nil || !isAlive { + containerDetail, err := dc.client.ContainerInspect(context.Background(), containerID) + if err != nil { + dc.setLastError(err, "inspect container error "+containerID) + return err + } + if !ContainerProcessAlive(containerDetail.State.Pid) { + errMsg := "inspect time out container " + containerID + err = errors.New(errMsg) + dc.setLastError(err, errMsg) return err } // docker 场景下