2015-02-26 02:15:07 +00:00
|
|
|
package checks
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
2015-08-19 00:19:46 +00:00
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/docker/distribution/health"
|
2015-02-26 02:15:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// FileChecker checks the existence of a file and returns and error
|
|
|
|
// if the file exists, taking the application out of rotation
|
|
|
|
func FileChecker(f string) health.Checker {
|
|
|
|
return health.CheckFunc(func() error {
|
|
|
|
if _, err := os.Stat(f); err == nil {
|
|
|
|
return errors.New("file exists")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// HTTPChecker does a HEAD request and verifies if the HTTP status
|
|
|
|
// code return is a 200, taking the application out of rotation if
|
|
|
|
// otherwise
|
|
|
|
func HTTPChecker(r string) health.Checker {
|
|
|
|
return health.CheckFunc(func() error {
|
|
|
|
response, err := http.Head(r)
|
|
|
|
if err != nil {
|
|
|
|
return errors.New("error while checking: " + r)
|
|
|
|
}
|
|
|
|
if response.StatusCode != http.StatusOK {
|
2015-08-19 00:19:46 +00:00
|
|
|
return errors.New("downstream service returned unexpected status: " + strconv.Itoa(response.StatusCode))
|
2015-02-26 02:15:07 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|