From bd8c6c9932e4ed13444dbd4618e29edc2787e6e8 Mon Sep 17 00:00:00 2001 From: Ben Adrian Sarmiento Date: Wed, 19 Jun 2024 13:11:50 +0200 Subject: [PATCH] Fix repair condition --- .github/workflows/build.yml | 4 +- .github/workflows/build_docker.yml | 2 +- internal/handlers/home.go | 57 ++------------------------ internal/handlers/router.go | 1 + internal/handlers/util.go | 65 ++++++++++++++++++++++++++++++ internal/torrent/refresh.go | 1 - internal/torrent/repair.go | 16 +++++++- internal/torrent/states.go | 2 + 8 files changed, 90 insertions(+), 58 deletions(-) create mode 100644 internal/handlers/util.go diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 3a5cb65..a006f91 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -5,7 +5,7 @@ on: tags: - '*' schedule: - - cron: '0 10 * * *' + - cron: '20 11 * * *' jobs: build: @@ -38,7 +38,7 @@ jobs: continue-on-error: false run: | # 3AM CET - since="$(date -d 'yesterday' +%Y-%m-%d) 10:00:00" + since="$(date -d 'yesterday' +%Y-%m-%d) 11:20:00" count=$(git rev-list --count --since="$since" --until=now HEAD) if [ "$count" -gt 0 ]; then echo "new_commits=true" >> $GITHUB_OUTPUT diff --git a/.github/workflows/build_docker.yml b/.github/workflows/build_docker.yml index 522e2d1..1284b25 100644 --- a/.github/workflows/build_docker.yml +++ b/.github/workflows/build_docker.yml @@ -5,7 +5,7 @@ on: tags: - '*' schedule: - - cron: '0 10 * * *' + - cron: '20 11 * * *' jobs: build-and-push-image: diff --git a/internal/handlers/home.go b/internal/handlers/home.go index 8f1b8bc..86e7206 100644 --- a/internal/handlers/home.go +++ b/internal/handlers/home.go @@ -270,7 +270,7 @@ func (zr *Handlers) handleHome(resp http.ResponseWriter, req *http.Request) { %v - IDs to be deleted once download completes + IDs to be deleted %v @@ -288,6 +288,9 @@ func (zr *Handlers) handleHome(resp http.ResponseWriter, req *http.Request) {
Trigger repair of all torrents
+
+ Reset repair state of all torrents so they can be repaired again +
Debug @@ -367,58 +370,6 @@ func (zr *Handlers) handleHome(resp http.ResponseWriter, req *http.Request) { fmt.Fprint(resp, out) } -func (zr *Handlers) handleRebootWorkerPool(resp http.ResponseWriter, req *http.Request) { - resp.Header().Set("Refresh", "2; url=/") - zr.workerPool.Release() - zr.workerPool.Reboot() - zr.log.Infof("Rebooted worker pool") - fmt.Fprint(resp, "Rebooting worker pool...") -} - -func (zr *Handlers) handleRebootRefreshWorker(resp http.ResponseWriter, req *http.Request) { - resp.Header().Set("Refresh", "2; url=/") - zr.torMgr.RefreshWorkerKillSwitch <- struct{}{} - zr.torMgr.StartRefreshJob() - zr.log.Infof("Rebooted refresh worker") - fmt.Fprint(resp, "Rebooting refresh worker...") -} - -func (zr *Handlers) handleRebootRepairWorker(resp http.ResponseWriter, req *http.Request) { - resp.Header().Set("Refresh", "2; url=/") - zr.torMgr.RepairWorkerKillSwitch <- struct{}{} - zr.torMgr.StartRepairJob() - zr.log.Infof("Rebooted repair worker") - fmt.Fprint(resp, "Rebooting repair worker...") -} - -func (zr *Handlers) handleRemountDownloads(resp http.ResponseWriter, req *http.Request) { - resp.Header().Set("Refresh", "2; url=/") - zr.torMgr.RemountTrigger <- struct{}{} - zr.log.Infof("Triggered remount of downloads") - fmt.Fprint(resp, "Remounting downloads...") -} - -func (zr *Handlers) handleDumpTorrents(resp http.ResponseWriter, req *http.Request) { - resp.Header().Set("Refresh", "2; url=/") - zr.torMgr.DumpTrigger <- struct{}{} - zr.log.Infof("Triggered dump of torrents") - fmt.Fprint(resp, "Dumping torrents...") -} - -func (zr *Handlers) handleAnalyzeTorrents(resp http.ResponseWriter, req *http.Request) { - resp.Header().Set("Refresh", "2; url=/") - zr.torMgr.AnalyzeTrigger <- struct{}{} - zr.log.Infof("Triggered media analysis of torrents") - fmt.Fprint(resp, "Analyzing all torrents...") -} - -func (zr *Handlers) handleTriggerRepairAll(resp http.ResponseWriter, req *http.Request) { - resp.Header().Set("Refresh", "2; url=/") - zr.torMgr.EnqueueForRepair(nil) - zr.log.Infof("Triggered repair of all torrents") - fmt.Fprint(resp, "Repairing all torrents...") -} - func bToMb(b uint64) uint64 { return b / 1024 / 1024 } diff --git a/internal/handlers/router.go b/internal/handlers/router.go index d8c528e..cd642eb 100644 --- a/internal/handlers/router.go +++ b/internal/handlers/router.go @@ -59,6 +59,7 @@ func AttachHandlers(router *chi.Mux, downloader *universal.Downloader, torMgr *t router.Post("/torrents/dump", hs.handleDumpTorrents) router.Post("/torrents/analyze", hs.handleAnalyzeTorrents) router.Post("/torrents/repair", hs.handleTriggerRepairAll) + router.Post("/torrents/reset-repair-state", hs.handleResetRepairState) // version router.Get(fmt.Sprintf("/{mountType}/%s", version.FILE), hs.handleVersionFile) router.Head(fmt.Sprintf("/{mountType}/%s", version.FILE), hs.handleCheckVersionFile) diff --git a/internal/handlers/util.go b/internal/handlers/util.go new file mode 100644 index 0000000..dcc6f18 --- /dev/null +++ b/internal/handlers/util.go @@ -0,0 +1,65 @@ +package handlers + +import ( + "fmt" + "net/http" +) + +func (zr *Handlers) handleRebootWorkerPool(resp http.ResponseWriter, req *http.Request) { + resp.Header().Set("Refresh", "2; url=/") + zr.log.Infof("Rebooting worker pool") + zr.workerPool.Release() + zr.workerPool.Reboot() + fmt.Fprint(resp, "Worker pool rebooted") +} + +func (zr *Handlers) handleRebootRefreshWorker(resp http.ResponseWriter, req *http.Request) { + resp.Header().Set("Refresh", "2; url=/") + zr.log.Infof("Rebooting refresh worker") + zr.torMgr.RefreshWorkerKillSwitch <- struct{}{} + zr.torMgr.StartRefreshJob() + fmt.Fprint(resp, "Refresh worker rebooted") +} + +func (zr *Handlers) handleRebootRepairWorker(resp http.ResponseWriter, req *http.Request) { + resp.Header().Set("Refresh", "2; url=/") + zr.log.Infof("Rebooting repair worker") + zr.torMgr.RepairWorkerKillSwitch <- struct{}{} + zr.torMgr.StartRepairJob() + fmt.Fprint(resp, "Repair worker rebooted") +} + +func (zr *Handlers) handleRemountDownloads(resp http.ResponseWriter, req *http.Request) { + resp.Header().Set("Refresh", "2; url=/") + zr.log.Infof("Triggering remount of downloads") + zr.torMgr.RemountTrigger <- struct{}{} + fmt.Fprint(resp, "Triggered remount of downloads") +} + +func (zr *Handlers) handleDumpTorrents(resp http.ResponseWriter, req *http.Request) { + resp.Header().Set("Refresh", "2; url=/") + zr.log.Infof("Triggering dump of torrents") + zr.torMgr.DumpTrigger <- struct{}{} + fmt.Fprint(resp, "Triggered dump of torrents") +} + +func (zr *Handlers) handleAnalyzeTorrents(resp http.ResponseWriter, req *http.Request) { + resp.Header().Set("Refresh", "2; url=/") + zr.log.Infof("Triggered analysis of all torrents") + zr.torMgr.AnalyzeTrigger <- struct{}{} + fmt.Fprint(resp, "Triggered analysis of all torrents") +} + +func (zr *Handlers) handleTriggerRepairAll(resp http.ResponseWriter, req *http.Request) { + resp.Header().Set("Refresh", "2; url=/") + zr.log.Infof("Triggering repair of all torrents") + zr.torMgr.EnqueueForRepair(nil) + fmt.Fprint(resp, "Triggered repair of all torrents") +} + +func (zr *Handlers) handleResetRepairState(resp http.ResponseWriter, req *http.Request) { + resp.Header().Set("Refresh", "2; url=/") + zr.log.Infof("Resetting repair state of all torrents") + zr.torMgr.ResetRepairState() + fmt.Fprint(resp, "Repair state of all torrents has been reset") +} diff --git a/internal/torrent/refresh.go b/internal/torrent/refresh.go index 8344038..5afee83 100644 --- a/internal/torrent/refresh.go +++ b/internal/torrent/refresh.go @@ -152,7 +152,6 @@ func (t *TorrentManager) StartRefreshJob() { continue } t.setNewLatestState(checksum) - t.refreshTorrents(false) t.log.Info("Finished refreshing torrents") case <-t.RefreshWorkerKillSwitch: diff --git a/internal/torrent/repair.go b/internal/torrent/repair.go index 7f2ea06..3dd0aee 100644 --- a/internal/torrent/repair.go +++ b/internal/torrent/repair.go @@ -124,7 +124,7 @@ func (t *TorrentManager) executeRepairJob(torrent *Torrent) { t.workerPool.Submit(func() { defer wg.Done() canExtract := t.Config.GetRarAction() == "extract" && strings.Contains(torrent.UnrepairableReason, "rar") - if !canExtract || torrent.UnrepairableReason != "" { + if torrent.UnrepairableReason != "" && !canExtract { return } // check 1: for broken files @@ -640,3 +640,17 @@ func (t *TorrentManager) isStillBroken(info *realdebrid.TorrentInfo, brokenFiles } return false } + +func (t *TorrentManager) ResetRepairState() { + if !t.Config.EnableRepair() { + return + } + allTorrents, _ := t.DirectoryMap.Get(INT_ALL) + allTorrents.IterCb(func(_ string, torrent *Torrent) { + err := torrent.State.Event(context.Background(), "reset_repair") + if err == nil { + t.repairLog.Debugf("Repair state of torrent %s has been reset", t.GetKey(torrent)) + t.writeTorrentToFile(torrent) + } + }) +} diff --git a/internal/torrent/states.go b/internal/torrent/states.go index db10b01..5f4df88 100644 --- a/internal/torrent/states.go +++ b/internal/torrent/states.go @@ -16,6 +16,8 @@ func NewTorrentState(initial string) *fsm.FSM { // when merging with another same hash torrent // when a torrent is rar'ed and not extracting {Name: "mark_as_repaired", Src: []string{"broken_torrent", "under_repair_torrent"}, Dst: "ok_torrent"}, + // when resetting repair state + {Name: "reset_repair", Src: []string{"under_repair_torrent"}, Dst: "broken_torrent"}, }, fsm.Callbacks{}, )