func GetIndex(client *etcd.Client, basedir string, jobname string, ip string) (int, error) { jobdir := "/jobs" + "/" + jobname response, err := client.Get(jobdir, true, true) if err == nil { for i := 0; i < response.Node.Nodes.Len(); i++ { if response.Node.Nodes[i].Value == ip { return i, nil } } } response, err = client.AddChild(jobdir, ip, 0) if err != nil { fmt.Printf("use etcd to get index error: %v\n", err) return 0, err } mykey := response.Node.Key response, err = client.Get(jobdir, true, true) if err != nil { fmt.Printf("get etcd jobdir error: %v\n", err) return 0, err } for i := 0; i < response.Node.Nodes.Len(); i++ { if response.Node.Nodes[i].Key == mykey { return i, nil } } // this line would never reach. return 0, errors.New("etcd add child error!") }
func addLockDirChild(client *etcd.Client, key string) (*etcd.Response, error) { hostname, err := os.Hostname() if err != nil { return nil, errgo.Notef(err, "fail to get hostname") } client.SyncCluster() return client.AddChild(key, hostname, 0) }