add delta sync before each reconcile, have it not repeat if it's been done in the last minute
This commit is contained in:
parent
ccb6b3277d
commit
f98a23418f
3 changed files with 22 additions and 0 deletions
|
|
@ -6,6 +6,9 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func (s *Server) reconcile(ctx context.Context, runID int64, rep *User, dept string) error {
|
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)
|
reps, err := s.store.RepsFor(rep.ChapterID, dept)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,7 @@ import (
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
// User is the subset of an ST user record this worker cares about.
|
// User is the subset of an ST user record this worker cares about.
|
||||||
|
|
@ -53,6 +54,7 @@ type Server struct {
|
||||||
eligible map[string]bool
|
eligible map[string]bool
|
||||||
// runTimeout caps a single reconcile.
|
// runTimeout caps a single reconcile.
|
||||||
runTimeout time.Duration
|
runTimeout time.Duration
|
||||||
|
syncMu sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewServer(st STClient, store Store, secret string, eligible map[string]bool) *Server {
|
func NewServer(st STClient, store Store, secret string, eligible map[string]bool) *Server {
|
||||||
|
|
|
||||||
17
sync.go
17
sync.go
|
|
@ -44,3 +44,20 @@ func (s *Server) SyncRoster(ctx context.Context) error {
|
||||||
log.Printf("synced %d users (since=%d)", len(users), since)
|
log.Printf("synced %d users (since=%d)", len(users), since)
|
||||||
return s.store.SetLastSync(started)
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue