Support rebooting workers

This commit is contained in:
Ben Sarmiento
2024-01-28 23:04:39 +01:00
parent 4ff88372bc
commit 15a24a58a2
7 changed files with 79 additions and 47 deletions

View File

@@ -21,24 +21,26 @@ const (
)
type TorrentManager struct {
Config config.ConfigInterface
Api *realdebrid.RealDebrid
DirectoryMap cmap.ConcurrentMap[string, cmap.ConcurrentMap[string, *Torrent]] // directory -> accessKey -> Torrent
DownloadCache cmap.ConcurrentMap[string, *realdebrid.Download]
DownloadMap cmap.ConcurrentMap[string, *realdebrid.Download]
fixers cmap.ConcurrentMap[string, *Torrent]
deleteOnceDone mapset.Set[string]
allAccessKeys mapset.Set[string]
latestState *LibraryState
requiredVersion string
workerPool *ants.Pool
refreshPool *ants.Pool
repairPool *ants.Pool
repairTrigger chan *Torrent
repairSet mapset.Set[*Torrent]
repairRunning bool
repairRunningMu sync.Mutex
log *logutil.Logger
Config config.ConfigInterface
Api *realdebrid.RealDebrid
DirectoryMap cmap.ConcurrentMap[string, cmap.ConcurrentMap[string, *Torrent]] // directory -> accessKey -> Torrent
DownloadCache cmap.ConcurrentMap[string, *realdebrid.Download]
DownloadMap cmap.ConcurrentMap[string, *realdebrid.Download]
fixers cmap.ConcurrentMap[string, *Torrent]
deleteOnceDone mapset.Set[string]
allAccessKeys mapset.Set[string]
latestState *LibraryState
requiredVersion string
workerPool *ants.Pool
refreshPool *ants.Pool
RefreshKillSwitch chan struct{}
RepairKillSwitch chan struct{}
repairPool *ants.Pool
repairTrigger chan *Torrent
repairSet mapset.Set[*Torrent]
repairRunning bool
repairRunningMu sync.Mutex
log *logutil.Logger
}
// NewTorrentManager creates a new torrent manager
@@ -46,20 +48,22 @@ type TorrentManager struct {
// and store them in-memory and cached in files
func NewTorrentManager(cfg config.ConfigInterface, api *realdebrid.RealDebrid, workerPool, refreshPool, repairPool *ants.Pool, log *logutil.Logger) *TorrentManager {
t := &TorrentManager{
Config: cfg,
Api: api,
DirectoryMap: cmap.New[cmap.ConcurrentMap[string, *Torrent]](),
DownloadCache: cmap.New[*realdebrid.Download](),
DownloadMap: cmap.New[*realdebrid.Download](),
fixers: cmap.New[*Torrent](),
deleteOnceDone: mapset.NewSet[string](),
allAccessKeys: mapset.NewSet[string](),
latestState: &LibraryState{},
requiredVersion: "0.9.3-hotfix.3",
workerPool: workerPool,
refreshPool: refreshPool,
repairPool: repairPool,
log: log,
Config: cfg,
Api: api,
DirectoryMap: cmap.New[cmap.ConcurrentMap[string, *Torrent]](),
DownloadCache: cmap.New[*realdebrid.Download](),
DownloadMap: cmap.New[*realdebrid.Download](),
RefreshKillSwitch: make(chan struct{}, 1),
RepairKillSwitch: make(chan struct{}, 1),
fixers: cmap.New[*Torrent](),
deleteOnceDone: mapset.NewSet[string](),
allAccessKeys: mapset.NewSet[string](),
latestState: &LibraryState{},
requiredVersion: "0.9.3-hotfix.3",
workerPool: workerPool,
refreshPool: refreshPool,
repairPool: repairPool,
log: log,
}
t.initializeDirectories()

View File

@@ -123,20 +123,27 @@ func (t *TorrentManager) refreshTorrents() []string {
func (t *TorrentManager) StartRefreshJob() {
_ = t.refreshPool.Submit(func() {
t.log.Info("Starting periodic refresh job")
refreshTicker := time.NewTicker(time.Duration(t.Config.GetRefreshEverySecs()) * time.Second)
defer refreshTicker.Stop()
for {
<-time.After(time.Duration(t.Config.GetRefreshEverySecs()) * time.Second)
select {
case <-refreshTicker.C:
checksum := t.getCurrentState()
if t.latestState.equal(checksum) {
continue
}
t.SetNewLatestState(checksum)
t.log.Infof("Detected changes! Refreshing %d torrents", checksum.TotalCount)
checksum := t.getCurrentState()
if t.latestState.equal(checksum) {
continue
updatedPaths := t.refreshTorrents()
t.log.Info("Finished refreshing torrents")
t.TriggerHookOnLibraryUpdate(updatedPaths)
case <-t.RefreshKillSwitch:
t.log.Info("Stopping periodic refresh job")
return
}
t.SetNewLatestState(checksum)
t.log.Infof("Detected changes! Refreshing %d torrents", checksum.TotalCount)
updatedPaths := t.refreshTorrents()
t.log.Info("Finished refreshing torrents")
t.TriggerHookOnLibraryUpdate(updatedPaths)
}
})
}

View File

@@ -37,6 +37,9 @@ func (t *TorrentManager) StartRepairJob() {
case torrent := <-t.repairTrigger:
// On-demand trigger with a specific torrent
t.invokeRepair(torrent)
case <-t.RepairKillSwitch:
t.log.Info("Stopping periodic repair job")
return
}
}
})