From 3df201cc206ce50be548c1c1218a58b337cf692b Mon Sep 17 00:00:00 2001 From: Ben Sarmiento Date: Sat, 2 Dec 2023 20:37:40 +0100 Subject: [PATCH] Fixes --- internal/config/types.go | 9 ++++++++ internal/dav/delete.go | 3 ++- internal/torrent/hooks.go | 1 + internal/torrent/manager.go | 42 ++++++++++++++++++++++++++----------- 4 files changed, 42 insertions(+), 13 deletions(-) diff --git a/internal/config/types.go b/internal/config/types.go index 1518be2..913ba9e 100644 --- a/internal/config/types.go +++ b/internal/config/types.go @@ -22,6 +22,7 @@ type ConfigInterface interface { GetRealDebridTimeout() int GetRetriesUntilFailed() int EnableDownloadCache() bool + GetRateLimitSleepSeconds() int } type ZurgConfig struct { @@ -34,6 +35,7 @@ type ZurgConfig struct { CanRepair bool `yaml:"enable_repair"` OnLibraryUpdate string `yaml:"on_library_update"` NetworkBufferSize int `yaml:"network_buffer_size"` + RateLimitSleepSeconds int `yaml:"rate_limit_sleep_secs"` RetainFolderNameExtension bool `yaml:"retain_folder_name_extension"` RetainRDTorrentName bool `yaml:"retain_rd_torrent_name"` PreferredHosts []string `yaml:"preferred_hosts"` @@ -132,3 +134,10 @@ func (z *ZurgConfig) GetRealDebridTimeout() int { } return z.RealDebridTimeout } + +func (z *ZurgConfig) GetRateLimitSleepSeconds() int { + if z.RateLimitSleepSeconds == 0 { + return 4 + } + return z.RateLimitSleepSeconds +} diff --git a/internal/dav/delete.go b/internal/dav/delete.go index 24413f1..12ba8c3 100644 --- a/internal/dav/delete.go +++ b/internal/dav/delete.go @@ -35,7 +35,8 @@ func HandleDeleteFile(directory, torrentName, fileName string, t *torrent.Torren if t.CheckDeletedState(torrent) { t.Delete(torrentName, true, true) } else { - t.UpdateTorrentResponseCache(torrent) + updatedPaths := t.UpdateTorrentResponseCache(torrent) + t.TriggerHookOnLibraryUpdate(updatedPaths) } return nil } diff --git a/internal/torrent/hooks.go b/internal/torrent/hooks.go index 11c846d..a8c728f 100644 --- a/internal/torrent/hooks.go +++ b/internal/torrent/hooks.go @@ -46,5 +46,6 @@ func OnLibraryUpdateHook(paths []string, config config.ConfigInterface, log *zap log.Errorf("Failed to execute hook on_library_update: %v", err) return } + log.Infof("Successfully executed hook on_library_update") // No need to log the output since we're not capturing it } diff --git a/internal/torrent/manager.go b/internal/torrent/manager.go index e547b0f..e8ae859 100644 --- a/internal/torrent/manager.go +++ b/internal/torrent/manager.go @@ -117,11 +117,11 @@ func NewTorrentManager(cfg config.ConfigInterface, api *realdebrid.RealDebrid, p return t } -func (t *TorrentManager) RefreshTorrents() { +func (t *TorrentManager) RefreshTorrents() []string { instances, _, err := t.Api.GetTorrents(0) if err != nil { t.log.Warnf("Cannot get torrents: %v\n", err) - return + return nil } infoChan := make(chan *Torrent, len(instances)) var wg sync.WaitGroup @@ -155,18 +155,21 @@ func (t *TorrentManager) RefreshTorrents() { } t.log.Infof("Compiled into %d torrents, %d were missing info", oldTorrents.Count(), noInfoCount) + var updatedPaths []string somethingChanged := false // removed strset.Difference(t.accessKeySet, freshKeys).Each(func(accessKey string) bool { somethingChanged = true - t.Delete(accessKey, false, false) + torrentPaths := t.Delete(accessKey, false, false) + updatedPaths = append(updatedPaths, torrentPaths...) return true }) // new strset.Difference(freshKeys, t.accessKeySet).Each(func(accessKey string) bool { somethingChanged = true torrent, _ := oldTorrents.Get(accessKey) - t.UpdateTorrentResponseCache(torrent) + torrentPaths := t.UpdateTorrentResponseCache(torrent) + updatedPaths = append(updatedPaths, torrentPaths...) t.accessKeySet.Add(accessKey) return true }) @@ -177,10 +180,7 @@ func (t *TorrentManager) RefreshTorrents() { t.SetNewLatestState(t.getCurrentState()) - // todo: work on hook - // _ = t.workerPool.Submit(func() { - // OnLibraryUpdateHook(updatedPaths, t.Config, t.log) - // }) + return updatedPaths } // getMoreInfo gets original name, size and files for a torrent @@ -311,6 +311,13 @@ func (t *TorrentManager) mergeToMain(existing, toMerge *Torrent) Torrent { return mainTorrent } +func (t *TorrentManager) TriggerHookOnLibraryUpdate(updatedPaths []string) { + t.log.Debugf("Triggering hook on_library_update for %d path(s)", len(updatedPaths)) + _ = t.workerPool.Submit(func() { + OnLibraryUpdateHook(updatedPaths, t.Config, t.log) + }) +} + // proxy func (t *TorrentManager) UnrestrictUntilOk(link string) *realdebrid.Download { t.log.Debugf("Unrestricting %s", link) @@ -400,9 +407,13 @@ func (t *TorrentManager) startRefreshJob() { } t.log.Infof("Detected changes! Refreshing %d torrents", checksum.TotalCount) - t.RefreshTorrents() + updatedPaths := t.RefreshTorrents() t.log.Info("Finished refreshing torrents") + if updatedPaths != nil { + t.TriggerHookOnLibraryUpdate(updatedPaths) + } + if t.Config.EnableRepair() { t.RepairAll() } else { @@ -597,7 +608,7 @@ func (t *TorrentManager) CheckDeletedState(torrent *Torrent) bool { return false } -func (t *TorrentManager) Delete(accessKey string, deleteInRD bool, updateDirectoryResponses bool) { +func (t *TorrentManager) Delete(accessKey string, deleteInRD bool, updateDirectoryResponses bool) []string { if deleteInRD { allTorrents, _ := t.DirectoryMap.Get(INT_ALL) infoCache, _ := t.DirectoryMap.Get(INT_INFO_CACHE) @@ -612,16 +623,19 @@ func (t *TorrentManager) Delete(accessKey string, deleteInRD bool, updateDirecto } } t.log.Infof("Removing torrent %s from zurg database", accessKey) + var updatedPaths []string t.DirectoryMap.IterCb(func(directory string, torrents cmap.ConcurrentMap[string, *Torrent]) { if ok := torrents.Has(accessKey); ok { torrents.Remove(accessKey) pathKey := fmt.Sprintf("%s/%s", directory, accessKey) + updatedPaths = append(updatedPaths, pathKey) t.ResponseCache.Del(pathKey) } }) if updateDirectoryResponses { t.UpdateDirectoryResponsesCache() } + return updatedPaths } func (t *TorrentManager) repair(torrent *Torrent) { @@ -846,21 +860,25 @@ func (t *TorrentManager) Repair(torrent *Torrent) { t.repair(torrent) t.log.Info("Finished repairing torrent %s", torrent.AccessKey) }) - t.UpdateTorrentResponseCache(torrent) + updatedPaths := t.UpdateTorrentResponseCache(torrent) + t.TriggerHookOnLibraryUpdate(updatedPaths) } -func (t *TorrentManager) UpdateTorrentResponseCache(torrent *Torrent) { +func (t *TorrentManager) UpdateTorrentResponseCache(torrent *Torrent) []string { + updatedPaths := []string{} dav, html := t.buildTorrentResponses(torrent) t.AssignedDirectoryCb(torrent, func(directory string) { torrents, _ := t.DirectoryMap.Get(directory) torrents.Set(torrent.AccessKey, torrent) pathKey := fmt.Sprintf("%s/%s", directory, torrent.AccessKey) + updatedPaths = append(updatedPaths, pathKey) // torrent responses newHtml := strings.ReplaceAll(html, "$dir", directory) t.ResponseCache.Set(pathKey+".html", newHtml, 1) newDav := strings.ReplaceAll(dav, "$dir", directory) t.ResponseCache.Set(pathKey+".dav", newDav, 1) }) + return updatedPaths } func (t *TorrentManager) UpdateDirectoryResponsesCache() {