Browse files

added retry logic to monit status call

Signed-off-by: Ben Smith <bsmith@pivotallabs.com>
  • Loading branch information...
1 parent 23bfb39 commit f49b5de24db355de554a126c50d0f5e9bf40486e @mjstallard mjstallard committed Jan 22, 2014
View
18 go_agent/src/bosh/jobsupervisor/monit/http_client.go
@@ -50,7 +50,7 @@ func (c httpClient) ServicesInGroup(name string) (services []string, err error)
}
func (c httpClient) StartService(serviceName string) (err error) {
- response, err := c.makeRequest(serviceName, "POST", "action=start")
+ response, err := c.makeRequest(c.monitUrl(serviceName), "POST", "action=start")
if err != nil {
err = bosherr.WrapError(err, "Sending start request to monit")
@@ -66,7 +66,7 @@ func (c httpClient) StartService(serviceName string) (err error) {
}
func (c httpClient) StopService(serviceName string) (err error) {
- response, err := c.makeRequest(serviceName, "POST", "action=stop")
+ response, err := c.makeRequest(c.monitUrl(serviceName), "POST", "action=stop")
if err != nil {
err = bosherr.WrapError(err, "Sending stop request to monit")
@@ -86,13 +86,10 @@ func (c httpClient) Status() (status Status, err error) {
}
func (c httpClient) status() (status status, err error) {
- endpoint := c.monitUrl("/_status2")
- endpoint.RawQuery = "format=xml"
- request, err := http.NewRequest("GET", endpoint.String(), nil)
- request.SetBasicAuth(c.username, c.password)
+ url := c.monitUrl("/_status2")
+ url.RawQuery = "format=xml"
- httpClient := http.DefaultClient
- response, err := httpClient.Do(request)
+ response, err := c.makeRequest(url, "GET", "")
if err != nil {
err = bosherr.WrapError(err, "Sending status request to monit")
return
@@ -138,9 +135,8 @@ func (c httpClient) validateResponse(response *http.Response) (err error) {
return
}
-func (c httpClient) makeRequest(serviceName, method, requestBody string) (response *http.Response, err error) {
- endpoint := c.monitUrl(serviceName)
- request, err := http.NewRequest(method, endpoint.String(), strings.NewReader(requestBody))
+func (c httpClient) makeRequest(url url.URL, method, requestBody string) (response *http.Response, err error) {
+ request, err := http.NewRequest(method, url.String(), strings.NewReader(requestBody))
request.SetBasicAuth(c.username, c.password)
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
response, err = c.client.Do(request)
View
28 go_agent/src/bosh/jobsupervisor/monit/http_client_test.go
@@ -170,3 +170,31 @@ func TestDecodeStatus(t *testing.T) {
assert.Equal(t, 1, status.Services.Services[0].Monitor)
assert.Equal(t, "dummy", status.Services.Services[0].Name)
}
+
+func TestStatusRetriesWhenNon200Response(t *testing.T) {
+ fakeHttpClient := http_fakes.NewFakeHttpClient()
+ fakeHttpClient.StatusCode = 500
+ fakeHttpClient.SetMessage("fake error message")
+
+ client := NewHttpClient("agent.example.com", "fake-user", "fake-pass", fakeHttpClient)
+ client.delayBetweenRetries = 1 * time.Millisecond
+
+ _, err := client.Status()
+ assert.Equal(t, fakeHttpClient.CallCount, 20)
+ assert.Error(t, err)
+ assert.Contains(t, err.Error(), "fake error message")
+}
+
+func TestStatusRetriesWhenConnectionRefused(t *testing.T) {
+ fakeHttpClient := http_fakes.NewFakeHttpClient()
+ fakeHttpClient.SetNilResponse()
+ fakeHttpClient.Error = errors.New("some error")
+
+ client := NewHttpClient("agent.example.com", "fake-user", "fake-pass", fakeHttpClient)
+ client.delayBetweenRetries = 1 * time.Millisecond
+
+ _, err := client.Status()
+ assert.Equal(t, fakeHttpClient.CallCount, 20)
+ assert.Error(t, err)
+ assert.Contains(t, err.Error(), "some error")
+}

0 comments on commit f49b5de

Please sign in to comment.