Create repair queue
This commit is contained in:
@@ -19,6 +19,7 @@ type ConfigInterface interface {
|
|||||||
GetNetworkBufferSize() int
|
GetNetworkBufferSize() int
|
||||||
EnableRetainFolderNameExtension() bool
|
EnableRetainFolderNameExtension() bool
|
||||||
EnableRetainRDTorrentName() bool
|
EnableRetainRDTorrentName() bool
|
||||||
|
ShouldExposeFullPath() bool
|
||||||
ShouldIgnoreRenames() bool
|
ShouldIgnoreRenames() bool
|
||||||
ShouldServeFromRclone() bool
|
ShouldServeFromRclone() bool
|
||||||
ShouldVerifyDownloadLink() bool
|
ShouldVerifyDownloadLink() bool
|
||||||
@@ -47,6 +48,7 @@ type ZurgConfig struct {
|
|||||||
IgnoreRenames bool `yaml:"ignore_renames" json:"ignore_renames"`
|
IgnoreRenames bool `yaml:"ignore_renames" json:"ignore_renames"`
|
||||||
RetainRDTorrentName bool `yaml:"retain_rd_torrent_name" json:"retain_rd_torrent_name"`
|
RetainRDTorrentName bool `yaml:"retain_rd_torrent_name" json:"retain_rd_torrent_name"`
|
||||||
RetainFolderNameExtension bool `yaml:"retain_folder_name_extension" json:"retain_folder_name_extension"`
|
RetainFolderNameExtension bool `yaml:"retain_folder_name_extension" json:"retain_folder_name_extension"`
|
||||||
|
ExposeFullPath bool `yaml:"expose_full_path" json:"expose_full_path"`
|
||||||
|
|
||||||
CanRepair bool `yaml:"enable_repair" json:"enable_repair"`
|
CanRepair bool `yaml:"enable_repair" json:"enable_repair"`
|
||||||
DeleteRarFiles bool `yaml:"auto_delete_rar_torrents" json:"auto_delete_rar_torrents"`
|
DeleteRarFiles bool `yaml:"auto_delete_rar_torrents" json:"auto_delete_rar_torrents"`
|
||||||
@@ -142,6 +144,10 @@ func (z *ZurgConfig) EnableRetainRDTorrentName() bool {
|
|||||||
return z.RetainRDTorrentName
|
return z.RetainRDTorrentName
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (z *ZurgConfig) ShouldExposeFullPath() bool {
|
||||||
|
return z.ExposeFullPath
|
||||||
|
}
|
||||||
|
|
||||||
func (z *ZurgConfig) ShouldIgnoreRenames() bool {
|
func (z *ZurgConfig) ShouldIgnoreRenames() bool {
|
||||||
return !z.IgnoreRenames
|
return !z.IgnoreRenames
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package torrent
|
|||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
@@ -33,6 +34,7 @@ type TorrentManager struct {
|
|||||||
workerPool *ants.Pool
|
workerPool *ants.Pool
|
||||||
repairPool *ants.Pool
|
repairPool *ants.Pool
|
||||||
repairTrigger chan *Torrent
|
repairTrigger chan *Torrent
|
||||||
|
repairSet mapset.Set[*Torrent]
|
||||||
repairRunning bool
|
repairRunning bool
|
||||||
repairRunningMu sync.Mutex
|
repairRunningMu sync.Mutex
|
||||||
log *logutil.Logger
|
log *logutil.Logger
|
||||||
@@ -105,6 +107,14 @@ func (t *TorrentManager) GetKey(torrent *Torrent) string {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *TorrentManager) GetPath(file *File) string {
|
||||||
|
if t.Config.ShouldExposeFullPath() {
|
||||||
|
filename := strings.TrimPrefix(file.Path, "/")
|
||||||
|
return strings.ReplaceAll(filename, "/", " - ")
|
||||||
|
}
|
||||||
|
return filepath.Base(file.Path)
|
||||||
|
}
|
||||||
|
|
||||||
func (t *TorrentManager) writeTorrentToFile(instanceID string, torrent *Torrent) {
|
func (t *TorrentManager) writeTorrentToFile(instanceID string, torrent *Torrent) {
|
||||||
filePath := "data/" + instanceID + ".json"
|
filePath := "data/" + instanceID + ".json"
|
||||||
file, err := os.Create(filePath)
|
file, err := os.Create(filePath)
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ func (t *TorrentManager) startRepairJob() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
t.repairTrigger = make(chan *Torrent)
|
t.repairTrigger = make(chan *Torrent)
|
||||||
|
t.repairSet = mapset.NewSet[*Torrent]()
|
||||||
// there is 1 repair worker, with max 1 blocking task
|
// there is 1 repair worker, with max 1 blocking task
|
||||||
_ = t.repairPool.Submit(func() {
|
_ = t.repairPool.Submit(func() {
|
||||||
t.log.Info("Starting periodic repair job")
|
t.log.Info("Starting periodic repair job")
|
||||||
@@ -45,9 +46,11 @@ func (t *TorrentManager) invokeRepair(torrent *Torrent) {
|
|||||||
t.repairRunningMu.Lock()
|
t.repairRunningMu.Lock()
|
||||||
if t.repairRunning {
|
if t.repairRunning {
|
||||||
t.repairRunningMu.Unlock()
|
t.repairRunningMu.Unlock()
|
||||||
|
t.repairSet.Add(torrent)
|
||||||
// don't do anything if repair is already running
|
// don't do anything if repair is already running
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
t.repairRunning = true
|
t.repairRunning = true
|
||||||
t.repairRunningMu.Unlock()
|
t.repairRunningMu.Unlock()
|
||||||
|
|
||||||
@@ -58,16 +61,19 @@ func (t *TorrentManager) invokeRepair(torrent *Torrent) {
|
|||||||
t.repairRunningMu.Lock()
|
t.repairRunningMu.Lock()
|
||||||
t.repairRunning = false
|
t.repairRunning = false
|
||||||
t.repairRunningMu.Unlock()
|
t.repairRunningMu.Unlock()
|
||||||
|
|
||||||
|
// before we let go, let's check repairSet
|
||||||
|
t.workerPool.Submit(func() {
|
||||||
|
queuedTorrent, exists := t.repairSet.Pop()
|
||||||
|
if exists {
|
||||||
|
t.TriggerRepair(queuedTorrent)
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// TriggerRepair allows an on-demand repair to be initiated.
|
// TriggerRepair allows an on-demand repair to be initiated.
|
||||||
func (t *TorrentManager) TriggerRepair(torrent *Torrent) {
|
func (t *TorrentManager) TriggerRepair(torrent *Torrent) {
|
||||||
select {
|
t.repairTrigger <- torrent
|
||||||
case t.repairTrigger <- torrent:
|
|
||||||
// Repair triggered
|
|
||||||
default:
|
|
||||||
// Already a repair request pending, so do nothing
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *TorrentManager) repairAll(torrent *Torrent) {
|
func (t *TorrentManager) repairAll(torrent *Torrent) {
|
||||||
@@ -118,6 +124,7 @@ func (t *TorrentManager) repairAll(torrent *Torrent) {
|
|||||||
return false
|
return false
|
||||||
})
|
})
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
|
|
||||||
t.log.Infof("Finished repairing %d broken torrents", toRepair.Cardinality())
|
t.log.Infof("Finished repairing %d broken torrents", toRepair.Cardinality())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user