From 8794b129c6da3fed7d467b16cd958db57a2effe8 Mon Sep 17 00:00:00 2001 From: TheBeastLT Date: Wed, 17 Mar 2021 12:54:55 +0100 Subject: [PATCH] [addon] fix rd retry loop --- addon/lib/cache.js | 2 +- addon/moch/realdebrid.js | 12 +++++++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/addon/lib/cache.js b/addon/lib/cache.js index 042389b..60508e0 100644 --- a/addon/lib/cache.js +++ b/addon/lib/cache.js @@ -7,7 +7,7 @@ const RESOLVED_URL_KEY_PREFIX = `${GLOBAL_KEY_PREFIX}|resolved`; const STREAM_TTL = process.env.STREAM_TTL || 4 * 60 * 60; // 4 hours const STREAM_EMPTY_TTL = process.env.STREAM_EMPTY_TTL || 30 * 60; // 30 minutes -const RESOLVED_URL_TTL = 60; // 2 minutes +const RESOLVED_URL_TTL = 60; // 1 minutes // When the streams are empty we want to cache it for less time in case of timeouts or failures const MONGO_URI = process.env.MONGODB_URI; diff --git a/addon/moch/realdebrid.js b/addon/moch/realdebrid.js index f8968d1..c41b541 100644 --- a/addon/moch/realdebrid.js +++ b/addon/moch/realdebrid.js @@ -196,10 +196,15 @@ async function _createTorrentId(RD, infoHash, cachedFileIds) { return addedMagnet.id; } -async function _retryCreateTorrent(RD, infoHash, fileIndex) { - console.log(`Retry failed download in RealDebrid ${infoHash} [${fileIndex}]...`); +async function _recreateTorrentId(RD, infoHash, fileIndex) { const newTorrentId = await _createTorrentId(RD, infoHash); await _selectTorrentFiles(RD, { id: newTorrentId }, fileIndex); + return newTorrentId; +} + +async function _retryCreateTorrent(RD, infoHash, fileIndex) { + console.log(`Retry failed download in RealDebrid ${infoHash} [${fileIndex}]...`); + const newTorrentId = await _recreateTorrentId(RD, infoHash, fileIndex); const newTorrent = await _getTorrentInfo(RD, newTorrentId); return newTorrent && statusReady(newTorrent.status) ? _unrestrictLink(RD, newTorrent, fileIndex) @@ -230,7 +235,8 @@ async function _unrestrictLink(RD, torrent, fileIndex) { const targetFile = torrent.files.find(file => file.id === fileIndex + 1) || torrent.files.filter(file => file.selected).sort((a, b) => b.bytes - a.bytes)[0]; if (!targetFile.selected) { - await _retryCreateTorrent(RD, torrent.hash.toLowerCase(), fileIndex); + console.log(`Target RealDebrid file is not downloaded: ${JSON.stringify(torrent)}`); + await _recreateTorrentId(RD, torrent.hash.toLowerCase(), fileIndex); return StaticResponse.DOWNLOADING; }