Files
worker/app/models/server_health_ticker.go
Gleb Tv 2c7a0236da feat: publish standalone worker
Separate worker packaging and service lifecycle from the control plane.
2026-07-13 17:55:14 +03:00

44 строки
1.0 KiB
Go

package models
import (
"context"
"log"
"sync"
"time"
)
// HealthTickInterval is exported so focused tests can exercise the same
// lifecycle with a short interval.
var HealthTickInterval = time.Minute
var serverHealthTickerOnce sync.Once
// StartServerHealthTicker periodically refreshes derived server health. The
// first production tick is delayed to keep CLI migration paths side-effect
// free; assignment/pause paths recompute synchronously.
func StartServerHealthTicker(parent context.Context) {
serverHealthTickerOnce.Do(func() {
go func() {
ticker := time.NewTicker(HealthTickInterval)
defer ticker.Stop()
for {
select {
case <-parent.Done():
return
case <-ticker.C:
var ids []int64
if err := DB().Model(&Server{}).Pluck("id", &ids).Error; err != nil {
log.Printf("server health: list: %v", err)
continue
}
for _, id := range ids {
if err := HealthForServer(id); err != nil {
log.Printf("server health %d: %v", id, err)
}
}
}
}
}()
})
}