Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions backend/controllers/healthcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,49 @@ package controllers
import (
"encoding/json"
"net/http"
"os/exec"
"regexp"
"strconv"
)

func HealthCheckHandler(w http.ResponseWriter, r *http.Request) {
// if it get any other request other than get then it will show error
if r.Method != http.MethodGet {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
// Default state is healthy
status := "healthy"
statusCode := http.StatusOK

// checks the taskwarrior version and if the command fails will give 503 (dependency missing)
cmd := exec.Command("task", "--version")
output, err := cmd.Output()
if err != nil {
status = "unhealthy: taskwarrior not found or failed to execute"
statusCode = http.StatusServiceUnavailable
} else {
re := regexp.MustCompile(`(\d+)\.(\d+)`)
matches := re.FindStringSubmatch(string(output))

if len(matches) < 3 {
status = "unhealthy: unable to determine taskwarrior version"
statusCode = http.StatusServiceUnavailable
} else {
// check the taskwarrior version (major version should be >= 3 )
major, _ := strconv.Atoi(matches[1])

if major < 3 {
status = "unhealthy: unsupported taskwarrior version (>= 3.0 required)"
statusCode = http.StatusServiceUnavailable
}
}
}

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.WriteHeader(statusCode)
json.NewEncoder(w).Encode(map[string]string{
"status": "healthy",
"status": status,
"service": "ccsync-backend",
})
}
Loading