periodic repair
This commit is contained in:
@@ -18,17 +18,68 @@ const (
|
||||
|
||||
func (t *TorrentManager) startRepairJob() {
|
||||
if !t.Config.EnableRepair() {
|
||||
t.log.Info("Repair is disabled, skipping repair job")
|
||||
t.log.Debug("Repair is disabled, skipping repair job")
|
||||
return
|
||||
}
|
||||
t.repairTrigger = make(chan *Torrent)
|
||||
// there is 1 repair worker, with max 1 blocking task
|
||||
_ = t.repairPool.Submit(func() {
|
||||
t.repairAll()
|
||||
t.log.Info("Starting periodic repair job")
|
||||
repairTicker := time.NewTicker(time.Duration(t.Config.GetRepairEveryMinutes()) * time.Minute)
|
||||
defer repairTicker.Stop()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-repairTicker.C:
|
||||
t.invokeRepair(nil)
|
||||
case torrent := <-t.repairTrigger:
|
||||
// On-demand trigger with a specific torrent
|
||||
t.invokeRepair(torrent)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func (t *TorrentManager) repairAll() {
|
||||
func (t *TorrentManager) invokeRepair(torrent *Torrent) {
|
||||
t.repairRunningMu.Lock()
|
||||
if t.repairRunning {
|
||||
t.repairRunningMu.Unlock()
|
||||
// don't do anything if repair is already running
|
||||
return
|
||||
}
|
||||
t.repairRunning = true
|
||||
t.repairRunningMu.Unlock()
|
||||
|
||||
// Execute the repair job
|
||||
t.repairAll(torrent)
|
||||
|
||||
// After repair is done
|
||||
t.repairRunningMu.Lock()
|
||||
t.repairRunning = false
|
||||
t.repairRunningMu.Unlock()
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
}
|
||||
|
||||
func (t *TorrentManager) repairAll(torrent *Torrent) {
|
||||
t.log.Info("Periodic repair invoked; searching for broken torrents")
|
||||
allTorrents, _ := t.DirectoryMap.Get(INT_ALL)
|
||||
|
||||
// todo: a more elegant way to do this
|
||||
var allTorrents cmap.ConcurrentMap[string, *Torrent]
|
||||
if torrent == nil {
|
||||
allTorrents, _ = t.DirectoryMap.Get(INT_ALL)
|
||||
} else {
|
||||
allTorrents = cmap.New[*Torrent]()
|
||||
allTorrents.Set(t.GetKey(torrent), torrent)
|
||||
}
|
||||
|
||||
// collect all torrents that need to be repaired
|
||||
toRepair := mapset.NewSet[*Torrent]()
|
||||
|
||||
Reference in New Issue
Block a user