Skip to content
This repository has been archived by the owner on Oct 23, 2024. It is now read-only.

Commit

Permalink
Making docker watch logic only trigger handler on real updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben Keith authored and keitwb committed Sep 5, 2018
1 parent 6b186f4 commit f76e175
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions internal/core/common/docker/containerlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,18 @@ func ListAndWatchContainers(ctx context.Context, client *docker.Client, changeHa
containers := make(map[string]*dtypes.ContainerJSON)

// Make sure you hold the lock before calling this
updateContainer := func(id string) {
updateContainer := func(id string) bool {
inspectCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
c, err := client.ContainerInspect(inspectCtx, id)
defer cancel()
if err != nil {
logger.WithError(err).Errorf("Could not inspect updated container %s", id)
} else if imageFilter == nil || !imageFilter.Matches(c.Config.Image) {
logger.Debugf("Updated Docker container %s", id)
containers[id] = &c
return true
}
cancel()
return false
}

watchStarted := make(chan struct{})
Expand Down Expand Up @@ -78,12 +80,15 @@ func ListAndWatchContainers(ctx context.Context, client *docker.Client, changeHa
// be unbounded.
case "destroy":
logger.Debugf("Docker container was destroyed: %s", event.ID)
delete(containers, event.ID)
changeHandler(containers[event.ID], nil)
if _, ok := containers[event.ID]; ok {
delete(containers, event.ID)
changeHandler(containers[event.ID], nil)
}
default:
oldContainer := containers[event.ID]
updateContainer(event.ID)
changeHandler(oldContainer, containers[event.ID])
if updateContainer(event.ID) {
changeHandler(oldContainer, containers[event.ID])
}
}

lock.Unlock()
Expand Down

0 comments on commit f76e175

Please sign in to comment.