Ejemplo n.º 1
0
        protected override async Task ExecuteJob(IJobExecutionContext context)
        {
            VideoId     = context.MergedJobDataMap.GetInt("VideoId");
            shouldRetry = false;

            video = dataContext.Videos.Find(VideoId);
            if (video == null)
            {
                throw new ArgumentException($"Download failed - invalid video id {VideoId}.");
            }

            if (video.DownloadedPath != null)
            {
                throw new ArgumentException($"Download failed - video {VideoId} is already downloaded!");
            }

            var opts = ResolveDownloadOptions(video).ToArray();

            shouldRetry = true;

            log.LogInformation("Running youtube-dl with arguments: {0}", string.Join(" ", opts));

            try
            {
                await ytdlService.UsingYoutubeDL(ytdl =>
                {
                    int resultCode = ytdl.Run(opts,
                                              ProcessStdout,
                                              ProcessStderr,
                                              timeoutMs: 24 * 3600 * 1000,
                                              cancellationToken: cancellationTokenSrc.Token);

                    if (resultCode != 0)
                    {
                        throw new Exception($"videoId={VideoId}: Download failed!\n");
                    }

                    return(Task.CompletedTask);
                });
            }
            catch (OperationCanceledException)
            {
                shouldRetry = false;
                log.LogInformation("Video download was canceled!");
                throw;
            }
            finally
            {
                videoDownloader.OnDownloadFinished(VideoId);
            }

            using (var @lock = await videoMutex.LockAsync())
            {
                video.DownloadedPath = outputPath;
                video.DownloadedSize = await videoStorage.CalculateSize(video);

                await dataContext.SaveChangesAsync();
            }

            log.LogInformation($"videoId={VideoId}: Download completed!");
        }