Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve torrent add reannounce handling #100

Merged
merged 1 commit into from
Nov 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions cmd/torrent_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"runtime"
"strconv"
"strings"
"sync"
"time"

"github.com/ludviglundgren/qbittorrent-cli/internal/config"
Expand Down Expand Up @@ -144,11 +145,18 @@ func RunTorrentAdd() *cobra.Command {

hash := magnet.InfoHash.String()

wg := sync.WaitGroup{}

wg.Add(1)

go func() {
defer wg.Done()
if err := checkTrackerStatus(ctx, qb, removeStalled, hash); err != nil {
log.Fatalf("could not get tracker status for torrent: %q\n", err)
}
}()

wg.Wait()
}

log.Printf("successfully added torrent from magnet: %s %s\n", filePath, hash)
Expand Down Expand Up @@ -178,6 +186,8 @@ func RunTorrentAdd() *cobra.Command {

log.Printf("found (%d) torrent(s) to add\n", len(files))

wg := sync.WaitGroup{}

success := 0
for _, file := range files {
if dry {
Expand All @@ -204,7 +214,10 @@ func RunTorrentAdd() *cobra.Command {

// some trackers are bugged or slow, so we need to re-announce the torrent until it works
if config.Reannounce.Enabled && !paused {
wg.Add(1)

go func() {
defer wg.Done()
if err := checkTrackerStatus(ctx, qb, removeStalled, hash); err != nil {
log.Printf("could not get tracker status for torrent: %s err: %q\n", hash, err)
}
Expand All @@ -225,6 +238,8 @@ func RunTorrentAdd() *cobra.Command {

}

wg.Wait()

log.Printf("successfully added %d torrent(s)\n", success)
}
}
Expand All @@ -248,7 +263,11 @@ func checkTrackerStatus(ctx context.Context, qb *qbittorrent.Client, removeStall

time.Sleep(time.Duration(config.Reannounce.Interval) * time.Millisecond)

log.Printf("starting reannounce for %s\n", hash)

for attempts < config.Reannounce.Attempts {
log.Printf("[%d/%d] reannounce attempt for %s\n", attempts, config.Reannounce.Attempts, hash)

trackers, err := qb.GetTorrentTrackersCtx(ctx, hash)
if err != nil {
log.Fatalf("could not get trackers of torrent: %v %v", hash, err)
Expand All @@ -257,6 +276,7 @@ func checkTrackerStatus(ctx context.Context, qb *qbittorrent.Client, removeStall
// check if status not working or something else
_, working := findTrackerStatus(trackers, 2)
if working {
log.Printf("[%d/%d] reannounce found working tracker for %s\n", attempts, config.Reannounce.Attempts, hash)
announceOK = true
break
}
Expand Down
Loading