sync mechanism complete
This commit is contained in:
parent
40f16346f5
commit
d3803dead3
3 changed files with 142 additions and 105 deletions
|
|
@ -104,6 +104,7 @@ func (a apiUser) toUser() *User {
|
||||||
ChapterID: a.ChapterID,
|
ChapterID: a.ChapterID,
|
||||||
Department: departmentOf(a.CustomProps),
|
Department: departmentOf(a.CustomProps),
|
||||||
AppointmentTypes: optionLabels(a.CustomProps, "appointment-type"),
|
AppointmentTypes: optionLabels(a.CustomProps, "appointment-type"),
|
||||||
|
IsRep: isRep(a.CustomProps),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
35
server.go
35
server.go
|
|
@ -3,8 +3,8 @@ package main
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"crypto/subtle"
|
"crypto/subtle"
|
||||||
"fmt"
|
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
@ -14,11 +14,12 @@ import (
|
||||||
|
|
||||||
// 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.
|
||||||
type User struct {
|
type User struct {
|
||||||
ID int64
|
ID int64
|
||||||
Email string
|
Email string
|
||||||
ChapterID int64
|
ChapterID int64
|
||||||
Department string
|
Department string
|
||||||
AppointmentTypes []string
|
AppointmentTypes []string
|
||||||
|
IsRep bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// STClient is what the handler needs from the Solidarity Tech API.
|
// STClient is what the handler needs from the Solidarity Tech API.
|
||||||
|
|
@ -30,6 +31,10 @@ type STClient interface {
|
||||||
type Store interface {
|
type Store interface {
|
||||||
AcquireLock(chapterID int64, dept string, repID int64) (int64, error)
|
AcquireLock(chapterID int64, dept string, repID int64) (int64, error)
|
||||||
ReleaseLock(runID 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
|
// 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")
|
var ErrLocked = errors.New("department already running")
|
||||||
|
|
||||||
type Server struct {
|
type Server struct {
|
||||||
st STClient
|
st STClient
|
||||||
store Store
|
store Store
|
||||||
secret string
|
secret string
|
||||||
eligible map[string]bool
|
eligible map[string]bool
|
||||||
// runTimeout caps a single reconcile.
|
// runTimeout caps a single reconcile.
|
||||||
runTimeout time.Duration
|
runTimeout time.Duration
|
||||||
|
|
@ -86,8 +91,7 @@ func (s *Server) Routes() *http.ServeMux {
|
||||||
http.Error(w, err.Error(), 500)
|
http.Error(w, err.Error(), 500)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
fmt.Fprintf(w, "%d users\n", len(users))
|
counts := map[string]int{}
|
||||||
counts := map[string]int{}
|
|
||||||
eligible := 0
|
eligible := 0
|
||||||
for _, u := range users {
|
for _, u := range users {
|
||||||
if !u.Eligible(s.eligible) {
|
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
|
return mux
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
211
store.go
211
store.go
|
|
@ -3,41 +3,56 @@ package main
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"modernc.org/sqlite"
|
"modernc.org/sqlite"
|
||||||
sqlite3 "modernc.org/sqlite/lib"
|
sqlite3 "modernc.org/sqlite/lib"
|
||||||
)
|
)
|
||||||
|
|
||||||
const schema = `
|
var schemaStatements = []string{
|
||||||
CREATE TABLE IF NOT EXISTS people (
|
`CREATE TABLE IF NOT EXISTS runs (
|
||||||
id INTEGER PRIMARY KEY,
|
id INTEGER PRIMARY KEY,
|
||||||
email TEXT,
|
chapter_id INTEGER NOT NULL,
|
||||||
chapter_id INTEGER,
|
department TEXT NOT NULL,
|
||||||
department TEXT,
|
rep_user_id INTEGER NOT NULL,
|
||||||
appointment_type TEXT,
|
status TEXT NOT NULL,
|
||||||
is_rep INTEGER NOT NULL DEFAULT 0,
|
started_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
synced_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP
|
ended_at TEXT
|
||||||
);
|
)`,
|
||||||
|
`CREATE UNIQUE INDEX IF NOT EXISTS one_run_per_dept
|
||||||
CREATE INDEX IF NOT EXISTS people_dept ON people(chapter_id, department);
|
ON runs(chapter_id, department) WHERE status = 'running'`,
|
||||||
|
`CREATE TABLE IF NOT EXISTS people (
|
||||||
CREATE TABLE IF NOT EXISTS sync_state (
|
id INTEGER PRIMARY KEY,
|
||||||
key TEXT PRIMARY KEY,
|
email TEXT,
|
||||||
value TEXT NOT NULL
|
chapter_id INTEGER,
|
||||||
);
|
department TEXT,
|
||||||
`
|
appointment_type TEXT,
|
||||||
|
is_rep INTEGER NOT NULL DEFAULT 0,
|
||||||
|
is_eligible INTEGER NOT NULL DEFAULT 0,
|
||||||
|
synced_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||||
|
)`,
|
||||||
|
`CREATE INDEX IF NOT EXISTS people_dept ON people(chapter_id, department)`,
|
||||||
|
`CREATE TABLE IF NOT EXISTS sync_state (
|
||||||
|
key TEXT PRIMARY KEY,
|
||||||
|
value TEXT NOT NULL
|
||||||
|
)`,
|
||||||
|
}
|
||||||
|
|
||||||
type SQLStore struct{ db *sql.DB }
|
type SQLStore struct{ db *sql.DB }
|
||||||
|
|
||||||
func OpenStore(path string) (*SQLStore, error) {
|
func OpenStore(path string) (*SQLStore, error) {
|
||||||
db, err := sql.Open("sqlite",
|
db, err := sql.Open("sqlite",
|
||||||
"file:"+path+"?_pragma=journal_mode(WAL)&_pragma=busy_timeout(5000)")
|
"file:"+path+"?_pragma=journal_mode(WAL)&_pragma=busy_timeout(5000)")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
db.SetMaxOpenConns(1)
|
db.SetMaxOpenConns(1)
|
||||||
if _, err := db.Exec(schema); err != nil {
|
for i, stmt := range schemaStatements {
|
||||||
return nil, err
|
if _, err := db.Exec(stmt); err != nil {
|
||||||
|
return nil, fmt.Errorf("schema statement %d: %w", i, err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return &SQLStore{db: db}, nil
|
return &SQLStore{db: db}, nil
|
||||||
}
|
}
|
||||||
|
|
@ -45,84 +60,90 @@ func OpenStore(path string) (*SQLStore, error) {
|
||||||
func (s *SQLStore) AcquireLock(chapterID int64, dept string, repID int64) (int64, error) {
|
func (s *SQLStore) AcquireLock(chapterID int64, dept string, repID int64) (int64, error) {
|
||||||
res, err := s.db.Exec(
|
res, err := s.db.Exec(
|
||||||
`INSERT INTO runs (chapter_id, department, rep_user_id, status)
|
`INSERT INTO runs (chapter_id, department, rep_user_id, status)
|
||||||
VALUES (?, ?, ?, 'running')`, chapterID, dept, repID)
|
VALUES (?, ?, ?, 'running')`, chapterID, dept, repID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
var serr *sqlite.Error
|
var serr *sqlite.Error
|
||||||
if errors.As(err, &serr) && serr.Code() == sqlite3.SQLITE_CONSTRAINT_UNIQUE {
|
if errors.As(err, &serr) && serr.Code() == sqlite3.SQLITE_CONSTRAINT_UNIQUE {
|
||||||
|
return 0, ErrLocked
|
||||||
|
}
|
||||||
return 0, ErrLocked
|
return 0, ErrLocked
|
||||||
}
|
}
|
||||||
return 0, ErrLocked
|
return res.LastInsertId()
|
||||||
}
|
}
|
||||||
return res.LastInsertId()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *SQLStore) ReleaseLock(runID int64) error {
|
func (s *SQLStore) ReleaseLock(runID int64) error {
|
||||||
_, err := s.db.Exec(
|
_, err := s.db.Exec(
|
||||||
`UPDATE runs SET status='done', ended_at=CURRENT_TIMESTAMP WHERE id=?`, runID)
|
`UPDATE runs SET status='done', ended_at=CURRENT_TIMESTAMP WHERE id=?`, runID)
|
||||||
return err
|
return err
|
||||||
}
|
|
||||||
|
|
||||||
func (s *SQLStore) SweepStaleLocks() error {
|
|
||||||
_, err := s.db.Exec(
|
|
||||||
`UPDATE runs SET status='crashed', ended_at=CURRENT_TIMESTAMP
|
|
||||||
WHERE status='running'`)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *SQLStore) UpsertPeople(users []User, eligible map[string]bool) error {
|
|
||||||
tx, err := s.db.Begin()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer tx.Rollback()
|
|
||||||
|
|
||||||
stmt, err := tx.Prepare(`
|
|
||||||
INSERT INTO people (id, email, chapter_id, department, appointment_type, is_rep, synced_at)
|
|
||||||
VALUES (?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP)
|
|
||||||
ON CONFLICT(id) DO UPDATE SET
|
|
||||||
email=excluded.email,
|
|
||||||
chapter_id=excluded.chapter_id,
|
|
||||||
department=excluded.department,
|
|
||||||
appointment_type=excluded.appointment_type,
|
|
||||||
is_rep=excluded.is_rep,
|
|
||||||
synced_at=CURRENT_TIMESTAMP`)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer stmt.Close()
|
|
||||||
|
|
||||||
for _, u := range users {
|
|
||||||
rep := 0
|
|
||||||
if u.IsRep {
|
|
||||||
rep = 1
|
|
||||||
}
|
}
|
||||||
_, err := stmt.Exec(u.ID, u.Email, u.ChapterID,
|
|
||||||
normalizeDept(u.Department),
|
|
||||||
strings.Join(u.AppointmentTypes, ","),
|
|
||||||
rep)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("upserting user %d: %w", u.ID, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return tx.Commit()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *SQLStore) LastSync() (int64, error) {
|
func (s *SQLStore) SweepStaleLocks() error {
|
||||||
var v string
|
_, err := s.db.Exec(
|
||||||
err := s.db.QueryRow(`SELECT value FROM sync_state WHERE key='users_synced_at'`).Scan(&v)
|
`UPDATE runs SET status='crashed', ended_at=CURRENT_TIMESTAMP
|
||||||
if err == sql.ErrNoRows {
|
WHERE status='running'`)
|
||||||
return 0, nil
|
return err
|
||||||
}
|
}
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
return strconv.ParseInt(v, 10, 64)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *SQLStore) SetLastSync(ts int64) error {
|
func (s *SQLStore) UpsertPeople(users []User, eligible map[string]bool) error {
|
||||||
_, err := s.db.Exec(`
|
tx, err := s.db.Begin()
|
||||||
INSERT INTO sync_state (key, value) VALUES ('users_synced_at', ?)
|
if err != nil {
|
||||||
ON CONFLICT(key) DO UPDATE SET value=excluded.value`,
|
return err
|
||||||
strconv.FormatInt(ts, 10))
|
}
|
||||||
return err
|
defer tx.Rollback()
|
||||||
}
|
|
||||||
|
stmt, err := tx.Prepare(`
|
||||||
|
INSERT INTO people (id, email, chapter_id, department, appointment_type, is_rep, is_eligible, synced_at)
|
||||||
|
VALUES (?, ?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP)
|
||||||
|
ON CONFLICT(id) DO UPDATE SET
|
||||||
|
email=excluded.email,
|
||||||
|
chapter_id=excluded.chapter_id,
|
||||||
|
department=excluded.department,
|
||||||
|
appointment_type=excluded.appointment_type,
|
||||||
|
is_rep=excluded.is_rep,
|
||||||
|
is_rep=excluded.is_rep,
|
||||||
|
synced_at=CURRENT_TIMESTAMP`)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer stmt.Close()
|
||||||
|
|
||||||
|
//loop body
|
||||||
|
|
||||||
|
for _, u := range users {
|
||||||
|
rep, elig := 0, 0
|
||||||
|
if u.IsRep {
|
||||||
|
rep = 1
|
||||||
|
}
|
||||||
|
if u.Eligible(eligible) {
|
||||||
|
elig = 1
|
||||||
|
}
|
||||||
|
_, err := stmt.Exec(u.ID, u.Email, u.ChapterID,
|
||||||
|
normalizeDept(u.Department),
|
||||||
|
strings.Join(u.AppointmentTypes, ","),
|
||||||
|
rep, elig)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("upserting user %d: %w", u.ID, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return tx.Commit()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *SQLStore) LastSync() (int64, error) {
|
||||||
|
var v string
|
||||||
|
err := s.db.QueryRow(`SELECT value FROM sync_state WHERE key='users_synced_at'`).Scan(&v)
|
||||||
|
if err == sql.ErrNoRows {
|
||||||
|
return 0, nil
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
return strconv.ParseInt(v, 10, 64)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *SQLStore) SetLastSync(ts int64) error {
|
||||||
|
_, err := s.db.Exec(`
|
||||||
|
INSERT INTO sync_state (key, value) VALUES ('users_synced_at', ?)
|
||||||
|
ON CONFLICT(key) DO UPDATE SET value=excluded.value`,
|
||||||
|
strconv.FormatInt(ts, 10))
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue