Add global role synchronization for Discord with configurable interval
This commit introduces a new feature for global role synchronization in Discord, allowing for periodic reconciliation of managed roles. A new environment variable, `DISCORD_GLOBAL_SYNC_INTERVAL`, has been added to configure the synchronization interval, defaulting to 5 minutes. The `RoleWorker` has been updated to schedule global sync jobs, ensuring that missing managed roles are restored and extra assignments are removed without affecting unrelated server roles. Database schema changes support the new synchronization logic, and tests have been added to validate the functionality of the global reconciliation process.
This commit is contained in:
@@ -37,6 +37,13 @@ func (s *Store) SeedDiscordRoleSyncJobs(ctx context.Context) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *Store) ScheduleGlobalDiscordRoleSync(ctx context.Context, bucket int64) error {
|
||||
_, err := s.pool.Exec(ctx, `INSERT INTO discord_role_sync_jobs(event_id,action,generation)
|
||||
VALUES('__global__','full_reconcile',$1)
|
||||
ON CONFLICT(event_id,action,generation) DO NOTHING`, bucket)
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *Store) ClaimDiscordRoleSyncJob(ctx context.Context) (application.DiscordRoleSyncJob, error) {
|
||||
var job application.DiscordRoleSyncJob
|
||||
var snapshot []byte
|
||||
@@ -97,6 +104,24 @@ func (s *Store) ListDiscordManagedRoles(ctx context.Context, eventID string) ([]
|
||||
return roles, rows.Err()
|
||||
}
|
||||
|
||||
func (s *Store) ListAllDiscordManagedRoles(ctx context.Context) ([]application.DiscordManagedRole, error) {
|
||||
rows, err := s.pool.Query(ctx, `SELECT scope,event_id,team_id,kind,discord_role_id,role_name,hoist
|
||||
FROM discord_managed_roles ORDER BY scope,event_id,team_id,kind`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
roles := make([]application.DiscordManagedRole, 0)
|
||||
for rows.Next() {
|
||||
var role application.DiscordManagedRole
|
||||
if err = rows.Scan(&role.Scope, &role.EventID, &role.TeamID, &role.Kind, &role.DiscordRoleID, &role.RoleName, &role.Hoist); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
roles = append(roles, role)
|
||||
}
|
||||
return roles, rows.Err()
|
||||
}
|
||||
|
||||
func (s *Store) UpsertDiscordManagedRole(ctx context.Context, role application.DiscordManagedRole) error {
|
||||
_, err := s.pool.Exec(ctx, `INSERT INTO discord_managed_roles(scope,event_id,team_id,kind,discord_role_id,role_name,hoist)
|
||||
VALUES($1,$2,$3,$4,$5,$6,$7)
|
||||
@@ -144,6 +169,24 @@ func (s *Store) DeleteDiscordRoleAssignment(ctx context.Context, discordRoleID,
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *Store) SetDiscordRoleAssignments(ctx context.Context, discordRoleID string, discordUserIDs []string) error {
|
||||
tx, err := s.pool.Begin(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
if _, err = tx.Exec(ctx, `DELETE FROM discord_role_assignments WHERE discord_role_id=$1`, discordRoleID); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, userID := range discordUserIDs {
|
||||
if _, err = tx.Exec(ctx, `INSERT INTO discord_role_assignments(discord_role_id,discord_user_id)
|
||||
VALUES($1,$2)`, discordRoleID, userID); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return tx.Commit(ctx)
|
||||
}
|
||||
|
||||
func (s *Store) GetDiscordRoleRoster(ctx context.Context, eventID string) (application.DiscordRoleRoster, error) {
|
||||
roster, err := s.GetRoster(ctx, eventID)
|
||||
if err != nil {
|
||||
@@ -209,6 +252,36 @@ func (s *Store) GetDiscordRoleRegistration(ctx context.Context, eventID string)
|
||||
return application.DiscordRoleRegistration{Event: event, Registrations: registrations, PlayerDiscordIDs: discordIDs}, nil
|
||||
}
|
||||
|
||||
func (s *Store) ListActiveDiscordRoleRegistrations(ctx context.Context) ([]application.DiscordRoleRegistration, error) {
|
||||
rows, err := s.pool.Query(ctx, `SELECT id FROM events
|
||||
WHERE state NOT IN ('Completed','Cancelled') ORDER BY id`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
eventIDs := make([]string, 0)
|
||||
for rows.Next() {
|
||||
var eventID string
|
||||
if err = rows.Scan(&eventID); err != nil {
|
||||
rows.Close()
|
||||
return nil, err
|
||||
}
|
||||
eventIDs = append(eventIDs, eventID)
|
||||
}
|
||||
rows.Close()
|
||||
if err = rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := make([]application.DiscordRoleRegistration, 0, len(eventIDs))
|
||||
for _, eventID := range eventIDs {
|
||||
registration, registrationErr := s.GetDiscordRoleRegistration(ctx, eventID)
|
||||
if registrationErr != nil {
|
||||
return nil, registrationErr
|
||||
}
|
||||
out = append(out, registration)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (s *Store) discordRoleRoster(ctx context.Context, roster domain.RosterDraft) (application.DiscordRoleRoster, error) {
|
||||
playerIDs := make([]string, 0)
|
||||
for _, team := range roster.Teams {
|
||||
|
||||
@@ -125,6 +125,29 @@ func TestDiscordRoleSyncPersistence(t *testing.T) {
|
||||
if status != "completed" || warning != "warning" {
|
||||
t.Fatalf("unexpected completed job state: status=%s warning=%s", status, warning)
|
||||
}
|
||||
bucket := time.Now().UnixNano()
|
||||
defer func() {
|
||||
_, _ = store.pool.Exec(ctx, `DELETE FROM discord_role_sync_jobs
|
||||
WHERE event_id='__global__' AND action='full_reconcile' AND generation=$1`, bucket)
|
||||
}()
|
||||
if err = store.ScheduleGlobalDiscordRoleSync(ctx, bucket); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = store.ScheduleGlobalDiscordRoleSync(ctx, bucket); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var bucketJobs int
|
||||
if err = store.pool.QueryRow(ctx, `SELECT count(*) FROM discord_role_sync_jobs
|
||||
WHERE event_id='__global__' AND action='full_reconcile' AND generation=$1`, bucket).Scan(&bucketJobs); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if bucketJobs != 1 {
|
||||
t.Fatalf("expected one global bucket job, got %d", bucketJobs)
|
||||
}
|
||||
job, err = store.ClaimDiscordRoleSyncJob(ctx)
|
||||
if err != nil || job.Action != application.DiscordRoleActionFullReconcile || job.Generation != bucket {
|
||||
t.Fatalf("unexpected global job: %+v err=%v", job, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFullScrimPipeline(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user