diff --git a/cmd/downloader/downloader/downloader.go b/cmd/downloader/downloader/downloader.go index 3bd5edf2f0b14e109caa817d4ee5d114bcc8ef78..11f8f3117c5ca67ed098e9c1bc58b7f2424dd373 100644 --- a/cmd/downloader/downloader/downloader.go +++ b/cmd/downloader/downloader/downloader.go @@ -207,12 +207,18 @@ func CalcStats(prevStats AggStats, interval time.Duration, client *torrent.Clien // added first time - pieces verification process will start (disk IO heavy) - Progress // kept in `piece completion storage` (surviving reboot). Once it done - no disk IO needed again. // Don't need call torrent.VerifyData manually -func AddTorrentFiles(snapshotsDir string, torrentClient *torrent.Client) error { +func AddTorrentFiles(ctx context.Context, snapshotsDir string, torrentClient *torrent.Client) error { files, err := AllTorrentPaths(snapshotsDir) if err != nil { return err } for _, torrentFilePath := range files { + select { + case <-ctx.Done(): + return ctx.Err() + default: + } + mi, err := metainfo.LoadFromFile(torrentFilePath) if err != nil { return err diff --git a/cmd/downloader/downloader/server.go b/cmd/downloader/downloader/server.go index 36cec1a6e750d57cdc604e494bfd0d20e8b75a1a..69f0abe9c274bb420812ce3b0e5c1c40cf0d6ce5 100644 --- a/cmd/downloader/downloader/server.go +++ b/cmd/downloader/downloader/server.go @@ -35,7 +35,7 @@ func CreateTorrentFilesAndAdd(ctx context.Context, snapshotDir string, torrentCl if err := BuildTorrentFilesIfNeed(ctx, snapshotDir); err != nil { return err } - if err := AddTorrentFiles(snapshotDir, torrentClient); err != nil { + if err := AddTorrentFiles(ctx, snapshotDir, torrentClient); err != nil { return err } for _, t := range torrentClient.Torrents() { diff --git a/cmd/downloader/main.go b/cmd/downloader/main.go index 372489c499f9bf6ffcccb26bed22657f2a776290..c48f7418745f8689f33e795e8d3fdc109b2dc3a1 100644 --- a/cmd/downloader/main.go +++ b/cmd/downloader/main.go @@ -94,7 +94,7 @@ var rootCmd = &cobra.Command{ debug.Exit() }, RunE: func(cmd *cobra.Command, args []string) error { - if err := Downloader(cmd.Context(), cmd); err != nil { + if err := Downloader(cmd.Context()); err != nil { log.Error("Downloader", "err", err) return nil } @@ -102,7 +102,7 @@ var rootCmd = &cobra.Command{ }, } -func Downloader(ctx context.Context, cmd *cobra.Command) error { +func Downloader(ctx context.Context) error { snapshotDir := path.Join(datadir, "snapshots") common.MustExist(snapshotDir) torrentLogLevel, ok := torrentcfg.String2LogLevel[torrentVerbosity]