Beispiel #1
0
// watcher for data change in etcd directory
func (ns *numbers) watcher() {
	client := ns.client_pool.Get().(*etcd.Client)
	defer func() {
		ns.client_pool.Put(client)
	}()

	for {
		ch := make(chan *etcd.Response, 10)
		go func() {
			for {
				if resp, ok := <-ch; ok {
					if !resp.Node.Dir {
						ns.parse(resp.Node.Key, resp.Node.Value)
						log.Trace("csv change:", resp.Node.Key)
					}
				} else {
					return
				}
			}
		}()

		_, err := client.Watch(DEFAULT_NUMBERS_PATH, 0, true, ch, nil)
		if err != nil {
			log.Critical(err)
		}
		<-time.After(RETRY_DELAY)
	}
}
Beispiel #2
0
// load all csv(s) from a given directory in etcd
func (ns *numbers) load(directory string) {
	client := ns.client_pool.Get().(*etcd.Client)
	defer func() {
		ns.client_pool.Put(client)
	}()

	// get the keys under directory
	log.Info("loading numbers from:", directory)
	resp, err := client.Get(directory, true, false)
	if err != nil {
		log.Error(err)
		return
	}

	// validation check
	if !resp.Node.Dir {
		log.Error("not a directory")
		return
	}

	// read all csv(s) from this dirctory
	ns.tables = make(map[string]*table)
	count := 0
	for _, node := range resp.Node.Nodes {
		if !node.Dir {
			log.Trace("loading:", node.Key)
			ns.parse(node.Key, node.Value)
			count++
		} else {
			log.Warning("not a file:", node.Key)
		}
	}

	log.Finef("%v csv(s) loaded", count)
}