Create repair queue

This commit is contained in:
Ben Sarmiento
2024-01-28 03:20:58 +01:00
parent f07b65d5da
commit 30dc080dba
3 changed files with 29 additions and 6 deletions

View File

@@ -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())
}