From a7811b4e339ebe8ebd621c8f6c4dee54c246626c Mon Sep 17 00:00:00 2001 From: Marcos Lilljedahl Date: Fri, 2 Dec 2016 10:29:41 -0300 Subject: [PATCH] Omit race conditions and update swarm info events --- docker-compose.yml | 4 ++-- services/check_swarm_status_task.go | 2 ++ services/collect_stats_task.go | 18 +++++++++--------- services/session.go | 3 ++- 4 files changed, 15 insertions(+), 12 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 407ce90..75b3133 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,12 +1,12 @@ version: '2' services: - web: + pwd: # pwd daemon container always needs to be named this way container_name: pwd # use the latest golang image image: golang # go to the right place and starts the app - command: /bin/sh -c 'cd /go/src/github.com/franela/play-with-docker; go run api.go' + command: /bin/sh -c 'cd /go/src/github.com/franela/play-with-docker; go run -race api.go' ports: # app exposes port 3000 - "3000:3000" diff --git a/services/check_swarm_status_task.go b/services/check_swarm_status_task.go index a892d18..ede3da0 100644 --- a/services/check_swarm_status_task.go +++ b/services/check_swarm_status_task.go @@ -9,6 +9,8 @@ func (c checkSwarmStatusTask) Run(i *Instance) { if info, err := GetDaemonInfo(i); err == nil { if info.Swarm.LocalNodeState != swarm.LocalNodeStateInactive && info.Swarm.LocalNodeState != swarm.LocalNodeStateLocked { i.IsManager = &info.Swarm.ControlAvailable + } else { + i.IsManager = nil } } diff --git a/services/collect_stats_task.go b/services/collect_stats_task.go index 13b7d43..330edcf 100644 --- a/services/collect_stats_task.go +++ b/services/collect_stats_task.go @@ -13,7 +13,6 @@ type collectStatsTask struct { mem float64 memLimit float64 memPercent float64 - v *types.StatsJSON cpuPercent float64 previousCPU uint64 @@ -27,24 +26,25 @@ func (c collectStatsTask) Run(i *Instance) { return } dec := json.NewDecoder(reader) - e := dec.Decode(&c.v) + var v *types.StatsJSON + e := dec.Decode(&v) if e != nil { log.Println("Error while trying to collect instance stats", e) return } // Memory - if c.v.MemoryStats.Limit != 0 { - c.memPercent = float64(c.v.MemoryStats.Usage) / float64(c.v.MemoryStats.Limit) * 100.0 + if v.MemoryStats.Limit != 0 { + c.memPercent = float64(v.MemoryStats.Usage) / float64(v.MemoryStats.Limit) * 100.0 } - c.mem = float64(c.v.MemoryStats.Usage) - c.memLimit = float64(c.v.MemoryStats.Limit) + c.mem = float64(v.MemoryStats.Usage) + c.memLimit = float64(v.MemoryStats.Limit) i.Mem = fmt.Sprintf("%.2f%% (%s / %s)", c.memPercent, units.BytesSize(c.mem), units.BytesSize(c.memLimit)) // cpu - c.previousCPU = c.v.PreCPUStats.CPUUsage.TotalUsage - c.previousSystem = c.v.PreCPUStats.SystemUsage - c.cpuPercent = calculateCPUPercentUnix(c.previousCPU, c.previousSystem, c.v) + c.previousCPU = v.PreCPUStats.CPUUsage.TotalUsage + c.previousSystem = v.PreCPUStats.SystemUsage + c.cpuPercent = calculateCPUPercentUnix(c.previousCPU, c.previousSystem, v) i.Cpu = fmt.Sprintf("%.2f%%", c.cpuPercent) } diff --git a/services/session.go b/services/session.go index 49f3e24..de9eea3 100644 --- a/services/session.go +++ b/services/session.go @@ -66,7 +66,8 @@ func (s *Session) SchedulePeriodicTasks() { for range s.ticker.C { var wg = sync.WaitGroup{} wg.Add(len(s.Instances)) - for _, i := range s.Instances { + for _, ins := range s.Instances { + var i *Instance = ins if i.dockerClient == nil { // Need to create client to the DinD docker daemon