Skip to content

PostgreSQL backup tool with Point-In-Time-Recovery and restore verification

FEATURE (docker): Add `databasus healthcheck` command and image HEALTHCHECK (issue #648)

Rostislav Dugin1a65eb14f6a4
diff --git a/Dockerfile b/Dockerfile
index 3c63c348..501e0b78 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -165,9 +165,12 @@ WORKDIR /app
# Copy Goose from build stage
COPY --from=backend-build /usr/local/bin/goose /usr/local/bin/goose
-# Copy app binary
+# Copy app binary
COPY --from=backend-build /app/main .
+# Expose the binary as the `databasus` command on PATH (e.g. `databasus healthcheck`)
+RUN ln -s /app/main /usr/local/bin/databasus
+
# Copy migrations directory
COPY backend/migrations ./migrations
@@ -427,6 +430,12 @@ RUN chmod +x /app/start.sh
EXPOSE 4005
+# Liveness probe: the runtime image ships no wget/curl, so the binary checks
+# itself. Targets the dependency-free /system/version endpoint (not the deep
+# /system/health) so a degraded-but-serving instance is never restarted.
+HEALTHCHECK --interval=30s --timeout=5s --start-period=60s --retries=3 \
+ CMD ["databasus", "healthcheck"]
+
# Volume for PostgreSQL data
VOLUME ["/databasus-data"]
diff --git a/README.md b/README.md
index 604b0f0b..eb35d0d5 100644
--- a/README.md
+++ b/README.md
@@ -176,6 +176,12 @@ services:
volumes:
- ./databasus-data:/databasus-data
restart: unless-stopped
+ healthcheck:
+ test: ["CMD", "databasus", "healthcheck"]
+ interval: 30s
+ timeout: 5s
+ retries: 3
+ start_period: 60s
```
Then run:
diff --git a/backend/Makefile b/backend/Makefile
index ace4a8d6..1161ae12 100644
--- a/backend/Makefile
+++ b/backend/Makefile
@@ -4,7 +4,7 @@ PORT ?= 4005
TEST_PARALLEL_WORKERS ?= 8
run: kill-stale
- go run cmd/main.go
+ go run ./cmd
test: clean-testcontainers pull-testcontainers
TEST_PARALLEL_WORKERS=$(TEST_PARALLEL_WORKERS) go run ./cmd/cleanup_test_db
diff --git a/backend/cmd/healthcheck.go b/backend/cmd/healthcheck.go
new file mode 100644
index 00000000..91859d08
--- /dev/null
+++ b/backend/cmd/healthcheck.go
@@ -0,0 +1,47 @@
+package main
+
+import (
+ "context"
+ "fmt"
+ "io"
+ "net/http"
+ "os"
+ "time"
+)
+
+const healthcheckTimeout = 4 * time.Second
+
+func checkServerAlive(baseURL string) error {
+ ctx, cancel := context.WithTimeout(context.Background(), healthcheckTimeout)
+ defer cancel()
+
+ request, err := http.NewRequestWithContext(ctx, http.MethodGet, baseURL+"/api/v1/system/version", nil)
+ if err != nil {
+ return err
+ }
+
+ response, err := http.DefaultClient.Do(request)
+ if err != nil {
+ return err
+ }
+ defer func() { _ = response.Body.Close() }()
+
+ _, _ = io.Copy(io.Discard, response.Body)
+
+ if response.StatusCode != http.StatusOK {
+ return fmt.Errorf("unexpected status code: %d", response.StatusCode)
+ }
+
+ return nil
+}
+
+func runHealthcheckCommand() {
+ err := checkServerAlive("http://localhost" + serverAddr)
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "healthcheck failed: %v\n", err)
+ os.Exit(1)
+ }
+
+ fmt.Println("healthcheck ok")
+ os.Exit(0)
+}
diff --git a/backend/cmd/healthcheck_test.go b/backend/cmd/healthcheck_test.go
new file mode 100644
index 00000000..e9b48590
--- /dev/null
+++ b/backend/cmd/healthcheck_test.go
@@ -0,0 +1,43 @@
+package main
+
+import (
+ "net/http"
+ "net/http/httptest"
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+)
+
+func Test_CheckServerAlive_WhenServerReturns200_ReturnsNil(t *testing.T) {
+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ assert.Equal(t, "/api/v1/system/version", r.URL.Path)
+ w.WriteHeader(http.StatusOK)
+ }))
+ defer server.Close()
+
+ err := checkServerAlive(server.URL)
+
+ assert.NoError(t, err)
+}
+
+func Test_CheckServerAlive_WhenServerReturnsNon200_ReturnsErrorWithStatus(t *testing.T) {
+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ w.WriteHeader(http.StatusInternalServerError)
+ }))
+ defer server.Close()
+
+ err := checkServerAlive(server.URL)
+
+ assert.Error(t, err)
+ assert.Contains(t, err.Error(), "500")
+}
+
+func Test_CheckServerAlive_WhenServerUnreachable_ReturnsError(t *testing.T) {
+ server := httptest.NewServer(http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) {}))
+ unreachableURL := server.URL
+ server.Close()
+
+ err := checkServerAlive(unreachableURL)
+
+ assert.Error(t, err)
+}
diff --git a/backend/cmd/main.go b/backend/cmd/main.go
index 9b4c5612..d9e6264c 100644
--- a/backend/cmd/main.go
+++ b/backend/cmd/main.go
@@ -66,7 +66,15 @@ import (
// @host localhost:4005
// @BasePath /api/v1
// @schemes http
+
+const serverAddr = ":4005"
+
func main() {
+ if len(os.Args) > 1 && os.Args[1] == "healthcheck" {
+ runHealthcheckCommand() // exits the process
+ return
+ }
+
log := logger.GetLogger()
cache_utils.TestCacheConnection()
@@ -172,7 +180,7 @@ func resetPassword(email, newPassword string, log *slog.Logger) {
func startServerWithGracefulShutdown(log *slog.Logger, app *gin.Engine) {
srv := &http.Server{
- Addr: ":4005",
+ Addr: serverAddr,
Handler: app,
}
diff --git a/docker-compose.yml b/docker-compose.yml
index fbebf81c..b7430a64 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -17,6 +17,12 @@ services:
- ./databasus-data:/databasus-data
container_name: databasus-local
restart: unless-stopped
+ healthcheck:
+ test: ["CMD", "databasus", "healthcheck"]
+ interval: 30s
+ timeout: 5s
+ retries: 3
+ start_period: 60s
# ================================================================
# Backend dev services
diff --git a/install-databasus.sh b/install-databasus.sh
index 6332ebdc..85f49684 100644
--- a/install-databasus.sh
+++ b/install-databasus.sh
@@ -120,6 +120,12 @@ services:
volumes:
- ./databasus-data:/databasus-data
restart: unless-stopped
+ healthcheck:
+ test: ["CMD", "databasus", "healthcheck"]
+ interval: 30s
+ timeout: 5s
+ retries: 3
+ start_period: 60s
EOF
log "docker-compose.yml created successfully"