forked from flynn/flynn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wait_action.go
66 lines (57 loc) · 1.23 KB
/
wait_action.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
package bootstrap
import (
"fmt"
"net/http"
"net/url"
"strconv"
"time"
"github.com/flynn/flynn/discoverd/client"
"github.com/flynn/flynn/discoverd/client/dialer"
)
type WaitAction struct {
URL string `json:"url"`
Status int `json:"status"`
}
func init() {
Register("wait", &WaitAction{})
}
func (a *WaitAction) Run(s *State) error {
const waitMax = time.Minute
const waitInterval = 500 * time.Millisecond
if a.Status == 0 {
a.Status = 200
}
u, err := url.Parse(a.URL)
if err != nil {
return err
}
httpc := http.DefaultClient
if u.Scheme == "discoverd+http" {
if err := discoverd.Connect(""); err != nil {
return err
}
d := dialer.New(discoverd.DefaultClient, nil)
defer d.Close()
httpc = &http.Client{Transport: &http.Transport{Dial: d.Dial}}
u.Scheme = "http"
}
start := time.Now()
for {
var result string
res, err := httpc.Get(u.String())
if err != nil {
result = fmt.Sprintf("%q", err)
goto fail
}
res.Body.Close()
if res.StatusCode == a.Status {
return nil
}
result = strconv.Itoa(res.StatusCode)
fail:
if time.Now().Sub(start) >= waitMax {
return fmt.Errorf("bootstrap: timed out waiting for %s, last response %s", a.URL, result)
}
time.Sleep(waitInterval)
}
}