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