Add invalid link reset worker

This commit is contained in:
Mukhtar Akere
2025-04-08 17:48:01 +01:00
parent 4b5e18df94
commit 9011420ac3
3 changed files with 49 additions and 2 deletions

View File

@@ -169,7 +169,6 @@ func (c *Cache) refreshDownloadLink(link string) error {
return nil
}
func (c *Cache) resetDownloadLinks() {
func (c *Cache) resetInvalidLinks() {
c.invalidDownloadLinks = xsync.NewMapOf[string, string]()
c.downloadLinks = xsync.NewMapOf[string, downloadLinkCache]()
}

View File

@@ -6,6 +6,7 @@ func (c *Cache) Refresh() error {
// For now, we just want to refresh the listing and download links
go c.refreshDownloadLinksWorker()
go c.refreshTorrentsWorker()
go c.resetInvalidLinksWorker()
return nil
}
@@ -26,3 +27,49 @@ func (c *Cache) refreshTorrentsWorker() {
c.refreshTorrents()
}
}
func (c *Cache) resetInvalidLinksWorker() {
// Calculate time until next 12:00 CET
now := time.Now()
loc, err := time.LoadLocation("CET")
if err != nil {
// Fallback if CET timezone can't be loaded
c.logger.Error().Err(err).Msg("Failed to load CET timezone, using local time")
loc = time.Local
}
nowInCET := now.In(loc)
next := time.Date(
nowInCET.Year(),
nowInCET.Month(),
nowInCET.Day(),
12, 0, 0, 0,
loc,
)
// If it's already past 12:00 CET today, schedule for tomorrow
if nowInCET.After(next) {
next = next.Add(24 * time.Hour)
}
// Duration until next 12:00 CET
initialWait := next.Sub(nowInCET)
// Set up initial timer
timer := time.NewTimer(initialWait)
defer timer.Stop()
c.logger.Debug().Msgf("Scheduled invalid links reset at %s (in %s)", next.Format("2006-01-02 15:04:05 MST"), initialWait)
// Wait for the first execution
<-timer.C
c.resetInvalidLinks()
// Now set up the daily ticker
refreshTicker := time.NewTicker(24 * time.Hour)
defer refreshTicker.Stop()
for range refreshTicker.C {
c.resetInvalidLinks()
}
}