@@ -18,6 +18,7 @@ import (
18
18
"github.com/docker/docker/api/types/events"
19
19
"github.com/docker/docker/api/types/filters"
20
20
"github.com/pkg/errors"
21
+ "github.com/sirupsen/logrus"
21
22
"github.com/spf13/cobra"
22
23
)
23
24
@@ -129,9 +130,9 @@ func RunStats(ctx context.Context, dockerCLI command.Cli, options *StatsOptions)
129
130
return err
130
131
}
131
132
132
- eh := command . InitEventHandler ()
133
+ eh := newEventHandler ()
133
134
if options .All {
134
- eh .Handle (events .ActionCreate , func (e events.Message ) {
135
+ eh .setHandler (events .ActionCreate , func (e events.Message ) {
135
136
s := NewStats (e .Actor .ID [:12 ])
136
137
if cStats .add (s ) {
137
138
waitFirst .Add (1 )
@@ -140,7 +141,7 @@ func RunStats(ctx context.Context, dockerCLI command.Cli, options *StatsOptions)
140
141
})
141
142
}
142
143
143
- eh .Handle (events .ActionStart , func (e events.Message ) {
144
+ eh .setHandler (events .ActionStart , func (e events.Message ) {
144
145
s := NewStats (e .Actor .ID [:12 ])
145
146
if cStats .add (s ) {
146
147
waitFirst .Add (1 )
@@ -149,7 +150,7 @@ func RunStats(ctx context.Context, dockerCLI command.Cli, options *StatsOptions)
149
150
})
150
151
151
152
if ! options .All {
152
- eh .Handle (events .ActionDie , func (e events.Message ) {
153
+ eh .setHandler (events .ActionDie , func (e events.Message ) {
153
154
cStats .remove (e .Actor .ID [:12 ])
154
155
})
155
156
}
@@ -186,7 +187,7 @@ func RunStats(ctx context.Context, dockerCLI command.Cli, options *StatsOptions)
186
187
}
187
188
188
189
eventChan := make (chan events.Message )
189
- go eh .Watch (eventChan )
190
+ go eh .watch (eventChan )
190
191
stopped := make (chan struct {})
191
192
go monitorContainerEvents (started , eventChan , stopped )
192
193
defer close (stopped )
@@ -313,3 +314,31 @@ func RunStats(ctx context.Context, dockerCLI command.Cli, options *StatsOptions)
313
314
}
314
315
return err
315
316
}
317
+
318
+ // newEventHandler initializes and returns an eventHandler
319
+ func newEventHandler () * eventHandler {
320
+ return & eventHandler {handlers : make (map [events.Action ]func (events.Message ))}
321
+ }
322
+
323
+ // eventHandler allows for registering specific events to setHandler.
324
+ type eventHandler struct {
325
+ handlers map [events.Action ]func (events.Message )
326
+ }
327
+
328
+ func (eh * eventHandler ) setHandler (action events.Action , handler func (events.Message )) {
329
+ eh .handlers [action ] = handler
330
+ }
331
+
332
+ // watch ranges over the passed in event chan and processes the events based on the
333
+ // handlers created for a given action.
334
+ // To stop watching, close the event chan.
335
+ func (eh * eventHandler ) watch (c <- chan events.Message ) {
336
+ for e := range c {
337
+ h , exists := eh .handlers [e .Action ]
338
+ if ! exists {
339
+ continue
340
+ }
341
+ logrus .Debugf ("event handler: received event: %v" , e )
342
+ go h (e )
343
+ }
344
+ }
0 commit comments