fix: Correct version check and refactor healthz check

This commit is contained in:
bakito 2025-07-16 16:59:39 +02:00
parent 447bfb24a9
commit 7b382ada9f
No known key found for this signature in database
GPG key ID: BCCEB081DB8A24D8
2 changed files with 16 additions and 25 deletions

View file

@ -94,7 +94,21 @@ func (w *worker) handleStatus(c *gin.Context) {
}
func (w *worker) handleHealthz(c *gin.Context) {
c.Status(w.healthz())
status := w.status()
if status.Origin.Status != "success" {
c.Status(http.StatusServiceUnavailable)
return
}
for _, replica := range status.Replicas {
if replica.Status != "success" {
c.Status(http.StatusServiceUnavailable)
return
}
}
c.Status(http.StatusOK)
}
func (w *worker) listenAndServe() {

View file

@ -3,7 +3,6 @@ package sync
import (
"errors"
"fmt"
"regexp"
"runtime"
"sort"
"time"
@ -21,10 +20,7 @@ import (
"github.com/bakito/adguardhome-sync/version"
)
var (
l = log.GetLogger("sync")
fixVersionCompareRegExp = regexp.MustCompile(`[^0-9.]`)
)
var l = log.GetLogger("sync")
// Sync config from origin to replica.
func Sync(cfg *types.Config) error {
@ -124,22 +120,6 @@ func (w *worker) status() *syncStatus {
return syncStatus
}
func (w *worker) healthz() int {
status := w.status()
if status.Origin.Status != "success" {
return 503
}
for _, replica := range status.Replicas {
if replica.Status != "success" {
return 503
}
}
return 200
}
func (w *worker) getStatus(inst types.AdGuardInstance) replicaStatus {
st := replicaStatus{Host: inst.WebHost, URL: inst.WebURL}
@ -313,9 +293,6 @@ func (w *worker) syncTo(l *zap.SugaredLogger, o *origin, replica types.AdGuardIn
return
}
replicaStatus.Version = fixVersionCompareRegExp.ReplaceAllString(replicaStatus.Version, "")
o.status.Version = fixVersionCompareRegExp.ReplaceAllString(o.status.Version, "")
rl.With("version", replicaStatus.Version).Info("Connected to replica")
if versions.IsNewerThan(versions.MinAgh, replicaStatus.Version) {