From c7a8b063362344f0c2acc63b44be80269bd571fc Mon Sep 17 00:00:00 2001 From: Johan Nyman Date: Wed, 27 Sep 2023 14:57:02 +0200 Subject: [PATCH] fix: rewrite the retrying of ffmpeg --- .../expectationHandlers/packageDeepScan.ts | 108 ++++++++++++------ 1 file changed, 71 insertions(+), 37 deletions(-) diff --git a/shared/packages/worker/src/worker/workers/windowsWorker/expectationHandlers/packageDeepScan.ts b/shared/packages/worker/src/worker/workers/windowsWorker/expectationHandlers/packageDeepScan.ts index 582fc5a8..21c103b6 100644 --- a/shared/packages/worker/src/worker/workers/windowsWorker/expectationHandlers/packageDeepScan.ts +++ b/shared/packages/worker/src/worker/workers/windowsWorker/expectationHandlers/packageDeepScan.ts @@ -190,46 +190,80 @@ export const PackageDeepScan: ExpectationWindowsHandler = { let resultScenes: number[] = [] if (hasVideoStream) { let hasGottenProgress = false - let isDone = false - currentProcess = scanMoreInfo( - sourceHandle, - ffProbeScan, - exp.endRequirement.version, - (progress) => { - hasGottenProgress = true - workInProgress._reportProgress(sourceVersionHash, 0.21 + 0.77 * progress) - }, - worker.logger.category('scanMoreInfo') - ) - setTimeout(() => { - if (!isDone && currentProcess && !hasGottenProgress) { - // If we haven't gotten any progress yet, we probably won't get any. - - // 2023-09-20: There seems to be some bug in the FFMpeg scan where it won't output any progress - // if the scene detection is on. - // Let's abort and try again without scene detection: - - currentProcess.cancel() - - currentProcess = scanMoreInfo( - sourceHandle, - ffProbeScan, - { - ...exp.endRequirement.version, - scenes: false, // no scene detection - }, - (progress) => { - hasGottenProgress = true - workInProgress._reportProgress(sourceVersionHash, 0.21 + 0.77 * progress) - }, - worker.logger.category('scanMoreInfo') - ) - } - }, progressTimeout * 0.5) + currentProcess = new CancelablePromise<{ + scenes: number[] + freezes: ScanAnomaly[] + blacks: ScanAnomaly[] + }>(async (resolve, reject, onCancel) => { + let isDone = false + let ignoreNextCancelError = false + + const scanMoreInfoProcess = scanMoreInfo( + sourceHandle, + ffProbeScan, + exp.endRequirement.version, + (progress) => { + hasGottenProgress = true + workInProgress._reportProgress(sourceVersionHash, 0.21 + 0.77 * progress) + }, + worker.logger.category('scanMoreInfo') + ) + onCancel(() => { + scanMoreInfoProcess.cancel() + }) + + scanMoreInfoProcess.then( + (result) => { + isDone = true + resolve(result) + }, + (error) => { + if (`${error}`.match(/cancelled/i) && ignoreNextCancelError) { + // ignore this + ignoreNextCancelError = false + } else { + reject(error) + } + } + ) + + // Guard against an edge case where we don't get any progress reports: + setTimeout(() => { + if (!isDone && currentProcess && !hasGottenProgress) { + // If we haven't gotten any progress yet, we probably won't get any. + + // 2023-09-20: There seems to be some bug in the FFMpeg scan where it won't output any progress + // if the scene detection is on. + // Let's abort and try again without scene detection: + + ignoreNextCancelError = true + currentProcess.cancel() + + const scanMoreInfoProcessSecondTry = scanMoreInfo( + sourceHandle, + ffProbeScan, + { + ...exp.endRequirement.version, + scenes: false, // no scene detection + }, + (progress) => { + hasGottenProgress = true + workInProgress._reportProgress(sourceVersionHash, 0.21 + 0.77 * progress) + }, + worker.logger.category('scanMoreInfo') + ) + + scanMoreInfoProcessSecondTry.then( + (result) => resolve(result), + (error) => reject(error) + ) + } + }, progressTimeout * 0.5) + }) const result = await currentProcess - isDone = true + resultBlacks = result.blacks resultFreezes = result.freezes resultScenes = result.scenes