From 7630e1c893f9848dce9d94135ce9b9a21ab80788 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 9 Feb 2022 11:40:47 +0100 Subject: Fix import timeout inconsistency --- server/helpers/youtube-dl/youtube-dl-cli.ts | 13 ++++++- server/helpers/youtube-dl/youtube-dl-wrapper.ts | 50 +++++++++++-------------- 2 files changed, 33 insertions(+), 30 deletions(-) (limited to 'server/helpers') diff --git a/server/helpers/youtube-dl/youtube-dl-cli.ts b/server/helpers/youtube-dl/youtube-dl-cli.ts index 293acff43..d4a6dd9b3 100644 --- a/server/helpers/youtube-dl/youtube-dl-cli.ts +++ b/server/helpers/youtube-dl/youtube-dl-cli.ts @@ -90,11 +90,13 @@ export class YoutubeDLCLI { format: string output: string processOptions: execa.NodeOptions + timeout: number additionalYoutubeDLArgs?: string[] }) { return this.run({ url: options.url, processOptions: options.processOptions, + timeout: options.timeout, args: (options.additionalYoutubeDLArgs || []).concat([ '-f', options.format, '-o', options.output ]) }) } @@ -145,16 +147,23 @@ export class YoutubeDLCLI { private async run (options: { url: string args: string[] + timeout?: number processOptions: execa.NodeOptions }) { - const { url, args, processOptions } = options + const { url, args, timeout, processOptions } = options let completeArgs = this.wrapWithProxyOptions(args) completeArgs = this.wrapWithIPOptions(completeArgs) completeArgs = this.wrapWithFFmpegOptions(completeArgs) const { PYTHON_PATH } = CONFIG.IMPORT.VIDEOS.HTTP.YOUTUBE_DL_RELEASE - const output = await execa(PYTHON_PATH, [ youtubeDLBinaryPath, ...completeArgs, url ], processOptions) + const subProcess = execa(PYTHON_PATH, [ youtubeDLBinaryPath, ...completeArgs, url ], processOptions) + + if (timeout) { + setTimeout(() => subProcess.cancel(), timeout) + } + + const output = await subProcess logger.debug('Runned youtube-dl command.', { command: output.command, ...lTags() }) diff --git a/server/helpers/youtube-dl/youtube-dl-wrapper.ts b/server/helpers/youtube-dl/youtube-dl-wrapper.ts index 6442c1e85..68c29cd86 100644 --- a/server/helpers/youtube-dl/youtube-dl-wrapper.ts +++ b/server/helpers/youtube-dl/youtube-dl-wrapper.ts @@ -77,38 +77,32 @@ class YoutubeDLWrapper { const youtubeDL = await YoutubeDLCLI.safeGet() - let timer: NodeJS.Timeout - const timeoutPromise = new Promise((_, rej) => { - timer = setTimeout(() => rej(new Error('YoutubeDL download timeout.')), timeout) - }) - - const downloadPromise = youtubeDL.download({ - url: this.url, - format: YoutubeDLCLI.getYoutubeDLVideoFormat(this.enabledResolutions), - output: pathWithoutExtension, - processOptions - }).then(() => clearTimeout(timer)) - .then(async () => { - // If youtube-dl did not guess an extension for our file, just use .mp4 as default - if (await pathExists(pathWithoutExtension)) { - await move(pathWithoutExtension, pathWithoutExtension + '.mp4') - } - - return this.guessVideoPathWithExtension(pathWithoutExtension, fileExt) + try { + await youtubeDL.download({ + url: this.url, + format: YoutubeDLCLI.getYoutubeDLVideoFormat(this.enabledResolutions), + output: pathWithoutExtension, + timeout, + processOptions }) - return Promise.race([ downloadPromise, timeoutPromise ]) - .catch(err => { - this.guessVideoPathWithExtension(pathWithoutExtension, fileExt) - .then(path => { - logger.debug('Error in youtube-dl import, deleting file %s.', path, { err, ...lTags() }) + // If youtube-dl did not guess an extension for our file, just use .mp4 as default + if (await pathExists(pathWithoutExtension)) { + await move(pathWithoutExtension, pathWithoutExtension + '.mp4') + } - return remove(path) - }) - .catch(innerErr => logger.error('Cannot remove file in youtubeDL timeout.', { innerErr, ...lTags() })) + return this.guessVideoPathWithExtension(pathWithoutExtension, fileExt) + } catch (err) { + this.guessVideoPathWithExtension(pathWithoutExtension, fileExt) + .then(path => { + logger.debug('Error in youtube-dl import, deleting file %s.', path, { err, ...lTags() }) - throw err - }) + return remove(path) + }) + .catch(innerErr => logger.error('Cannot remove file in youtubeDL timeout.', { innerErr, ...lTags() })) + + throw err + } } private async guessVideoPathWithExtension (tmpPath: string, sourceExt: string) { -- cgit v1.2.3