1
0
mirror of https://github.com/bingohuang/docker-labs.git synced 2025-07-14 01:57:32 +08:00

Omit race conditions and update swarm info events

This commit is contained in:
Marcos Lilljedahl 2016-12-02 10:29:41 -03:00
parent 84da622775
commit a7811b4e33
4 changed files with 15 additions and 12 deletions

View File

@ -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"

View File

@ -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
}
}

View File

@ -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)
}

View File

@ -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