Merge pull request #2072 from andyxning/fix_filechecker_in_health

fix filechecker in health with precondition check
This commit is contained in:
Derek McGowan 2016-12-09 09:51:46 -08:00 committed by GitHub
commit 923c7763b0
2 changed files with 19 additions and 3 deletions

View File

@ -2,9 +2,11 @@ package checks
import ( import (
"errors" "errors"
"fmt"
"net" "net"
"net/http" "net/http"
"os" "os"
"path/filepath"
"strconv" "strconv"
"time" "time"
@ -15,10 +17,19 @@ import (
// if the file exists. // if the file exists.
func FileChecker(f string) health.Checker { func FileChecker(f string) health.Checker {
return health.CheckFunc(func() error { return health.CheckFunc(func() error {
if _, err := os.Stat(f); err == nil { absoluteFilePath, err := filepath.Abs(f)
return errors.New("file exists") if err != nil {
return fmt.Errorf("failed to get absolute path for %q: %v", f, err)
} }
_, err = os.Stat(absoluteFilePath)
if err == nil {
return errors.New("file exists")
} else if os.IsNotExist(err) {
return nil return nil
}
return err
}) })
} }

View File

@ -122,6 +122,11 @@
// # curl localhost:5001/debug/health // # curl localhost:5001/debug/health
// {"fileChecker":"file exists"} // {"fileChecker":"file exists"}
// //
// FileChecker only accepts absolute or relative file path. It does not work properly with tilde(~).
// You should make sure that the application has proper permission(read and execute permission
// for directory along with the specified file path). Otherwise, the FileChecker will report error
// and file health check is not ok.
//
// You could also test the connectivity to a downstream service by using a // You could also test the connectivity to a downstream service by using a
// "HTTPChecker", but ensure that you only mark the test unhealthy if there // "HTTPChecker", but ensure that you only mark the test unhealthy if there
// are a minimum of two failures in a row: // are a minimum of two failures in a row: