Skip to content

Commit

Permalink
Configure download timeouts #186
Browse files Browse the repository at this point in the history
  • Loading branch information
mxpv committed Oct 17, 2020
1 parent 31cb976 commit d39e464
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 7 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ vimeo = [ # Multiple keys will be rotated.

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

# Optional log config. If not specified logs to the stdout
[log]
Expand Down
2 changes: 1 addition & 1 deletion cmd/podsync/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func main() {
"date": date,
}).Info("running podsync")

downloader, err := ytdl.New(ctx, cfg.Downloader.SelfUpdate)
downloader, err := ytdl.New(ctx, cfg.Downloader)
if err != nil {
log.WithError(err).Fatal("youtube-dl error")
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ type Log struct {
type Downloader struct {
// SelfUpdate toggles self update every 24 hour
SelfUpdate bool `toml:"self_update"`
// Timeout in minutes for youtube-dl process to finish download
Timeout int `toml:"timeout"`
}

type Config struct {
Expand Down
2 changes: 2 additions & 0 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ dir = "/home/user/db/"
[downloader]
self_update = true
timeout = 15
[feeds]
[feeds.XYZ]
Expand Down Expand Up @@ -82,6 +83,7 @@ self_update = true
assert.Nil(t, config.Database.Badger)

assert.True(t, config.Downloader.SelfUpdate)
assert.EqualValues(t, 15, config.Downloader.Timeout)
}

func TestLoadEmptyKeyList(t *testing.T) {
Expand Down
21 changes: 15 additions & 6 deletions pkg/ytdl/ytdl.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import (
)

const (
DownloadTimeout = 10 * time.Minute
UpdatePeriod = 24 * time.Hour
DefaultDownloadTimeout = 10 * time.Minute
UpdatePeriod = 24 * time.Hour
)

var (
Expand All @@ -31,19 +31,28 @@ var (

type YoutubeDl struct {
path string
timeout time.Duration
updateLock sync.Mutex // Don't call youtube-dl while self updating
}

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

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

timeout := DefaultDownloadTimeout
if cfg.Timeout > 0 {
timeout = time.Duration(cfg.Timeout) * time.Minute
}

log.Debugf("download timeout: %d min(s)", int(timeout.Minutes()))

ytdl := &YoutubeDl{
path: path,
path: path,
timeout: timeout,
}

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

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

func (dl *YoutubeDl) exec(ctx context.Context, args ...string) (string, error) {
ctx, cancel := context.WithTimeout(ctx, DownloadTimeout)
ctx, cancel := context.WithTimeout(ctx, dl.timeout)
defer cancel()

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

0 comments on commit d39e464

Please sign in to comment.