// wait waits for an individual service state to reach its desired state func wait(shutdown <-chan interface{}, conn client.Connection, serviceID, stateID string, dstate service.DesiredState) error { for { var node ServiceStateNode event, err := conn.GetW(servicepath(serviceID, stateID), &node) if err == client.ErrNoNode { // if the node no longer exists, then there is nothing to watch, so we are done return nil } else if err != nil { glog.Errorf("Got an error while looking for %s (%s): %s", stateID, serviceID, err) return err } switch dstate { case service.SVCStop: // pass through, because the node needs to be deleted to be considered Stopped case service.SVCRun, service.SVCRestart: if node.IsRunning() { // instance reached the desired state return nil } case service.SVCPause: if node.IsPaused() { // instance reached the desired state return nil } } // wait for something to change on the node or shutdown select { case <-event: case <-shutdown: return zzk.ErrShutdown } } }
func (r *registryType) watchItem(conn client.Connection, path string, nodeType client.Node, cancel <-chan bool, processNode func(conn client.Connection, node client.Node), errorHandler WatchError) error { exists, err := zzk.PathExists(conn, path) if err != nil { return err } if !exists { return client.ErrNoNode } for { event, err := conn.GetW(path, nodeType) if err != nil { glog.Errorf("Could not watch %s: %s", path, err) defer errorHandler(path, err) return err } processNode(conn, nodeType) //This blocks until a change happens under the key select { case ev := <-event: glog.V(2).Infof("watch event %+v at path: %s", ev, path) case <-cancel: return nil } } return nil }
func MonitorResourcePool(shutdown <-chan interface{}, conn client.Connection, poolID string) <-chan *pool.ResourcePool { monitor := make(chan *pool.ResourcePool) go func() { defer close(monitor) if err := zzk.Ready(shutdown, conn, poolpath(poolID)); err != nil { glog.V(2).Infof("Could not watch pool %s: %s", poolID, err) return } for { var node PoolNode event, err := conn.GetW(poolpath(poolID), &node) if err != nil { glog.V(2).Infof("Could not get pool %s: %s", poolID, err) return } select { case monitor <- node.ResourcePool: case <-shutdown: return } select { case <-event: case <-shutdown: return } } }() return monitor }