mirror of
https://github.com/openfaas/faas.git
synced 2025-06-19 04:26:35 +00:00
Add /_/health
endpoint to watchdog
Introduce new endpoint `/_/health` to watchdog for health status of functions which check for `/tmp/.lock` file Fixes first part of #547 issue. Signed-off-by: Vivek Singh <vivekkmr45@yahoo.in>
This commit is contained in:
@ -252,6 +252,48 @@ func getAdditionalEnvs(config *WatchdogConfig, r *http.Request, method string) [
|
||||
return envs
|
||||
}
|
||||
|
||||
func lockFilePresent() bool {
|
||||
path := filepath.Join(os.TempDir(), ".lock")
|
||||
if _, err := os.Stat(path); os.IsNotExist(err) {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func createLockFile() error {
|
||||
path := filepath.Join(os.TempDir(), ".lock")
|
||||
log.Printf("Writing lock-file to: %s\n", path)
|
||||
writeErr := ioutil.WriteFile(path, []byte{}, 0660)
|
||||
return writeErr
|
||||
}
|
||||
|
||||
func removeLockFile() error {
|
||||
path := filepath.Join(os.TempDir(), ".lock")
|
||||
log.Printf("Removing lock-file : %s\n", path)
|
||||
removeErr := os.Remove(path)
|
||||
return removeErr
|
||||
}
|
||||
|
||||
func makeHealthHandler() func(http.ResponseWriter, *http.Request) {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.Method {
|
||||
case "GET":
|
||||
if lockFilePresent() == false {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte("OK"))
|
||||
break
|
||||
default:
|
||||
w.WriteHeader(http.StatusMethodNotAllowed)
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
func makeRequestHandler(config *WatchdogConfig) func(http.ResponseWriter, *http.Request) {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.Method {
|
||||
@ -290,6 +332,7 @@ func main() {
|
||||
MaxHeaderBytes: 1 << 20, // Max header of 1MB
|
||||
}
|
||||
|
||||
http.HandleFunc("/_/health", makeHealthHandler())
|
||||
http.HandleFunc("/", makeRequestHandler(&config))
|
||||
|
||||
if config.suppressLock == false {
|
||||
|
Reference in New Issue
Block a user