From 2fed7f2d2e9a359be7dda701d1099434da6b74c8 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Thu, 11 Apr 2024 17:34:49 +0300 Subject: [PATCH] Use static start time if time zone is invalid --- historysync.go | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/historysync.go b/historysync.go index a7946b6..dba452a 100644 --- a/historysync.go +++ b/historysync.go @@ -250,26 +250,33 @@ func (portal *Portal) legacyBackfill(ctx context.Context, user *User) { } func (user *User) dailyMediaRequestLoop() { - // Calculate when to do the first set of media retry requests - now := time.Now() - userTz, err := time.LoadLocation(user.Timezone) - if err != nil { - userTz = now.Local().Location() - } - tonightMidnight := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, userTz) - midnightOffset := time.Duration(user.bridge.Config.Bridge.HistorySync.MediaRequests.RequestLocalTime) * time.Minute - requestStartTime := tonightMidnight.Add(midnightOffset) - - // If the request time for today has already happened, we need to start the - // request loop tomorrow instead. - if requestStartTime.Before(now) { - requestStartTime = requestStartTime.AddDate(0, 0, 1) - } log := user.zlog.With(). Str("action", "daily media request loop"). Logger() ctx := log.WithContext(context.Background()) + // Calculate when to do the first set of media retry requests + now := time.Now() + userTz, err := time.LoadLocation(user.Timezone) + tzIsInvalid := err != nil && user.Timezone != "" + var requestStartTime time.Time + if tzIsInvalid { + requestStartTime = now.Add(8 * time.Hour) + log.Warn().Msg("Invalid time zone, using static 8 hour start time") + } else { + if userTz == nil { + userTz = now.Local().Location() + } + tonightMidnight := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, userTz) + midnightOffset := time.Duration(user.bridge.Config.Bridge.HistorySync.MediaRequests.RequestLocalTime) * time.Minute + requestStartTime = tonightMidnight.Add(midnightOffset) + // If the request time for today has already happened, we need to start the + // request loop tomorrow instead. + if requestStartTime.Before(now) { + requestStartTime = requestStartTime.AddDate(0, 0, 1) + } + } + // Wait to start the loop log.Info().Time("start_loop_at", requestStartTime).Msg("Waiting until start time to do media retry requests") time.Sleep(time.Until(requestStartTime))