From 1705acf506d67501b8d8a9213cc10f3d6d3e0eda Mon Sep 17 00:00:00 2001 From: Bradley Cicenas Date: Thu, 26 Jan 2017 00:53:03 +0000 Subject: [PATCH] add interval refresh of running/killed containers in map --- collector/docker.go | 2 ++ container.go | 3 ++- containermap.go | 22 +++++++++++++++++++++- grid.go | 5 +++++ 4 files changed, 30 insertions(+), 2 deletions(-) diff --git a/collector/docker.go b/collector/docker.go index 3c54e0d..b231528 100644 --- a/collector/docker.go +++ b/collector/docker.go @@ -33,6 +33,8 @@ func NewDocker(client *api.Client, id string) *Docker { }() go func() { + defer close(stats) + defer close(c.stream) for s := range stats { c.ReadCPU(s) c.ReadMem(s) diff --git a/container.go b/container.go index 1c16c0c..6fb7ca1 100644 --- a/container.go +++ b/container.go @@ -8,7 +8,7 @@ import ( type Container struct { id string name string - done chan bool + dead bool metrics collector.Metrics collect collector.Collector widgets widgets.ContainerWidgets @@ -30,5 +30,6 @@ func (c *Container) Collect() { c.widgets.SetMem(metrics.MemUsage, metrics.MemLimit, metrics.MemPercent) c.widgets.SetNet(metrics.NetRx, metrics.NetTx) } + c.dead = true }() } diff --git a/containermap.go b/containermap.go index 1e2378b..531e8b1 100644 --- a/containermap.go +++ b/containermap.go @@ -36,6 +36,7 @@ type ContainerMap struct { func (cm *ContainerMap) Refresh() { var id, name string + opts := docker.ListContainersOptions{ Filters: filters, } @@ -43,6 +44,8 @@ func (cm *ContainerMap) Refresh() { if err != nil { panic(err) } + + // add new containers for _, c := range containers { id = c.ID[:12] if _, ok := cm.containers[id]; ok == false { @@ -50,13 +53,22 @@ func (cm *ContainerMap) Refresh() { cm.containers[id] = &Container{ id: id, name: name, - done: make(chan bool), collect: collector.NewDocker(cm.client, id), widgets: widgets.NewCompact(id, name), } cm.containers[id].Collect() } } + + // remove dead containers + var removeIDs []string + for id, c := range cm.containers { + if c.dead { + removeIDs = append(removeIDs, id) + } + } + cm.Del(removeIDs...) + } // Kill a container by ID @@ -78,9 +90,17 @@ func (cm *ContainerMap) Get(id string) *Container { return cm.containers[id] } +// Remove one or more containers +func (cm *ContainerMap) Del(ids ...string) { + for _, id := range ids { + delete(cm.containers, id) + } +} + // Return array of all containers, sorted by field func (cm *ContainerMap) All() []*Container { var containers Containers + filter := GlobalConfig["filterStr"] re := regexp.MustCompile(fmt.Sprintf(".*%s", filter)) diff --git a/grid.go b/grid.go index 833e228..f03e09f 100644 --- a/grid.go +++ b/grid.go @@ -129,6 +129,7 @@ func (g *Grid) ExpandView() { func Display(g *Grid) bool { var menu func() var expand bool + var loopIter int // calculate layout ui.Body.Align() @@ -164,6 +165,10 @@ func Display(g *Grid) bool { ui.StopLoop() }) ui.Handle("/timer/1s", func(e ui.Event) { + loopIter++ + if loopIter%5 == 0 { + g.cmap.Refresh() + } g.containers = g.cmap.All() // refresh containers for current sort order g.redrawRows() })