Skip to content

Commit df95474

Browse files
committed
Fixes issue with stats on start event
In situations where a client is called like `docker stats` with no arguments or flags, if a container which was already created but not started yet is then subsequently started it will not be added to the stats list as expected. Also splits some of the stats helpers to a separate file from the stats CLI which is already quite long. Signed-off-by: Brian Goff <cpuguy83@gmail.com>
1 parent eb22fcc commit df95474

File tree

3 files changed

+267
-233
lines changed

3 files changed

+267
-233
lines changed

api/client/events.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ import (
66
"io"
77
"sort"
88
"strings"
9+
"sync"
910
"time"
1011

1112
"golang.org/x/net/context"
1213

14+
"github.com/Sirupsen/logrus"
1315
Cli "github.com/docker/docker/cli"
1416
"github.com/docker/docker/opts"
1517
"github.com/docker/docker/pkg/jsonlog"
@@ -115,3 +117,31 @@ func printOutput(event eventtypes.Message, output io.Writer) {
115117
}
116118
fmt.Fprint(output, "\n")
117119
}
120+
121+
type eventHandler struct {
122+
handlers map[string]func(eventtypes.Message)
123+
mu sync.Mutex
124+
closed bool
125+
}
126+
127+
func (w *eventHandler) Handle(action string, h func(eventtypes.Message)) {
128+
w.mu.Lock()
129+
w.handlers[action] = h
130+
w.mu.Unlock()
131+
}
132+
133+
// Watch ranges over the passed in event chan and processes the events based on the
134+
// handlers created for a given action.
135+
// To stop watching, close the event chan.
136+
func (w *eventHandler) Watch(c <-chan eventtypes.Message) {
137+
for e := range c {
138+
w.mu.Lock()
139+
h, exists := w.handlers[e.Action]
140+
w.mu.Unlock()
141+
if !exists {
142+
continue
143+
}
144+
logrus.Debugf("event handler: received event: %v", e)
145+
go h(e)
146+
}
147+
}

0 commit comments

Comments
 (0)