Skip to content

Commit

Permalink
#224 Subscribe to only those events that we handling
Browse files Browse the repository at this point in the history
  • Loading branch information
stokito committed Nov 27, 2020
1 parent 87b6ba3 commit cd6618c
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions connector/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/bcicen/ctop/connector/manager"
"github.com/bcicen/ctop/container"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/client"
"github.com/docker/go-connections/nat"
Expand Down Expand Up @@ -83,20 +84,22 @@ func (cm *Docker) Wait() struct{} { return <-cm.closed }
func (cm *Docker) watchEvents() {
log.Info("docker event listener starting")
ctx := context.Background()

eventsOpts := types.EventsOptions{}
filter := filters.NewArgs()
filter.Add("type", "container")
filter.Add("event", "health_status")
filter.Add("event", "create")
filter.Add("event", "destroy")
filter.Add("event", "start")
filter.Add("event", "die")
filter.Add("event", "stop")
filter.Add("event", "pause")
filter.Add("event", "unpause")

eventsOpts := types.EventsOptions{Filters: filter}
events, _ := cm.client.Events(ctx, eventsOpts)

for e := range events {
if e.Type != "container" {
continue
}

actionName := e.Action
// fast skip all exec_* events: exec_create, exec_start, exec_die
if strings.HasPrefix(actionName, "exec_") {
continue
}
// Action may have additional param i.e. "health_status: healthy"
// We need to strip to have only action name
sepIdx := strings.Index(actionName, ": ")
Expand Down

0 comments on commit cd6618c

Please sign in to comment.