diff --git a/README.md b/README.md index 666bccb9..c88fe22f 100644 --- a/README.md +++ b/README.md @@ -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] diff --git a/cmd/podsync/main.go b/cmd/podsync/main.go index fc670dd3..72816cd1 100644 --- a/cmd/podsync/main.go +++ b/cmd/podsync/main.go @@ -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") } diff --git a/pkg/config/config.go b/pkg/config/config.go index 50f913a8..a01e198d 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -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 { diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go index 57a9d866..84cd3fab 100644 --- a/pkg/config/config_test.go +++ b/pkg/config/config_test.go @@ -30,6 +30,7 @@ dir = "/home/user/db/" [downloader] self_update = true +timeout = 15 [feeds] [feeds.XYZ] @@ -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) { diff --git a/pkg/ytdl/ytdl.go b/pkg/ytdl/ytdl.go index b83faa05..fe87752f 100644 --- a/pkg/ytdl/ytdl.go +++ b/pkg/ytdl/ytdl.go @@ -21,8 +21,8 @@ import ( ) const ( - DownloadTimeout = 10 * time.Minute - UpdatePeriod = 24 * time.Hour + DefaultDownloadTimeout = 10 * time.Minute + UpdatePeriod = 24 * time.Hour ) var ( @@ -31,10 +31,11 @@ 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") @@ -42,8 +43,16 @@ func New(ctx context.Context, update bool) (*YoutubeDl, error) { 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 @@ -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") @@ -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...)