From a71cd1c6ae26110abf791152bbeef184cbed681c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iv=C3=A1n=20Ovejero?= Date: Mon, 2 Dec 2024 12:22:43 +0100 Subject: [PATCH] refactor: Encapsulate health check server logic (#46) --- cmd/launcher/main.go | 15 +-------------- internal/http/healthcheck_server.go | 22 +++++++++++++++++++++- internal/http/healthcheck_server_test.go | 2 +- 3 files changed, 23 insertions(+), 16 deletions(-) diff --git a/cmd/launcher/main.go b/cmd/launcher/main.go index ef2df7d..2d1eacb 100644 --- a/cmd/launcher/main.go +++ b/cmd/launcher/main.go @@ -3,7 +3,6 @@ package main import ( "flag" "fmt" - "net" "os" "task-runner-launcher/internal/commands" @@ -40,19 +39,7 @@ func main() { errorreporting.Init(cfg.Sentry) defer errorreporting.Close() - srv := http.NewHealthCheckServer(cfg.HealthCheckServerPort) - go func() { - if err := srv.ListenAndServe(); err != nil { - errMsg := "Health check server failed to start" - if opErr, ok := err.(*net.OpError); ok && opErr.Op == "listen" { - errMsg = fmt.Sprintf("%s: Port %s is already in use", errMsg, srv.Addr) - } else { - errMsg = fmt.Sprintf("%s: %s", errMsg, err) - } - logs.Error(errMsg) - } - logs.Infof("Started launcher's health check server at port %d", srv.Addr) - }() + http.InitHealthCheckServer(cfg.HealthCheckServerPort) cmd := &commands.LaunchCommand{} diff --git a/internal/http/healthcheck_server.go b/internal/http/healthcheck_server.go index 6d9c52b..c07fa63 100644 --- a/internal/http/healthcheck_server.go +++ b/internal/http/healthcheck_server.go @@ -3,6 +3,7 @@ package http import ( "encoding/json" "fmt" + "net" "net/http" "task-runner-launcher/internal/logs" "time" @@ -14,7 +15,26 @@ const ( writeTimeout = 1 * time.Second ) -func NewHealthCheckServer(port string) *http.Server { +// InitHealthCheckServer creates and starts the launcher's health check server +// exposing `/healthz` at the given port, running in a goroutine. +func InitHealthCheckServer(port string) { + srv := newHealthCheckServer(port) + go func() { + logs.Infof("Starting launcher's health check server at port %s", port) + if err := srv.ListenAndServe(); err != nil { + errMsg := "Health check server failed to start" + if opErr, ok := err.(*net.OpError); ok && opErr.Op == "listen" { + errMsg = fmt.Sprintf("%s: Port %s is already in use", errMsg, srv.Addr) + } else { + errMsg = fmt.Sprintf("%s: %s", errMsg, err) + } + logs.Error(errMsg) + return + } + }() +} + +func newHealthCheckServer(port string) *http.Server { mux := http.NewServeMux() mux.HandleFunc(healthCheckPath, handleHealthCheck) diff --git a/internal/http/healthcheck_server_test.go b/internal/http/healthcheck_server_test.go index ad1281e..0304b9d 100644 --- a/internal/http/healthcheck_server_test.go +++ b/internal/http/healthcheck_server_test.go @@ -86,7 +86,7 @@ func (w *failingWriter) WriteHeader(statusCode int) { } func TestNewHealthCheckServer(t *testing.T) { - server := NewHealthCheckServer("5680") + server := newHealthCheckServer("5680") require.NotNil(t, server, "server should not be nil")