forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 1
/
wrapper.go
46 lines (39 loc) · 1.41 KB
/
wrapper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// Copyright 2015, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package vtworkerclient
import (
"errors"
"fmt"
"time"
"github.com/youtube/vitess/go/vt/logutil"
"golang.org/x/net/context"
)
// RunCommandAndWait executes a single command on a given vtworker and blocks until the command did return or timed out.
// Output from vtworker is streamed as logutil.LoggerEvent messages which have to be consumed by the caller who has to specify a "recv" function.
func RunCommandAndWait(ctx context.Context, server string, args []string, recv func(*logutil.LoggerEvent)) error {
if recv == nil {
return errors.New("No function closure for LoggerEvent stream specified")
}
// create the client
// TODO(mberlin): vtctlclient exposes dialTimeout as flag. If there are no use cases, remove it there as well to be consistent?
client, err := New(server, 30*time.Second /* dialTimeout */)
if err != nil {
return fmt.Errorf("Cannot dial to server %v: %v", server, err)
}
defer client.Close()
// run the command
c, errFunc, err := client.ExecuteVtworkerCommand(ctx, args)
if err != nil {
return fmt.Errorf("Cannot execute remote command: %v", err)
}
// stream the result
for e := range c {
recv(e)
}
// then display the overall error
if err = errFunc(); err != nil {
return fmt.Errorf("Remote error: %v", err)
}
return nil
}