示例#1
0
文件: util.go 项目: rdterner/donut
func serializeCreate(zk *zookeeper.Conn, path string, data map[string]interface{}) (err error) {
	var e []byte
	if e, err = json.Marshal(data); err != nil {
		return
	}
	_, err = zk.Create(path, string(e), 0, zookeeper.WorldACL(zookeeper.PERM_ALL))
	return
}
示例#2
0
文件: util.go 项目: rdterner/donut
func getDeserialize(zk *zookeeper.Conn, path string) (data map[string]interface{}, err error) {
	var e string
	e, _, err = zk.Get(path)
	if err != nil {
		log.Printf("error on get in getDeserialize for %s: %v", path, err)
		return
	}
	err = json.Unmarshal([]byte(e), &data)
	return
}
示例#3
0
文件: util.go 项目: dforsyth/waffle
// XXX pulled this out of donut, maybe i should make a zk util lib?
// Watch the children at path until a byte is sent on the returned channel
// Uses the SafeMap more like a set, so you'll have to use Contains() for entries
func watchZKChildren(zk *zookeeper.Conn, path string, children *donut.SafeMap, onChange func(*donut.SafeMap)) (chan byte, error) {
	initial, _, watch, err := zk.ChildrenW(path)
	if err != nil {
		return nil, err
	}
	m := children.RangeLock()
	for _, node := range initial {
		m[node] = nil
	}
	children.RangeUnlock()
	kill := make(chan byte, 1)
	log.Printf("watching "+path+" len is %d", children.Len())
	go func() {
		defer close(kill)
		var nodes []string
		var err error
		for {
			select {
			case <-kill:
				// close(watch)
				log.Printf("got kill")
				return
			case event := <-watch:
				if !event.Ok() {
					continue
				}
				// close(watch)
				nodes, _, watch, err = zk.ChildrenW(path)
				if err != nil {
					log.Printf("Error in watchZkChildren: %v", err)
					// XXX I should really provide some way for the client to find out about this error...
					return
				}
				m := children.RangeLock()
				// mark all dead
				for k := range m {
					m[k] = 0
				}
				for _, node := range nodes {
					m[node] = 1
				}
				for k, v := range m {
					if v.(int) == 0 {
						delete(m, k)
					}
				}
				children.RangeUnlock()
				onChange(children)
			}
		}
	}()
	log.Printf("watcher setup on %s", path)
	return kill, nil
}
示例#4
0
文件: util.go 项目: rdterner/donut
// Remove a task from a cluster
func CompleteTask(clusterName string, zk *zookeeper.Conn, taskId string) {
	p := path.Join("/", clusterName, "tasks", taskId)
	zk.Delete(p, -1)
	log.Printf("Deleted task %s (%s)", taskId, p)
}