func createJobInOrder(ctx context.Context, kapi client.KeysAPI, j *job) error { errChan := make(chan error) done := make(chan struct{}) go func() { buf := new(bytes.Buffer) if err := json.NewEncoder(buf).Encode(j); err != nil { errChan <- err return } value := buf.String() fmt.Println("createJobInOrder:", value) opts := &client.CreateInOrderOptions{} if _, err := kapi.CreateInOrder(ctx, dirName, value, opts); err != nil { errChan <- err return } done <- struct{}{} }() select { case <-done: return nil case v := <-errChan: return v case <-ctx.Done(): return ctx.Err() } }
// mkCommandFunc executes the "mk" command. func mkCommandFunc(c *cli.Context, ki client.KeysAPI) { if len(c.Args()) == 0 { handleError(ExitBadArgs, errors.New("key required")) } key := c.Args()[0] value, err := argOrStdin(c.Args(), os.Stdin, 1) if err != nil { handleError(ExitBadArgs, errors.New("value required")) } ttl := c.Int("ttl") inorder := c.Bool("in-order") var resp *client.Response ctx, cancel := contextWithTotalTimeout(c) if !inorder { // Since PrevNoExist means that the Node must not exist previously, // this Set method always creates a new key. Therefore, mk command // succeeds only if the key did not previously exist, and the command // prevents one from overwriting values accidentally. resp, err = ki.Set(ctx, key, value, &client.SetOptions{TTL: time.Duration(ttl) * time.Second, PrevExist: client.PrevNoExist}) } else { // If in-order flag is specified then create an inorder key under // the directory identified by the key argument. resp, err = ki.CreateInOrder(ctx, key, value, &client.CreateInOrderOptions{TTL: time.Duration(ttl) * time.Second}) } cancel() if err != nil { handleError(ExitServerError, err) } printResponseKey(resp, c.GlobalString("output")) }
func doFillQueue(kapi client.KeysAPI) { var key = queuebase + *queuename list := rand.Perm(10) for _, v := range list { value := "Value" + strconv.Itoa(v) resp, err := kapi.CreateInOrder(context.TODO(), key, value, nil) if err != nil { log.Fatal(err) } fmt.Println(resp.Action + " " + resp.Node.Key + " to " + resp.Node.Value) } }