Fixed problem caused by parallel downloads where ok.mkdirs would fail (when called by youtube-dl).

This commit is contained in:
Tiberiu Chibici 2018-11-02 02:50:16 +02:00
parent 288328f538
commit 2a8cc8da0e

View File

@ -5,10 +5,13 @@ import os
import youtube_dl import youtube_dl
import logging import logging
import re import re
from threading import Lock
log = logging.getLogger('video_downloader') log = logging.getLogger('video_downloader')
log_youtube_dl = log.getChild('youtube_dl') log_youtube_dl = log.getChild('youtube_dl')
_lock = Lock()
def __get_valid_path(path): def __get_valid_path(path):
""" """
@ -73,6 +76,12 @@ def download_video(video: Video, attempt: int = 1):
log.info('Downloading video %d [%s %s]', video.id, video.video_id, video.name) log.info('Downloading video %d [%s %s]', video.id, video.video_id, video.name)
# Issue: if multiple videos are downloaded at the same time, a race condition appears in the mkdirs() call that
# youtube-dl makes, which causes it to fail with the error 'Cannot create folder - file already exists'.
# For now, allow a single download instance.
_lock.acquire()
try:
max_attempts = settings.getint_sub(video.subscription, 'user', 'DownloadMaxAttempts', fallback=3) max_attempts = settings.getint_sub(video.subscription, 'user', 'DownloadMaxAttempts', fallback=3)
youtube_dl_params, output_path = __build_youtube_dl_params(video) youtube_dl_params, output_path = __build_youtube_dl_params(video)
@ -95,6 +104,9 @@ def download_video(video: Video, attempt: int = 1):
video.downloaded_path = '' video.downloaded_path = ''
video.save() video.save()
finally:
_lock.release()
def __schedule_download_video(video: Video, attempt=1): def __schedule_download_video(video: Video, attempt=1):
job = scheduler.scheduler.add_job(download_video, args=[video, attempt]) job = scheduler.scheduler.add_job(download_video, args=[video, attempt])