Skip to content

Commit

Permalink
fix(runtime-watcher): make Stop() indempotent
Browse files Browse the repository at this point in the history
  • Loading branch information
hspedro committed Feb 27, 2025
1 parent 4ced805 commit 2bfeb98
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions internal/core/worker/runtimewatcher/runtime_watcher_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ func (w *runtimeWatcherWorker) spawnUpdateRoomWatchers(resultChan chan game_room
case event, ok := <-resultChan:
if !ok {
w.logger.Warn("resultChan closed, finishing worker goroutine")
w.Stop(w.ctx)
return
}
err := w.processEvent(w.ctx, event)
Expand All @@ -115,7 +116,7 @@ func (w *runtimeWatcherWorker) mitigateDisruptions() error {
zap.String("scheduler", w.scheduler.Name),
zap.Error(err),
)
return nil
return err
}
mitigationQuota := 0
if totalRoomsAmount >= MinRoomsToApplyDisruption {
Expand All @@ -126,7 +127,7 @@ func (w *runtimeWatcherWorker) mitigateDisruptions() error {
zap.String("scheduler", w.scheduler.Name),
zap.Error(err),
)
return nil
return err
}
}
err = w.runtime.MitigateDisruption(w.ctx, w.scheduler, mitigationQuota, w.config.DisruptionSafetyPercentage)
Expand Down Expand Up @@ -196,9 +197,13 @@ func (w *runtimeWatcherWorker) Start(ctx context.Context) error {
}

func (w *runtimeWatcherWorker) Stop(_ context.Context) {
w.logger.Info("stopping runtime watcher", zap.String("scheduler", w.scheduler.Name))
if w.cancelFunc != nil {
w.cancelFunc()
w.logger.Info("stopping runtime watcher", zap.String("scheduler", w.scheduler.Name))
// To make this function idempotent, we need to set the cancelFunc to nil
// before calling it.
cancelFunc := w.cancelFunc
w.cancelFunc = nil
cancelFunc()
}
}

Expand Down

0 comments on commit 2bfeb98

Please sign in to comment.