feat: introduce shared health check logic (#1073)

This commit is contained in:
kolaente
2025-07-02 23:01:41 +02:00
committed by GitHub
parent e7a4d9f180
commit 4d36771362
4 changed files with 64 additions and 21 deletions

View File

@@ -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)
},
}

46
pkg/health/health.go Normal file
View File

@@ -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 <https://www.gnu.org/licenses/>.
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
}

View File

@@ -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")
}

View File

@@ -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")