Skip to content

Commit

Permalink
Merge pull request #916 from JustaPenguin/scheduled-race-detect-fix
Browse files Browse the repository at this point in the history
stop and clear existing race start schedules and notification reminder timers when scheduling races
  • Loading branch information
cj123 authored May 21, 2020
2 parents d0fa775 + 55c17fa commit 0382d61
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
3 changes: 2 additions & 1 deletion race_custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
)

type CustomRace struct {
ScheduledEvents map[ServerID]*ScheduledEventBase
ScheduledEventBase

Name string
Expand All @@ -35,6 +34,8 @@ type CustomRace struct {

RaceConfig CurrentRaceConfig
EntryList EntryList

ScheduledEvents map[ServerID]*ScheduledEventBase
}

func (cr *CustomRace) GetRaceConfig() CurrentRaceConfig {
Expand Down
37 changes: 26 additions & 11 deletions race_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,14 @@ func NewRaceManager(
raceControl *RaceControl,
) *RaceManager {
return &RaceManager{
store: store,
process: process,
carManager: carManager,
trackManager: trackManager,
notificationManager: notificationManager,
raceControl: raceControl,
store: store,
process: process,
carManager: carManager,
trackManager: trackManager,
notificationManager: notificationManager,
raceControl: raceControl,
customRaceStartTimers: make(map[string]*when.Timer),
customRaceReminderTimers: make(map[string]*when.Timer),
}
}

Expand Down Expand Up @@ -1615,9 +1617,6 @@ func (rm *RaceManager) clearLoopedRaceSessionTypes() {
}

func (rm *RaceManager) InitScheduledRaces() error {
rm.customRaceStartTimers = make(map[string]*when.Timer)
rm.customRaceReminderTimers = make(map[string]*when.Timer)

races, err := rm.store.ListCustomRaces()

if err != nil {
Expand All @@ -1644,7 +1643,25 @@ func (rm *RaceManager) InitScheduledRaces() error {
continue
}

newScheduledEvent := false

if timer, ok := rm.customRaceStartTimers[race.UUID.String()]; ok {
timer.Stop()
delete(rm.customRaceStartTimers, race.UUID.String())
} else {
newScheduledEvent = true
}

if timer, ok := rm.customRaceReminderTimers[race.UUID.String()]; ok {
timer.Stop()
delete(rm.customRaceReminderTimers, race.UUID.String())
}

if race.Scheduled.After(time.Now()) {
if newScheduledEvent {
logrus.Infof("Adding new event (%s) to scheduled start timers, starts at %s", race.Name, race.Scheduled.String())
}

// add a scheduled event on date
rm.customRaceStartTimers[race.UUID.String()], err = when.When(race.Scheduled, func() {
err := rm.StartScheduledRace(race)
Expand All @@ -1656,8 +1673,6 @@ func (rm *RaceManager) InitScheduledRaces() error {

if err != nil {
logrus.WithError(err).Error("Could not set up scheduled race timer")
} else {
logrus.Infof("Added new race (%s) to scheduled start timers, starts at %s", race.Name, race.Scheduled.String())
}

if rm.notificationManager.HasNotificationReminders() {
Expand Down

0 comments on commit 0382d61

Please sign in to comment.