Skip to content

Commit d39e464

Browse files
committed
Configure download timeouts #186
1 parent 31cb976 commit d39e464

File tree

5 files changed

+21
-7
lines changed

5 files changed

+21
-7
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ vimeo = [ # Multiple keys will be rotated.
8888

8989
[downloader]
9090
self_update = true # Optional, auto update youtube-dl every 24 hours
91+
timeout = 15 # Timeout in minutes
9192

9293
# Optional log config. If not specified logs to the stdout
9394
[log]

cmd/podsync/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func main() {
9797
"date": date,
9898
}).Info("running podsync")
9999

100-
downloader, err := ytdl.New(ctx, cfg.Downloader.SelfUpdate)
100+
downloader, err := ytdl.New(ctx, cfg.Downloader)
101101
if err != nil {
102102
log.WithError(err).Fatal("youtube-dl error")
103103
}

pkg/config/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ type Log struct {
111111
type Downloader struct {
112112
// SelfUpdate toggles self update every 24 hour
113113
SelfUpdate bool `toml:"self_update"`
114+
// Timeout in minutes for youtube-dl process to finish download
115+
Timeout int `toml:"timeout"`
114116
}
115117

116118
type Config struct {

pkg/config/config_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ dir = "/home/user/db/"
3030
3131
[downloader]
3232
self_update = true
33+
timeout = 15
3334
3435
[feeds]
3536
[feeds.XYZ]
@@ -82,6 +83,7 @@ self_update = true
8283
assert.Nil(t, config.Database.Badger)
8384

8485
assert.True(t, config.Downloader.SelfUpdate)
86+
assert.EqualValues(t, 15, config.Downloader.Timeout)
8587
}
8688

8789
func TestLoadEmptyKeyList(t *testing.T) {

pkg/ytdl/ytdl.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ import (
2121
)
2222

2323
const (
24-
DownloadTimeout = 10 * time.Minute
25-
UpdatePeriod = 24 * time.Hour
24+
DefaultDownloadTimeout = 10 * time.Minute
25+
UpdatePeriod = 24 * time.Hour
2626
)
2727

2828
var (
@@ -31,19 +31,28 @@ var (
3131

3232
type YoutubeDl struct {
3333
path string
34+
timeout time.Duration
3435
updateLock sync.Mutex // Don't call youtube-dl while self updating
3536
}
3637

37-
func New(ctx context.Context, update bool) (*YoutubeDl, error) {
38+
func New(ctx context.Context, cfg config.Downloader) (*YoutubeDl, error) {
3839
path, err := exec.LookPath("youtube-dl")
3940
if err != nil {
4041
return nil, errors.Wrap(err, "youtube-dl binary not found")
4142
}
4243

4344
log.Debugf("found youtube-dl binary at %q", path)
4445

46+
timeout := DefaultDownloadTimeout
47+
if cfg.Timeout > 0 {
48+
timeout = time.Duration(cfg.Timeout) * time.Minute
49+
}
50+
51+
log.Debugf("download timeout: %d min(s)", int(timeout.Minutes()))
52+
4553
ytdl := &YoutubeDl{
46-
path: path,
54+
path: path,
55+
timeout: timeout,
4756
}
4857

4958
// Make sure youtube-dl exists
@@ -58,7 +67,7 @@ func New(ctx context.Context, update bool) (*YoutubeDl, error) {
5867
return nil, err
5968
}
6069

61-
if update {
70+
if cfg.SelfUpdate {
6271
// Do initial blocking update at launch
6372
if err := ytdl.Update(ctx); err != nil {
6473
log.WithError(err).Error("failed to update youtube-dl")
@@ -177,7 +186,7 @@ func (dl *YoutubeDl) Download(ctx context.Context, feedConfig *config.Feed, epis
177186
}
178187

179188
func (dl *YoutubeDl) exec(ctx context.Context, args ...string) (string, error) {
180-
ctx, cancel := context.WithTimeout(ctx, DownloadTimeout)
189+
ctx, cancel := context.WithTimeout(ctx, dl.timeout)
181190
defer cancel()
182191

183192
cmd := exec.CommandContext(ctx, dl.path, args...)

0 commit comments

Comments
 (0)