sync mechanism complete
This commit is contained in:
parent
d3803dead3
commit
9ef2d7689d
1 changed files with 46 additions and 0 deletions
46
sync.go
Normal file
46
sync.go
Normal file
|
|
@ -0,0 +1,46 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"log"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (s *Server) SyncRoster(ctx context.Context) error {
|
||||||
|
client, ok := s.st.(*Client)
|
||||||
|
if !ok {
|
||||||
|
return errors.New("sync requires a real client")
|
||||||
|
}
|
||||||
|
|
||||||
|
since, err := s.store.LastSync()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Take the timestamp before fetching, so anything modified mid-sync
|
||||||
|
// is picked up next time rather than skipped.
|
||||||
|
started := time.Now().Unix()
|
||||||
|
|
||||||
|
users, err := client.ListUsers(ctx, since)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := s.store.UpsertPeople(users, s.eligible); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
noDept := 0
|
||||||
|
for _, u := range users {
|
||||||
|
if u.Eligible(s.eligible) && normalizeDept(u.Department) == "" {
|
||||||
|
noDept++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if noDept > 0 {
|
||||||
|
log.Printf("%d eligible users synced with no department; they cannot be assigned", noDept)
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("synced %d users (since=%d)", len(users), since)
|
||||||
|
return s.store.SetLastSync(started)
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue