Dump torrents job

This commit is contained in:
Ben Sarmiento
2024-05-26 03:49:16 +02:00
parent 249eebbea9
commit 17059e6a4a
5 changed files with 114 additions and 9 deletions

View File

@@ -41,6 +41,7 @@ type TorrentManager struct {
RefreshKillSwitch chan struct{}
RepairKillSwitch chan struct{}
RemountTrigger chan struct{}
DumpTrigger chan struct{}
latestState *LibraryState
@@ -75,29 +76,37 @@ func NewTorrentManager(cfg config.ConfigInterface, api *realdebrid.RealDebrid, w
RefreshKillSwitch: make(chan struct{}, 1),
RepairKillSwitch: make(chan struct{}, 1),
RemountTrigger: make(chan struct{}, 1),
DumpTrigger: make(chan struct{}, 1),
latestState: &LibraryState{log: log},
}
t.initializeBins()
t.initializeDirectoryMaps()
var wg sync.WaitGroup
wg.Add(2)
t.workerPool.Submit(func() {
defer wg.Done()
t.refreshTorrents()
wg.Done()
t.setNewLatestState(t.getCurrentState())
t.StartRefreshJob()
t.StartRepairJob()
t.TriggerRepair(nil)
})
t.workerPool.Submit(func() {
defer wg.Done()
t.mountNewDownloads()
wg.Done()
t.StartDownloadsJob()
})
t.workerPool.Submit(func() {
wg.Wait()
t.StartRefreshJob()
t.StartDownloadsJob()
t.StartRepairJob()
t.StartDumpJob()
t.setNewLatestState(t.getCurrentState())
t.TriggerRepair(nil)
t.log.Info("Applying media info details to all torrents")
allTorrents, _ := t.DirectoryMap.Get(INT_ALL)
allTorrents.IterCb(func(_ string, torrent *Torrent) {
@@ -348,6 +357,63 @@ func (t *TorrentManager) StartDownloadsJob() {
})
}
func (t *TorrentManager) dumpTorrents() {
files := t.getTorrentFiles("data")
for file := range files.Iter() {
destPath := "dump/" + filepath.Base(file)
if err := copyFile(file, destPath); err != nil {
t.log.Warnf("Cannot copy file %s to %s: %v", file, destPath, err)
}
}
}
func copyFile(sourcePath, destPath string) error {
source, err := os.Open(sourcePath)
if err != nil {
return err
}
defer source.Close()
destination, err := os.Create(destPath)
if err != nil {
return err
}
defer destination.Close()
buf := make([]byte, 4096)
for {
n, err := source.Read(buf)
if err != nil && err != io.EOF {
return err
}
if n == 0 {
break
}
if _, err := destination.Write(buf[:n]); err != nil {
return err
}
}
return nil
}
func (t *TorrentManager) StartDumpJob() {
_ = t.workerPool.Submit(func() {
dumpTicker := time.NewTicker(time.Duration(t.Config.GetDumpTorrentsEveryMins()) * time.Minute)
defer dumpTicker.Stop()
for {
select {
case <-dumpTicker.C:
t.dumpTorrents()
case <-t.DumpTrigger:
t.dumpTorrents()
}
}
})
}
func (t *TorrentManager) initializeDirectoryMaps() {
// create internal directories
t.DirectoryMap.Set(INT_ALL, cmap.New[*Torrent]()) // key is GetAccessKey()