From 4d367713628023903d2f900758d1c3c28ab8e6e8 Mon Sep 17 00:00:00 2001 From: kolaente Date: Wed, 2 Jul 2025 23:01:41 +0200 Subject: [PATCH] feat: introduce shared health check logic (#1073) --- pkg/cmd/healthcheck.go | 25 ++++------------- pkg/health/health.go | 46 ++++++++++++++++++++++++++++++++ pkg/routes/healthcheck.go | 5 ++++ pkg/webtests/healthcheck_test.go | 9 ++++++- 4 files changed, 64 insertions(+), 21 deletions(-) create mode 100644 pkg/health/health.go diff --git a/pkg/cmd/healthcheck.go b/pkg/cmd/healthcheck.go index 59cf25e5b..5a98101d5 100644 --- a/pkg/cmd/healthcheck.go +++ b/pkg/cmd/healthcheck.go @@ -18,11 +18,9 @@ package cmd import ( "fmt" - "net/http" "os" - "time" - "code.vikunja.io/api/pkg/config" + "code.vikunja.io/api/pkg/health" "code.vikunja.io/api/pkg/initialize" "github.com/spf13/cobra" @@ -36,29 +34,16 @@ var healthcheckCmd = &cobra.Command{ Use: "healthcheck", Short: "Preform a healthcheck on the Vikunja api server", PreRun: func(_ *cobra.Command, _ []string) { - initialize.LightInit() + initialize.FullInitWithoutAsync() }, Run: func(_ *cobra.Command, _ []string) { - client := &http.Client{ - Timeout: 5 * time.Second, - } - host := config.ServiceInterface.GetString() - url := "http://%s/health" - resp, err := client.Get(fmt.Sprintf(url, host)) - if err != nil { + if err := health.Check(); err != nil { fmt.Printf("API server is not healthy: %v\n", err) os.Exit(1) return } - defer resp.Body.Close() - // Check the response status - if resp.StatusCode == http.StatusOK { - fmt.Println("API server is healthy") - os.Exit(0) - return - } - fmt.Printf("API server is not healthy: HTTP %d\n", resp.StatusCode) - os.Exit(1) + fmt.Println("API server is healthy") + os.Exit(0) }, } diff --git a/pkg/health/health.go b/pkg/health/health.go new file mode 100644 index 000000000..88a0245df --- /dev/null +++ b/pkg/health/health.go @@ -0,0 +1,46 @@ +// Vikunja is a to-do list application to facilitate your life. +// Copyright 2018-present Vikunja and contributors. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +package health + +import ( + "context" + "errors" + + "code.vikunja.io/api/pkg/config" + "code.vikunja.io/api/pkg/db" + "code.vikunja.io/api/pkg/red" +) + +// Check verifies the main service dependencies are reachable. +func Check() error { + s := db.NewSession() + defer s.Close() + if err := s.Ping(); err != nil { + return err + } + + if config.RedisEnabled.GetBool() { + r := red.GetRedis() + if r == nil { + return errors.New("redis not initialized") + } + if err := r.Ping(context.Background()).Err(); err != nil { + return err + } + } + return nil +} diff --git a/pkg/routes/healthcheck.go b/pkg/routes/healthcheck.go index 6194133c9..fe76b01b9 100644 --- a/pkg/routes/healthcheck.go +++ b/pkg/routes/healthcheck.go @@ -20,9 +20,14 @@ import ( "net/http" "github.com/labstack/echo/v4" + + "code.vikunja.io/api/pkg/health" ) // HealthcheckHandler handles healthckeck 'OK' response func HealthcheckHandler(c echo.Context) error { + if err := health.Check(); err != nil { + return echo.NewHTTPError(http.StatusInternalServerError).SetInternal(err) + } return c.String(http.StatusOK, "OK") } diff --git a/pkg/webtests/healthcheck_test.go b/pkg/webtests/healthcheck_test.go index c8607142d..785c3d28e 100644 --- a/pkg/webtests/healthcheck_test.go +++ b/pkg/webtests/healthcheck_test.go @@ -20,6 +20,7 @@ import ( "net/http" "testing" + "code.vikunja.io/api/pkg/health" "code.vikunja.io/api/pkg/routes" "github.com/stretchr/testify/assert" @@ -27,7 +28,13 @@ import ( ) func TestHealthcheck(t *testing.T) { - t.Run("healthcheck", func(t *testing.T) { + t.Run("function", func(t *testing.T) { + _, err := setupTestEnv() + require.NoError(t, err) + require.NoError(t, health.Check()) + }) + + t.Run("route", func(t *testing.T) { rec, err := newTestRequest(t, http.MethodGet, routes.HealthcheckHandler, ``, nil, nil) require.NoError(t, err) assert.Contains(t, rec.Body.String(), "OK")