123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- package Docker
- import (
- "context"
- "github.com/docker/docker/api/types/container"
- "github.com/docker/docker/client"
- "regexp"
- "sync"
- "time"
- )
- var OnlineContainerID map[string]container.Summary
- var OnlineContainerMux sync.RWMutex
- var isMonitoring bool
- var monitorTimer *time.Timer
- var debounceDuration = 5 * time.Second // 设置消抖时间为5秒
- func DockerOnlineContainer() {
- if isMonitoring {
- return
- }
- isMonitoring = true
- ctx := context.Background()
- cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
- if err != nil {
- return
- }
- // 查看正在运行的容器
- containers, err := cli.ContainerList(ctx, container.ListOptions{})
- if err != nil {
- return
- }
- OnlineContainerMux.Lock()
- OnlineContainerID = make(map[string]container.Summary)
- compile, err := regexp.Compile("business-go")
- if err != nil {
- return
- }
- for _, info := range containers {
- if !compile.MatchString(info.Image) {
- continue
- }
- OnlineContainerID[info.ID] = info
- }
- OnlineContainerMux.Unlock()
- if monitorTimer != nil {
- monitorTimer.Stop()
- }
- monitorTimer = time.AfterFunc(debounceDuration, func() {
- isMonitoring = false
- })
- return
- }
|