fix bugs; move to gocron for scheduled jobs
This commit is contained in:
+58
-89
@@ -1,109 +1,78 @@
|
||||
package debrid
|
||||
|
||||
import "time"
|
||||
import (
|
||||
"context"
|
||||
"github.com/sirrobot01/decypharr/internal/utils"
|
||||
"time"
|
||||
)
|
||||
|
||||
func (c *Cache) Refresh() error {
|
||||
func (c *Cache) StartSchedule() error {
|
||||
// For now, we just want to refresh the listing and download links
|
||||
go c.refreshDownloadLinksWorker()
|
||||
go c.refreshTorrentsWorker()
|
||||
go c.resetInvalidLinksWorker()
|
||||
go c.cleanupWorker()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Cache) refreshDownloadLinksWorker() {
|
||||
refreshTicker := time.NewTicker(c.downloadLinksRefreshInterval)
|
||||
defer refreshTicker.Stop()
|
||||
|
||||
for range refreshTicker.C {
|
||||
c.refreshDownloadLinks()
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Cache) refreshTorrentsWorker() {
|
||||
refreshTicker := time.NewTicker(c.torrentRefreshInterval)
|
||||
defer refreshTicker.Stop()
|
||||
|
||||
for range refreshTicker.C {
|
||||
c.refreshTorrents()
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Cache) resetInvalidLinksWorker() {
|
||||
// Calculate time until next 00:00 CET
|
||||
now := time.Now()
|
||||
loc, err := time.LoadLocation("CET")
|
||||
ctx := context.Background()
|
||||
downloadLinkJob, err := utils.ScheduleJob(ctx, c.downloadLinksRefreshInterval, nil, c.refreshDownloadLinks)
|
||||
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
|
||||
c.logger.Error().Err(err).Msg("Failed to add download link refresh job")
|
||||
}
|
||||
if t, err := downloadLinkJob.NextRun(); err == nil {
|
||||
c.logger.Trace().Msgf("Next download link refresh job: %s", t.Format("2006-01-02 15:04:05"))
|
||||
}
|
||||
|
||||
nowInCET := now.In(loc)
|
||||
next := time.Date(
|
||||
nowInCET.Year(),
|
||||
nowInCET.Month(),
|
||||
nowInCET.Day(),
|
||||
0, 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)
|
||||
torrentJob, err := utils.ScheduleJob(ctx, c.torrentRefreshInterval, nil, c.refreshTorrents)
|
||||
if err != nil {
|
||||
c.logger.Error().Err(err).Msg("Failed to add torrent refresh job")
|
||||
}
|
||||
if t, err := torrentJob.NextRun(); err == nil {
|
||||
c.logger.Trace().Msgf("Next torrent refresh job: %s", t.Format("2006-01-02 15:04:05"))
|
||||
}
|
||||
|
||||
// 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 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()
|
||||
// Schedule the reset invalid links job
|
||||
// This job will run every 24 hours
|
||||
// and reset the invalid links in the cache
|
||||
cet, _ := time.LoadLocation("CET")
|
||||
resetLinksJob, err := utils.ScheduleJob(ctx, "00:00", cet, c.resetInvalidLinks)
|
||||
if err != nil {
|
||||
c.logger.Error().Err(err).Msg("Failed to add reset invalid links job")
|
||||
}
|
||||
if t, err := resetLinksJob.NextRun(); err == nil {
|
||||
c.logger.Trace().Msgf("Next reset invalid download links job at: %s", t.Format("2006-01-02 15:04:05"))
|
||||
}
|
||||
|
||||
// Schedule the cleanup job
|
||||
|
||||
cleanupJob, err := utils.ScheduleJob(ctx, "1h", nil, c.cleanupWorker)
|
||||
if err != nil {
|
||||
c.logger.Error().Err(err).Msg("Failed to add cleanup job")
|
||||
}
|
||||
if t, err := cleanupJob.NextRun(); err == nil {
|
||||
c.logger.Trace().Msgf("Next cleanup job at: %s", t.Format("2006-01-02 15:04:05"))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Cache) cleanupWorker() {
|
||||
// Cleanup every hour
|
||||
// Removes deleted torrents from the cache
|
||||
torrents, err := c.client.GetTorrents()
|
||||
if err != nil {
|
||||
c.logger.Error().Err(err).Msg("Failed to get torrents")
|
||||
return
|
||||
}
|
||||
|
||||
ticker := time.NewTicker(1 * time.Hour)
|
||||
idStore := make(map[string]struct{})
|
||||
for _, t := range torrents {
|
||||
idStore[t.Id] = struct{}{}
|
||||
}
|
||||
|
||||
for range ticker.C {
|
||||
torrents, err := c.client.GetTorrents()
|
||||
if err != nil {
|
||||
c.logger.Error().Err(err).Msg("Failed to get torrents")
|
||||
continue
|
||||
deletedTorrents := make([]string, 0)
|
||||
c.torrents.Range(func(key string, _ *CachedTorrent) bool {
|
||||
if _, exists := idStore[key]; !exists {
|
||||
deletedTorrents = append(deletedTorrents, key)
|
||||
}
|
||||
return true
|
||||
})
|
||||
|
||||
idStore := make(map[string]struct{})
|
||||
for _, t := range torrents {
|
||||
idStore[t.Id] = struct{}{}
|
||||
}
|
||||
|
||||
deletedTorrents := make([]string, 0)
|
||||
c.torrents.Range(func(key string, _ *CachedTorrent) bool {
|
||||
if _, exists := idStore[key]; !exists {
|
||||
deletedTorrents = append(deletedTorrents, key)
|
||||
}
|
||||
return true
|
||||
})
|
||||
|
||||
if len(deletedTorrents) > 0 {
|
||||
c.DeleteTorrents(deletedTorrents)
|
||||
c.logger.Info().Msgf("Deleted %d torrents", len(deletedTorrents))
|
||||
}
|
||||
if len(deletedTorrents) > 0 {
|
||||
c.DeleteTorrents(deletedTorrents)
|
||||
c.logger.Info().Msgf("Deleted %d torrents", len(deletedTorrents))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user