diff --git a/README.md b/README.md index fc94587..056bd4e 100644 --- a/README.md +++ b/README.md @@ -112,7 +112,9 @@ make run ## Health check -The launcher exposes a health check endpoint at `/healthz` on port `5681` for liveness checks. +The launcher exposes a health check endpoint at `/healthz` on port `5681` for liveness checks. To override the launcher health check port, set the `N8N_LAUNCHER_HEALTCHECK_PORT` env var. + +If overriding the default health check port, be mindful of port conflicts - the n8n main instance uses `5678` by default for its HTTP server and `5679` for its WS server, and the runner uses `5680` by default for its WS server. ## Release diff --git a/cmd/launcher/main.go b/cmd/launcher/main.go index a3de1ab..d71e0af 100644 --- a/cmd/launcher/main.go +++ b/cmd/launcher/main.go @@ -2,7 +2,7 @@ package main import ( "flag" - "fmt" + "net" "os" "task-runner-launcher/internal/commands" @@ -24,9 +24,13 @@ func main() { srv := http.NewHealthCheckServer() go func() { + logs.Logger.Printf("Starting health check server at port %d", http.GetPort()) + if err := srv.ListenAndServe(); err != nil { - fmt.Printf("Health check server failed to start: %s", err) - os.Exit(1) + if opErr, ok := err.(*net.OpError); ok && opErr.Op == "listen" { + logs.Logger.Fatalf("Health check server failed to start: Port %d is already in use", http.GetPort()) + } + logs.Logger.Fatalf("Health check server failed to start: %s", err) } }() diff --git a/internal/http/healthcheck_server.go b/internal/http/healthcheck_server.go index cede22a..86380f7 100644 --- a/internal/http/healthcheck_server.go +++ b/internal/http/healthcheck_server.go @@ -4,12 +4,15 @@ import ( "encoding/json" "fmt" "net/http" + "os" + "strconv" "task-runner-launcher/internal/logs" "time" ) const ( defaultPort = 5681 + portEnvVar = "N8N_LAUNCHER_HEALTCHECK_PORT" healthCheckPath = "/healthz" readTimeout = 5 * time.Second writeTimeout = 5 * time.Second @@ -20,7 +23,7 @@ func NewHealthCheckServer() *http.Server { mux.HandleFunc(healthCheckPath, handleHealthCheck) return &http.Server{ - Addr: fmt.Sprintf(":%d", defaultPort), + Addr: fmt.Sprintf(":%d", GetPort()), Handler: mux, ReadTimeout: readTimeout, WriteTimeout: writeTimeout, @@ -45,3 +48,14 @@ func handleHealthCheck(w http.ResponseWriter, r *http.Request) { return } } + +func GetPort() int { + if customPortStr := os.Getenv(portEnvVar); customPortStr != "" { + if customPort, err := strconv.Atoi(customPortStr); err == nil && customPort > 0 && customPort < 65536 { + return customPort + } + logs.Logger.Printf("Invalid port in %s, falling back to default port %d", portEnvVar, defaultPort) + } + + return defaultPort +}