Exemple #1
0
Fichier : zk.go Projet : houcy/push
func GetNodes(conn *zk.Conn, path string) ([]string, error) {
	nodes, stat, err := conn.Children(path)
	if err != nil {
		if err == zk.ErrNoNode {
			return nil, ErrNodeNotExist
		}
		log.Errorf("zk.Children(\"%s\") error(%v)", path, err)
		return nil, err
	}
	if stat == nil {
		return nil, ErrNodeNotExist
	}
	if len(nodes) == 0 {
		return nil, ErrNoChild
	}
	return nodes, nil
}
Exemple #2
0
Fichier : zk.go Projet : houcy/push
func Register(conn *zk.Conn, fpath string, data []byte) error {
	_, err := conn.Create(fpath+"/", data, zk.FlagEphemeral|zk.FlagSequence, zk.WorldACL(zk.PermAll))
	if err != nil && err != zk.ErrNodeExists {
		return err
	}
	log.Infof("create zk node:%s", fpath)
	// watch self
	go func() {
		for {
			exist, _, watch, err := conn.ExistsW(fpath)
			if err != nil {
				log.Warnf("failed zk node \"%s\" set watch, [%v]", fpath, err)
				return
			}
			if !exist {
				log.Warnf("zk node \"%s\" not exist, [%v]", fpath, err)
				return
			}
			event := <-watch
			log.Infof("\"%s\" receive a event %v", fpath, event)
		}
	}()
	return nil
}