Skip to content

Commit

Permalink
feat: Add health check endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
ivov committed Nov 22, 2024
1 parent 671513b commit 122c23d
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ export N8N_RUNNERS_AUTH_TOKEN=...
make run
```

## Health check

The launcher exposes a health check endpoint at `/healthz` on port `5681` for liveness checks.

## Release

1. Create a git tag following semver:
Expand Down
11 changes: 11 additions & 0 deletions cmd/launcher/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package main

import (
"flag"
"fmt"
"os"

"task-runner-launcher/internal/commands"
"task-runner-launcher/internal/http"
"task-runner-launcher/internal/logs"
)

Expand All @@ -20,6 +22,15 @@ func main() {
os.Exit(1)
}

srv := http.NewHealthCheckServer()
go func() {
if err := srv.Start(); err != nil {
fmt.Printf("Health check server failed to start: %s", err)
os.Exit(1)
}
}()
logs.Logger.Printf("Started healthcheck server on port %d", srv.Port)

runnerType := os.Args[1]
cmd := &commands.LaunchCommand{RunnerType: runnerType}

Expand Down
47 changes: 47 additions & 0 deletions internal/http/healthcheck_server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package http

import (
"encoding/json"
"fmt"
"net/http"
)

const (
defaultPort = 5681
healthCheckPath = "/healthz"
)

type healthCheckResponse struct {
Status string `json:"status"`
}

type Server struct {
Port int
}

func NewHealthCheckServer() *Server {
return &Server{
Port: defaultPort,
}
}

func (s *Server) Start() error {
http.HandleFunc(healthCheckPath, s.handleHealthCheck)

addr := fmt.Sprintf(":%d", s.Port)

return http.ListenAndServe(addr, nil)

Check failure on line 33 in internal/http/healthcheck_server.go

View workflow job for this annotation

GitHub Actions / audit

G114: Use of net/http serve function that has no support for setting timeouts (gosec)
}

func (s *Server) handleHealthCheck(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)

res := healthCheckResponse{Status: "ok"}
json.NewEncoder(w).Encode(res)

Check failure on line 46 in internal/http/healthcheck_server.go

View workflow job for this annotation

GitHub Actions / audit

Error return value of `(*encoding/json.Encoder).Encode` is not checked (errcheck)
}

0 comments on commit 122c23d

Please sign in to comment.