github.com/kaisenlinux/docker.io@v0.0.0-20230510090727-ea55db55fac7/swarmkit/manager/state/watch.go (about)

     1  package state
     2  
     3  import (
     4  	"github.com/docker/go-events"
     5  	"github.com/docker/swarmkit/api"
     6  	"github.com/docker/swarmkit/watch"
     7  )
     8  
     9  // EventCommit delineates a transaction boundary.
    10  type EventCommit struct {
    11  	Version *api.Version
    12  }
    13  
    14  // Matches returns true if this event is a commit event.
    15  func (e EventCommit) Matches(watchEvent events.Event) bool {
    16  	_, ok := watchEvent.(EventCommit)
    17  	return ok
    18  }
    19  
    20  // TaskCheckStateGreaterThan is a TaskCheckFunc for checking task state.
    21  func TaskCheckStateGreaterThan(t1, t2 *api.Task) bool {
    22  	return t2.Status.State > t1.Status.State
    23  }
    24  
    25  // NodeCheckState is a NodeCheckFunc for matching node state.
    26  func NodeCheckState(n1, n2 *api.Node) bool {
    27  	return n1.Status.State == n2.Status.State
    28  }
    29  
    30  // Watch takes a variable number of events to match against. The subscriber
    31  // will receive events that match any of the arguments passed to Watch.
    32  //
    33  // Examples:
    34  //
    35  // // subscribe to all events
    36  // Watch(q)
    37  //
    38  // // subscribe to all UpdateTask events
    39  // Watch(q, EventUpdateTask{})
    40  //
    41  // // subscribe to all task-related events
    42  // Watch(q, EventUpdateTask{}, EventCreateTask{}, EventDeleteTask{})
    43  //
    44  // // subscribe to UpdateTask for node 123
    45  // Watch(q, EventUpdateTask{Task: &api.Task{NodeID: 123},
    46  //                         Checks: []TaskCheckFunc{TaskCheckNodeID}})
    47  //
    48  // // subscribe to UpdateTask for node 123, as well as CreateTask
    49  // // for node 123 that also has ServiceID set to "abc"
    50  // Watch(q, EventUpdateTask{Task: &api.Task{NodeID: 123},
    51  //                         Checks: []TaskCheckFunc{TaskCheckNodeID}},
    52  //         EventCreateTask{Task: &api.Task{NodeID: 123, ServiceID: "abc"},
    53  //                         Checks: []TaskCheckFunc{TaskCheckNodeID,
    54  //                                                 func(t1, t2 *api.Task) bool {
    55  //                                                         return t1.ServiceID == t2.ServiceID
    56  //                                                 }}})
    57  func Watch(queue *watch.Queue, specifiers ...api.Event) (eventq chan events.Event, cancel func()) {
    58  	if len(specifiers) == 0 {
    59  		return queue.Watch()
    60  	}
    61  	return queue.CallbackWatch(Matcher(specifiers...))
    62  }
    63  
    64  // Matcher returns an events.Matcher that Matches the specifiers with OR logic.
    65  func Matcher(specifiers ...api.Event) events.MatcherFunc {
    66  	return events.MatcherFunc(func(event events.Event) bool {
    67  		for _, s := range specifiers {
    68  			if s.Matches(event) {
    69  				return true
    70  			}
    71  		}
    72  		return false
    73  	})
    74  }