Create repair queue
This commit is contained in:
@@ -3,6 +3,7 @@ package torrent
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
@@ -33,6 +34,7 @@ type TorrentManager struct {
|
||||
workerPool *ants.Pool
|
||||
repairPool *ants.Pool
|
||||
repairTrigger chan *Torrent
|
||||
repairSet mapset.Set[*Torrent]
|
||||
repairRunning bool
|
||||
repairRunningMu sync.Mutex
|
||||
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) {
|
||||
filePath := "data/" + instanceID + ".json"
|
||||
file, err := os.Create(filePath)
|
||||
|
||||
@@ -23,6 +23,7 @@ func (t *TorrentManager) startRepairJob() {
|
||||
return
|
||||
}
|
||||
t.repairTrigger = make(chan *Torrent)
|
||||
t.repairSet = mapset.NewSet[*Torrent]()
|
||||
// there is 1 repair worker, with max 1 blocking task
|
||||
_ = t.repairPool.Submit(func() {
|
||||
t.log.Info("Starting periodic repair job")
|
||||
@@ -45,9 +46,11 @@ func (t *TorrentManager) invokeRepair(torrent *Torrent) {
|
||||
t.repairRunningMu.Lock()
|
||||
if t.repairRunning {
|
||||
t.repairRunningMu.Unlock()
|
||||
t.repairSet.Add(torrent)
|
||||
// don't do anything if repair is already running
|
||||
return
|
||||
}
|
||||
|
||||
t.repairRunning = true
|
||||
t.repairRunningMu.Unlock()
|
||||
|
||||
@@ -58,16 +61,19 @@ func (t *TorrentManager) invokeRepair(torrent *Torrent) {
|
||||
t.repairRunningMu.Lock()
|
||||
t.repairRunning = false
|
||||
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.
|
||||
func (t *TorrentManager) TriggerRepair(torrent *Torrent) {
|
||||
select {
|
||||
case t.repairTrigger <- torrent:
|
||||
// Repair triggered
|
||||
default:
|
||||
// Already a repair request pending, so do nothing
|
||||
}
|
||||
t.repairTrigger <- torrent
|
||||
}
|
||||
|
||||
func (t *TorrentManager) repairAll(torrent *Torrent) {
|
||||
@@ -118,6 +124,7 @@ func (t *TorrentManager) repairAll(torrent *Torrent) {
|
||||
return false
|
||||
})
|
||||
wg.Wait()
|
||||
|
||||
t.log.Infof("Finished repairing %d broken torrents", toRepair.Cardinality())
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user