-
Notifications
You must be signed in to change notification settings - Fork 785
/
health.go
48 lines (41 loc) · 1011 Bytes
/
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
package jenkins
import (
"fmt"
"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) error {
healthEndpoin := JenkinsLoginURL(url)
err := util.Retry(healthTimeout, func() error {
return ping(healthEndpoin)
})
if err != nil {
return errors.Wrapf(err, "pinging Jenkins server %q", healthEndpoin)
}
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
}