add delta sync before each reconcile, have it not repeat if it's been done in the last minute

This commit is contained in:
Miloš Jovanović 2026-09-24 20:01:01 -07:00
parent ccb6b3277d
commit f98a23418f
3 changed files with 22 additions and 0 deletions

View file

@ -6,6 +6,9 @@ import (
)
func (s *Server) reconcile(ctx context.Context, runID int64, rep *User, dept string) error {
if err := s.syncIfStale(ctx, time.Minute); err != nil {
log.Printf("run %d: pre-run sync failed, using cached roster: %v", runID, err)
}
reps, err := s.store.RepsFor(rep.ChapterID, dept)
if err != nil {
return err

View file

@ -9,6 +9,7 @@ import (
"strconv"
"strings"
"time"
"sync"
)
// User is the subset of an ST user record this worker cares about.
@ -53,6 +54,7 @@ type Server struct {
eligible map[string]bool
// runTimeout caps a single reconcile.
runTimeout time.Duration
syncMu sync.Mutex
}
func NewServer(st STClient, store Store, secret string, eligible map[string]bool) *Server {

17
sync.go
View file

@ -44,3 +44,20 @@ func (s *Server) SyncRoster(ctx context.Context) error {
log.Printf("synced %d users (since=%d)", len(users), since)
return s.store.SetLastSync(started)
}
// syncIfStale runs a delta sync unless one completed within maxAge (1 min)
// Serialized so a burst of runs triggers one sync, not one each.
func (s *Server) syncIfStale(ctx context.Context, maxAge time.Duration) error {
s.syncMu.Lock()
defer s.syncMu.Unlock()
last, err := s.store.LastSync()
if err != nil {
return err
}
if time.Since(time.Unix(last, 0)) < maxAge {
return nil
}
return s.SyncRoster(ctx)
}