Skip to content

Commit

Permalink
Make port configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
ivov committed Nov 22, 2024
1 parent b43feef commit fddf6cf
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 7 additions & 3 deletions cmd/launcher/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package main

import (
"flag"
"fmt"
"net"
"os"

"task-runner-launcher/internal/commands"
Expand All @@ -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)
}
}()

Expand Down
16 changes: 15 additions & 1 deletion internal/http/healthcheck_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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
}

0 comments on commit fddf6cf

Please sign in to comment.