Add exponential backoff retry to all downstream calls

New controller/retry.js wraps async calls with infinite retries, capped

at 30s with jitter. Applied to: TPB search, TMDB, Emby, Ollama chat,

Transmission RPC, and the status/status interval calls inherit it via

a Proxy around the Transmission client.

Also handles ollama package returning pre-parsed JSON objects.

Signed-off-by: William Mantly <wmantly@gmail.com>
This commit is contained in:
2026-07-20 14:44:41 -04:00
parent 803a6db033
commit 51e6817393
8 changed files with 147 additions and 51 deletions
+5 -15
View File
@@ -1,23 +1,13 @@
'use strict';
const conf = require('>/conf');
async function fetchJSON(url, options){
let res = await fetch(url, options);
if(!res.ok){
let error = new Error('UpstreamError');
error.message = `Request to ${url.split('?')[0]} failed( ${res.status})`;
error.status = 502;
throw error;
}
return await res.json();
}
const { fetchJSONWithRetry } = require('./retry');
async function tmdbSearch(query){
let url = `https://api.themoviedb.org/3/search/multi?include_adult=false`
+ `&query=${encodeURIComponent(query)}&api_key=${conf.tmdb.apiKey}`;
let data = await fetchJSON(url);
let data = await fetchJSONWithRetry(url);
return (data.results || [])
.filter(item => item.media_type === 'movie' || item.media_type === 'tv')
@@ -38,7 +28,7 @@ async function tmdbDetails(tmdbId, mediaType){
let url = `https://api.themoviedb.org/3/${mediaType}/${tmdbId}`
+ `?append_to_response=external_ids&api_key=${conf.tmdb.apiKey}`;
let data = await fetchJSON(url);
let data = await fetchJSONWithRetry(url);
let date = data.release_date || data.first_air_date || '';
return {
@@ -71,7 +61,7 @@ async function tmdbFindBest(title, year, mediaType){
// Per-season episode counts for a show( season 0 / specials excluded).
async function tmdbSeasons(tmdbId){
let data = await fetchJSON(`https://api.themoviedb.org/3/tv/${tmdbId}?api_key=${conf.tmdb.apiKey}`);
let data = await fetchJSONWithRetry(`https://api.themoviedb.org/3/tv/${tmdbId}?api_key=${conf.tmdb.apiKey}`);
let date = data.first_air_date || '';
return {
title: data.name,
@@ -82,4 +72,4 @@ async function tmdbSeasons(tmdbId){
};
}
module.exports = { fetchJSON, tmdbSearch, tmdbDetails, tmdbFindBest, tmdbSeasons };
module.exports = { tmdbSearch, tmdbDetails, tmdbFindBest, tmdbSeasons };