/
health.go
54 lines (46 loc) · 1.22 KB
/
health.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
package jenkins
import (
"fmt"
"github.com/jenkins-x/jx/pkg/log"
"net/http"
"time"
"github.com/jenkins-x/jx/pkg/util"
"github.com/pkg/errors"
)
const (
healthTimeout = 2 * time.Minute
pingTimeout = 2 * time.Second
)
// CheckHealth checks the health of Jenkins server using the login URL
func CheckHealth(url string, healthTimeout time.Duration) error {
endpoint := JenkinsLoginURL(url)
if ping(endpoint) == nil {
return nil
}
log.Infof("waiting up to %s for the Jenkins server to be healty at URL %s\n", util.ColorInfo(healthTimeout.String()), util.ColorInfo(endpoint))
err := util.Retry(healthTimeout, func() error {
return ping(endpoint)
})
if err != nil {
return errors.Wrapf(err, "pinging Jenkins server %q", endpoint)
}
return nil
}
func ping(url string) error {
client := http.Client{
Timeout: pingTimeout,
}
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return errors.Wrapf(err, "building ping request for URL %q", url)
}
resp, err := client.Do(req)
if err != nil {
return errors.Wrapf(err, "executing ping request against URL %q", url)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("ping status: %s", resp.Status)
}
return nil
}