/
cmd_wait.go
137 lines (114 loc) · 3.05 KB
/
cmd_wait.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package client
import (
"fmt"
"github.com/keybase/cli"
"github.com/keybase/client/go/libcmdline"
"github.com/keybase/client/go/libkb"
"github.com/keybase/client/go/protocol/keybase1"
"github.com/keybase/go-framed-msgpack-rpc/rpc"
"golang.org/x/net/context"
"time"
)
type CmdWait struct {
libkb.Contextified
duration time.Duration
includeKBFS bool
}
const maxWaitTime = 60 * time.Second
func NewCmdWait(cl *libcmdline.CommandLine, g *libkb.GlobalContext) cli.Command {
return cli.Command{
Name: "wait",
Usage: "Waits for the keybase service to start up",
Action: func(c *cli.Context) {
cl.SetForkCmd(libcmdline.NoFork)
cl.SetLogForward(libcmdline.LogForwardNone)
cl.ChooseCommand(&CmdWait{Contextified: libkb.NewContextified(g)}, "wait", c)
},
Flags: []cli.Flag{
cli.DurationFlag{
Name: "duration, d",
Value: 10 * time.Second,
Usage: "How long to wait before timing out",
},
cli.BoolFlag{
Name: "include-kbfs",
Usage: "Wait on kbfs to start for an additional same amount of time",
},
},
}
}
func (c *CmdWait) ParseArgv(ctx *cli.Context) error {
if len(ctx.Args()) > 0 {
return UnexpectedArgsError("wait")
}
c.duration = ctx.Duration("duration")
if c.duration.Seconds() <= 0 || c.duration.Seconds() > maxWaitTime.Seconds() {
return fmt.Errorf("invalid duration %s, must be between 1s and %s", c.duration, maxWaitTime)
}
c.includeKBFS = ctx.Bool("include-kbfs")
return nil
}
func (c *CmdWait) Run() error {
ctx, cancel := context.WithTimeout(context.Background(), c.duration)
defer cancel()
err := checkIsRunning(ctx, c.G(), false)
if err != nil {
return fmt.Errorf("service failed to startup: %v", err)
}
ctx, cancel = context.WithTimeout(context.Background(), c.duration)
defer cancel()
if c.includeKBFS {
err := checkIsRunning(ctx, c.G(), true)
if err != nil {
return fmt.Errorf("kbfs failed to startup: %v", err)
}
}
return nil
}
func (c *CmdWait) GetUsage() libkb.Usage {
return libkb.Usage{}
}
func getConfigClientWithRetry(g *libkb.GlobalContext) (cli keybase1.ConfigClient, err error) {
var rpcClient *rpc.Client
if rpcClient, _, err = getRPCClientWithContextWithRetry(g); err == nil {
cli = keybase1.ConfigClient{Cli: rpcClient}
}
return
}
func getRPCClientWithContextWithRetry(g *libkb.GlobalContext) (ret *rpc.Client, xp rpc.Transporter, err error) {
if xp, err = getSocketWithRetry(g); err == nil {
ret = rpc.NewClient(xp, libkb.NewContextifiedErrorUnwrapper(g), nil)
}
return
}
func checkIsRunning(ctx context.Context, g *libkb.GlobalContext, kbfs bool) error {
for {
client, err := getConfigClientWithRetry(g)
if err != nil {
select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(1 * time.Second):
}
continue
}
var isRunning bool
if kbfs {
isRunning, err = client.IsKBFSRunning(ctx, 0)
} else {
isRunning, err = client.IsServiceRunning(ctx, 0)
}
if err != nil {
return err
}
if isRunning {
break
}
select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(1 * time.Second):
}
}
return nil
}