Skip to content

Commit 6323416

Browse files
committed
fix:scheduler is shutdown on bat worker side not api side
1 parent b3bf9b7 commit 6323416

6 files changed

Lines changed: 30 additions & 7 deletions

File tree

apps/api/cmd/BAT_worker/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ func main() {
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/internal/api/api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ func (api *API) setupRoutes(mw *mw.Middleware) {
144144
r.With(ensureEventAdmin).Post("/calc-admissions", api.Handlers.Admission.HandleCalculateAdmissionsRequest)
145145
r.With(ensureEventAdmin).Patch("/transition-waitlisted-applications", api.Handlers.Application.TransitionWaitlistedApplications)
146146
r.With(ensureEventAdmin).Post("/begin-waitlist-transition", api.Handlers.Bat.QueueScheduleWaitlistTransitionTask)
147-
r.With(ensureEventAdmin).Post("/shutdown-waitlist-scheduler", api.Handlers.Bat.ShutdownWaitlistScheduler)
147+
r.With(ensureEventAdmin).Post("/shutdown-waitlist-scheduler", api.Handlers.Bat.QueueShutdownWaitlistSchedulerTask)
148148
r.With(ensureEventAdmin).Post("/reviews/bat-runs/{runId}/release", api.Handlers.Admission.ReleaseDecisions)
149149
r.With(ensureEventAdmin).Patch("/", api.Handlers.Event.UpdateEventById)
150150
r.With(ensureEventAdmin).Post("/banner", api.Handlers.Event.UploadEventBanner)

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ func (h *BatHandler) QueueScheduleWaitlistTransitionTask(w http.ResponseWriter,
179179
res.Send(w, http.StatusCreated, nil)
180180
}
181181

182-
// Shutdown scheduler
182+
// Queue Shutdown scheduler task
183183
//
184184
// @Summary Shutsdown an asynq scheduler
185185
// @Description Shutsdown the scheduler used for the waitlist transition task. Error returned through logs if a scheduler is not active.
@@ -188,8 +188,8 @@ func (h *BatHandler) QueueScheduleWaitlistTransitionTask(w http.ResponseWriter,
188188
// @Success 200 "Scheduler shutdown successfully"
189189
// @Failure 500 {object} res.ErrorResponse "Server error: failed to shutdown scheduler"
190190
// @Router /events/{eventId}/queue-transition-waitlist-task [post]
191-
func (h *BatHandler) ShutdownWaitlistScheduler(w http.ResponseWriter, r *http.Request) {
192-
err := h.BatService.ShutdownWaitlistScheduler()
191+
func (h *BatHandler) QueueShutdownWaitlistSchedulerTask(w http.ResponseWriter, r *http.Request) {
192+
err := h.BatService.QueueShutdownWaitlistScheduler()
193193
if err != nil {
194194
res.SendError(w, http.StatusInternalServerError, res.NewError("internal_err", "Failed to shutdown scheduler."))
195195
}

apps/api/internal/services/bat.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -384,9 +384,19 @@ func (s *BatService) QueueScheduleWaitlistTransitionTask(ctx context.Context, ev
384384
return nil
385385
}
386386

387-
func (s *BatService) ShutdownWaitlistScheduler() error {
388-
s.scheduler.Shutdown()
389-
// Error is output by logging.
387+
func (s *BatService) QueueShutdownWaitlistScheduler() error {
388+
task, err := tasks.NewTaskShutdownScheduler()
389+
if err != nil {
390+
s.logger.Err(err).Msg("Failed to create ShutdownWaitlistScheduler task")
391+
return err
392+
}
393+
394+
_, err = s.taskQueue.Enqueue(task, asynq.Queue("bat"))
395+
if err != nil {
396+
s.logger.Err(err).Msg("Failed to queue ShutdownWaitlistScheduler task")
397+
return err
398+
}
399+
s.logger.Info().Msg("Queued ShutdownWaitlistScheduler task")
390400

391401
return nil
392402
}

apps/api/internal/tasks/bat.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const (
1111
TypeCalculateAdmissions = "admissions:calculate"
1212
TypeScheduleTransitionWaitlist = "waitlist:scheduletransition"
1313
TypeTransitionWaitlist = "waitlist:transition"
14+
TypeShutdownScheduler = "waitlist:shutdownscheduler"
1415
)
1516

1617
type CalculateAdmissionsPayload struct {
@@ -55,3 +56,7 @@ func NewTaskTransitionWaitlist(payload TransitionWaitlistPayload) (*asynq.Task,
5556

5657
return asynq.NewTask(TypeTransitionWaitlist, data), nil
5758
}
59+
60+
func NewTaskShutdownScheduler() (*asynq.Task, error) {
61+
return asynq.NewTask(TypeShutdownScheduler, nil), nil
62+
}

apps/api/internal/workers/bat.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,10 @@ func (w *BATWorker) HandleTransitionWaitlistTask(ctx context.Context, t *asynq.T
106106

107107
return nil
108108
}
109+
110+
func (w *BATWorker) HandleShutdownScheduler(ctx context.Context, t *asynq.Task) error {
111+
w.scheduler.Shutdown()
112+
// Error returned by logging.
113+
114+
return nil
115+
}

0 commit comments

Comments
 (0)