sync mechanism complete

This commit is contained in:
Miloš Jovanović 2026-09-10 00:20:56 +02:00
parent 40f16346f5
commit d3803dead3
3 changed files with 142 additions and 105 deletions

View file

@ -3,8 +3,8 @@ package main
import (
"context"
"crypto/subtle"
"fmt"
"errors"
"fmt"
"log"
"net/http"
"strconv"
@ -14,11 +14,12 @@ import (
// User is the subset of an ST user record this worker cares about.
type User struct {
ID int64
Email string
ChapterID int64
Department string
ID int64
Email string
ChapterID int64
Department string
AppointmentTypes []string
IsRep bool
}
// STClient is what the handler needs from the Solidarity Tech API.
@ -30,6 +31,10 @@ type STClient interface {
type Store interface {
AcquireLock(chapterID int64, dept string, repID int64) (int64, error)
ReleaseLock(runID int64) error
UpsertPeople(users []User, eligible map[string]bool) error
LastSync() (int64, error)
SetLastSync(ts int64) error
}
// ErrLocked is returned by AcquireLock when a run is already in flight
@ -37,9 +42,9 @@ type Store interface {
var ErrLocked = errors.New("department already running")
type Server struct {
st STClient
store Store
secret string
st STClient
store Store
secret string
eligible map[string]bool
// runTimeout caps a single reconcile.
runTimeout time.Duration
@ -86,8 +91,7 @@ func (s *Server) Routes() *http.ServeMux {
http.Error(w, err.Error(), 500)
return
}
fmt.Fprintf(w, "%d users\n", len(users))
counts := map[string]int{}
counts := map[string]int{}
eligible := 0
for _, u := range users {
if !u.Eligible(s.eligible) {
@ -102,6 +106,17 @@ func (s *Server) Routes() *http.ServeMux {
}
})
// TEMPORARY: remove before deploy.
mux.HandleFunc("POST /debug/sync", func(w http.ResponseWriter, r *http.Request) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute)
defer cancel()
if err := s.SyncRoster(ctx); err != nil {
http.Error(w, err.Error(), 500)
return
}
w.WriteHeader(http.StatusOK)
})
return mux
}