Skip to content

Commit 7660310

Browse files
[IMPROVEMENT] Improve health check to verify Taskwarrior availability. (#438)
* feat: make health check fail when task binary is missing * updating the PR applying the check for the version --------- Co-authored-by: GAURAV KARMAKAR <gaurav.k@graeon.ai>
1 parent 54bfa37 commit 7660310

1 file changed

Lines changed: 33 additions & 2 deletions

File tree

backend/controllers/healthcheck.go

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,49 @@ package controllers
33
import (
44
"encoding/json"
55
"net/http"
6+
"os/exec"
7+
"regexp"
8+
"strconv"
69
)
710

811
func HealthCheckHandler(w http.ResponseWriter, r *http.Request) {
12+
// if it get any other request other than get then it will show error
913
if r.Method != http.MethodGet {
1014
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
1115
return
1216
}
17+
// Default state is healthy
18+
status := "healthy"
19+
statusCode := http.StatusOK
20+
21+
// checks the taskwarrior version and if the command fails will give 503 (dependency missing)
22+
cmd := exec.Command("task", "--version")
23+
output, err := cmd.Output()
24+
if err != nil {
25+
status = "unhealthy: taskwarrior not found or failed to execute"
26+
statusCode = http.StatusServiceUnavailable
27+
} else {
28+
re := regexp.MustCompile(`(\d+)\.(\d+)`)
29+
matches := re.FindStringSubmatch(string(output))
30+
31+
if len(matches) < 3 {
32+
status = "unhealthy: unable to determine taskwarrior version"
33+
statusCode = http.StatusServiceUnavailable
34+
} else {
35+
// check the taskwarrior version (major version should be >= 3 )
36+
major, _ := strconv.Atoi(matches[1])
37+
38+
if major < 3 {
39+
status = "unhealthy: unsupported taskwarrior version (>= 3.0 required)"
40+
statusCode = http.StatusServiceUnavailable
41+
}
42+
}
43+
}
1344

1445
w.Header().Set("Content-Type", "application/json")
15-
w.WriteHeader(http.StatusOK)
46+
w.WriteHeader(statusCode)
1647
json.NewEncoder(w).Encode(map[string]string{
17-
"status": "healthy",
48+
"status": status,
1849
"service": "ccsync-backend",
1950
})
2051
}

0 commit comments

Comments
 (0)