Skip to content

Commit

Permalink
Merge pull request #4936 from thaJeztah/deprecate_eventhandler
Browse files Browse the repository at this point in the history
cli/command: deprecate EventHandler and InitEventHandler
  • Loading branch information
thaJeztah committed Mar 11, 2024
2 parents d06f137 + 46afd26 commit a2f3f40
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
39 changes: 34 additions & 5 deletions cli/command/container/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/docker/docker/api/types/events"
"github.com/docker/docker/api/types/filters"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -129,9 +130,9 @@ func RunStats(ctx context.Context, dockerCLI command.Cli, options *StatsOptions)
return err
}

eh := command.InitEventHandler()
eh := newEventHandler()
if options.All {
eh.Handle(events.ActionCreate, func(e events.Message) {
eh.setHandler(events.ActionCreate, func(e events.Message) {
s := NewStats(e.Actor.ID[:12])
if cStats.add(s) {
waitFirst.Add(1)
Expand All @@ -140,7 +141,7 @@ func RunStats(ctx context.Context, dockerCLI command.Cli, options *StatsOptions)
})
}

eh.Handle(events.ActionStart, func(e events.Message) {
eh.setHandler(events.ActionStart, func(e events.Message) {
s := NewStats(e.Actor.ID[:12])
if cStats.add(s) {
waitFirst.Add(1)
Expand All @@ -149,7 +150,7 @@ func RunStats(ctx context.Context, dockerCLI command.Cli, options *StatsOptions)
})

if !options.All {
eh.Handle(events.ActionDie, func(e events.Message) {
eh.setHandler(events.ActionDie, func(e events.Message) {
cStats.remove(e.Actor.ID[:12])
})
}
Expand Down Expand Up @@ -186,7 +187,7 @@ func RunStats(ctx context.Context, dockerCLI command.Cli, options *StatsOptions)
}

eventChan := make(chan events.Message)
go eh.Watch(eventChan)
go eh.watch(eventChan)
stopped := make(chan struct{})
go monitorContainerEvents(started, eventChan, stopped)
defer close(stopped)
Expand Down Expand Up @@ -313,3 +314,31 @@ func RunStats(ctx context.Context, dockerCLI command.Cli, options *StatsOptions)
}
return err
}

// newEventHandler initializes and returns an eventHandler
func newEventHandler() *eventHandler {
return &eventHandler{handlers: make(map[events.Action]func(events.Message))}
}

// eventHandler allows for registering specific events to setHandler.
type eventHandler struct {
handlers map[events.Action]func(events.Message)
}

func (eh *eventHandler) setHandler(action events.Action, handler func(events.Message)) {
eh.handlers[action] = handler
}

// watch ranges over the passed in event chan and processes the events based on the
// handlers created for a given action.
// To stop watching, close the event chan.
func (eh *eventHandler) watch(c <-chan events.Message) {
for e := range c {
h, exists := eh.handlers[e.Action]
if !exists {
continue
}
logrus.Debugf("event handler: received event: %v", e)
go h(e)
}
}
4 changes: 4 additions & 0 deletions cli/command/events_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@ import (

// EventHandler is abstract interface for user to customize
// own handle functions of each type of events
//
// Deprecated: EventHandler is no longer used, and will be removed in the next release.
type EventHandler interface {
Handle(action events.Action, h func(events.Message))
Watch(c <-chan events.Message)
}

// InitEventHandler initializes and returns an EventHandler
//
// Deprecated: InitEventHandler is no longer used, and will be removed in the next release.
func InitEventHandler() EventHandler {
return &eventHandler{handlers: make(map[events.Action]func(events.Message))}
}
Expand Down

0 comments on commit a2f3f40

Please sign in to comment.