Skip to content

Commit 26fa847

Browse files
authored
Merge pull request #267 from swamphacks/feat/waitlist-transition
Major waitlist transition fixes
2 parents b8693bd + 2840d6e commit 26fa847

16 files changed

Lines changed: 130 additions & 49 deletions

File tree

apps/api/.env.dev.example

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ DATABASE_URL_MIGRATION="postgres://postgres:postgres@localhost:5432/coredb"
33
REDIS_URL="redis://redis:6379"
44
ALLOWED_ORIGINS="" # URLs in comma seperated list
55

6+
# Application waitlist
7+
MAX_ACCEPTED_APPLICATIONS=500
8+
ACCEPT_FROM_WAITLIST_COUNT=50
9+
ACCEPT_FROM_WAITLIST_PERIOD="@every 72h"
10+
611
# For OAuth
712
AUTH_DISCORD_CLIENT_ID=
813
AUTH_DISCORD_CLIENT_SECRET=

apps/api/cmd/BAT_worker/main.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,16 @@ func main() {
8282

8383
sesClient := email.NewSESClient(cfg.AWS.AccessKey, cfg.AWS.AccessKeySecret, cfg.AWS.Region, logger)
8484
emailService := services.NewEmailService(taskQueueClient, sesClient, logger)
85-
batService := services.NewBatService(applicationRepo, eventRepo, userRepo, batRunsRepo, emailService, txm, nil, logger)
85+
batService := services.NewBatService(applicationRepo, eventRepo, userRepo, batRunsRepo, emailService, txm, nil, scheduler, logger)
8686
applicationService := services.NewApplicationService(applicationRepo, userRepo, eventService, emailService, txm, nil, nil, scheduler, logger)
8787

88-
BATWorker := workers.NewBATWorker(batService, applicationService, scheduler, logger)
88+
BATWorker := workers.NewBATWorker(batService, applicationService, scheduler, taskQueueClient, logger)
8989

9090
mux := asynq.NewServeMux()
9191
mux.HandleFunc(tasks.TypeCalculateAdmissions, BATWorker.HandleCalculateAdmissionsTask)
9292
mux.HandleFunc(tasks.TypeTransitionWaitlist, BATWorker.HandleTransitionWaitlistTask)
9393
mux.HandleFunc(tasks.TypeScheduleTransitionWaitlist, BATWorker.HandleScheduleTransitionWaitlistTask)
94+
mux.HandleFunc(tasks.TypeShutdownScheduler, BATWorker.HandleShutdownScheduler)
9495

9596
if err := srv.Run(mux); err != nil {
9697
logger.Fatal().Msg("Failed to run BAT worker")

apps/api/cmd/api/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func main() {
9191
emailService := services.NewEmailService(taskQueueClient, sesClient, logger)
9292
applicationService := services.NewApplicationService(applicationRepo, userRepo, eventService, emailService, txm, r2Client, &cfg.CoreBuckets, nil, logger)
9393
teamService := services.NewTeamService(teamRepo, teamMemberRepo, teamJoinRequestRepo, eventRepo, txm, logger)
94-
batService := services.NewBatService(applicationRepo, eventRepo, userRepo, batRunsRepo, emailService, txm, taskQueueClient, logger)
94+
batService := services.NewBatService(applicationRepo, eventRepo, userRepo, batRunsRepo, emailService, txm, taskQueueClient, nil, logger)
9595

9696
// Injections into handlers
9797
apiHandlers := handlers.NewHandlers(authService, userService, eventInterestService, eventService, emailService, applicationService, teamService, batService, cfg, logger)

apps/api/internal/api/api.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,8 @@ func (api *API) setupRoutes(mw *mw.Middleware) {
143143
r.With(ensureEventAdmin).Post("/queue-confirmation-email", api.Handlers.Email.QueueConfirmationEmail)
144144
r.With(ensureEventAdmin).Post("/calc-admissions", api.Handlers.Admission.HandleCalculateAdmissionsRequest)
145145
r.With(ensureEventAdmin).Patch("/transition-waitlisted-applications", api.Handlers.Application.TransitionWaitlistedApplications)
146-
r.With(ensureEventAdmin).Post("/queue-transition-waitlist-task", api.Handlers.Bat.QueueScheduleWaitlistTransitionTask)
146+
r.With(ensureEventAdmin).Post("/begin-waitlist-transition", api.Handlers.Bat.QueueScheduleWaitlistTransitionTask)
147+
r.With(ensureEventAdmin).Post("/shutdown-waitlist-scheduler", api.Handlers.Bat.QueueShutdownWaitlistSchedulerTask)
147148
r.With(ensureEventAdmin).Post("/reviews/bat-runs/{runId}/release", api.Handlers.Admission.ReleaseDecisions)
148149
r.With(ensureEventAdmin).Patch("/", api.Handlers.Event.UpdateEventById)
149150
r.With(ensureEventAdmin).Post("/banner", api.Handlers.Event.UploadEventBanner)

apps/api/internal/api/handlers/bat.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,3 +178,21 @@ func (h *BatHandler) QueueScheduleWaitlistTransitionTask(w http.ResponseWriter,
178178

179179
res.Send(w, http.StatusCreated, nil)
180180
}
181+
182+
// Queue Shutdown scheduler task
183+
//
184+
// @Summary Shutsdown an asynq scheduler
185+
// @Description Shutsdown the scheduler used for the waitlist transition task. Error returned through logs if a scheduler is not active.
186+
// @Tags
187+
//
188+
// @Success 200 "Scheduler shutdown successfully"
189+
// @Failure 500 {object} res.ErrorResponse "Server error: failed to shutdown scheduler"
190+
// @Router /events/{eventId}/queue-transition-waitlist-task [post]
191+
func (h *BatHandler) QueueShutdownWaitlistSchedulerTask(w http.ResponseWriter, r *http.Request) {
192+
err := h.BatService.QueueShutdownWaitlistScheduler()
193+
if err != nil {
194+
res.SendError(w, http.StatusInternalServerError, res.NewError("internal_err", "Failed to shutdown scheduler."))
195+
}
196+
197+
res.Send(w, http.StatusOK, nil)
198+
}

apps/api/internal/config/config.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,15 @@ type CoreBuckets struct {
5555
}
5656

5757
type Config struct {
58-
DatabaseURL string `env:"DATABASE_URL"`
59-
RedisURL string `env:"REDIS_URL"`
60-
Port string `env:"PORT" envDefault:"8080"`
61-
AllowedOriginsString string `env:"ALLOWED_ORIGINS"`
62-
EmailTemplateDirectory string `env:"EMAIL_TEMPLATE_DIRECTORY"`
63-
AllowedOrigins []string ``
64-
ApiOrigin string `env:"API_ORIGIN"`
65-
WaitlistWorkerSessionIdCookie string `env:WAITLIST_WORKER_SESSION_ID_COOKIE`
58+
DatabaseURL string `env:"DATABASE_URL"`
59+
RedisURL string `env:"REDIS_URL"`
60+
Port string `env:"PORT" envDefault:"8080"`
61+
AllowedOriginsString string `env:"ALLOWED_ORIGINS"`
62+
EmailTemplateDirectory string `env:"EMAIL_TEMPLATE_DIRECTORY"`
63+
AllowedOrigins []string ``
64+
MaxAcceptedApplications uint32 `env:"MAX_ACCEPTED_APPLICATIONS"`
65+
AcceptFromWaitlistCount uint32 `env:"ACCEPT_FROM_WAITLIST_COUNT"`
66+
AcceptFromWaitlistPeriod string `env:"ACCEPT_FROM_WAITLIST_PERIOD"`
6667

6768
Auth AuthConfig `envPrefix:"AUTH_"`
6869
Cookie CookieConfig `envPrefix:"COOKIE_"`

apps/api/internal/db/queries/applications.sql

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,12 @@ UPDATE applications
104104
SET waitlist_join_time = COALESCE(waitlist_join_time, NOW()),
105105
status = 'waitlisted'
106106
WHERE event_id = @event_id::uuid
107-
AND status = 'accepted';
107+
AND status = 'accepted'
108+
AND user_id IN (
109+
SELECT user_id from event_roles AS er
110+
WHERE er.role = 'applicant'
111+
)
112+
;
108113

109114
-- name: TransitionWaitlistedApplicationsToAcceptedByEventID :many
110115
UPDATE applications
@@ -119,7 +124,3 @@ WHERE user_id IN (
119124
)
120125
RETURNING user_id;
121126

122-
-- name: GetTotalAcceptedApplicationsByEventId :one
123-
SELECT COUNT(*) FROM applications
124-
WHERE event_id = @event_id::uuid
125-
AND status = 'accepted';

apps/api/internal/db/queries/event_roles.sql

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,9 @@ WHERE er.event_id = $1;
2424
-- name: UpdateRole :exec
2525
UPDATE event_roles
2626
SET role = $3
27-
WHERE event_id = $1 AND user_id = $2;
27+
WHERE event_id = $1 AND user_id = $2;
28+
29+
-- name: GetAttendeeCountByEventId :one
30+
SELECT COUNT(*) FROM event_roles AS er
31+
WHERE er.event_id = @event_id::uuid
32+
AND er.role = 'attendee';

apps/api/internal/db/repository/application.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ func (r *ApplicationRepository) TransitionWaitlistedApplicationsToAcceptedByEven
211211
})
212212
}
213213

214-
func (r *ApplicationRepository) GetTotalAcceptedApplicationsByEventId(ctx context.Context, eventId uuid.UUID) (uint32, error) {
215-
amount, err := r.db.Query.GetTotalAcceptedApplicationsByEventId(ctx, eventId)
214+
func (r *ApplicationRepository) GetAttendeeCountByEventId(ctx context.Context, eventId uuid.UUID) (uint32, error) {
215+
amount, err := r.db.Query.GetAttendeeCountByEventId(ctx, eventId)
216216
return uint32(amount), err
217217
}

apps/api/internal/db/sqlc/applications.sql.go

Lines changed: 4 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)