From f718cec23fccbfd16fdb792c2778cd543b751799 Mon Sep 17 00:00:00 2001 From: Gauthier Date: Thu, 2 Jan 2025 16:44:46 +0100 Subject: [PATCH] fix(externalapi): clear cache after a request is made (#1217) This PR clears the Radarr/Sonarr cache after a request has been made, because the media status on Radarr/Sonarr will no longer be good. It also resolves a bug that prevented the media from being deleted after a request had been sent to Radarr/Sonarr. fix #1207 --- server/api/externalapi.ts | 8 ++++++++ server/api/servarr/radarr.ts | 17 +++++++++++++++++ server/api/servarr/sonarr.ts | 24 ++++++++++++++++++++++++ server/entity/MediaRequest.ts | 17 +++++++++++++++++ 4 files changed, 66 insertions(+) diff --git a/server/api/externalapi.ts b/server/api/externalapi.ts index f27752d4c..9d6ef8d4a 100644 --- a/server/api/externalapi.ts +++ b/server/api/externalapi.ts @@ -293,6 +293,14 @@ class ExternalAPI { return data; } + protected removeCache(endpoint: string, params?: Record) { + const cacheKey = this.serializeCacheKey(endpoint, { + ...this.params, + ...params, + }); + this.cache?.del(cacheKey); + } + private formatUrl( endpoint: string, params?: Record, diff --git a/server/api/servarr/radarr.ts b/server/api/servarr/radarr.ts index 51d300374..f3bf3faaf 100644 --- a/server/api/servarr/radarr.ts +++ b/server/api/servarr/radarr.ts @@ -230,6 +230,23 @@ class RadarrAPI extends ServarrBase<{ movieId: number }> { throw new Error(`[Radarr] Failed to remove movie: ${e.message}`); } }; + + public clearCache = ({ + tmdbId, + externalId, + }: { + tmdbId?: number | null; + externalId?: number | null; + }) => { + if (tmdbId) { + this.removeCache('/movie/lookup', { + term: `tmdb:${tmdbId}`, + }); + } + if (externalId) { + this.removeCache(`/movie/${externalId}`); + } + }; } export default RadarrAPI; diff --git a/server/api/servarr/sonarr.ts b/server/api/servarr/sonarr.ts index 8ae054edb..5590c9acb 100644 --- a/server/api/servarr/sonarr.ts +++ b/server/api/servarr/sonarr.ts @@ -353,6 +353,30 @@ class SonarrAPI extends ServarrBase<{ throw new Error(`[Radarr] Failed to remove serie: ${e.message}`); } }; + + public clearCache = ({ + tvdbId, + externalId, + title, + }: { + tvdbId?: number | null; + externalId?: number | null; + title?: string | null; + }) => { + if (tvdbId) { + this.removeCache('/series/lookup', { + term: `tvdb:${tvdbId}`, + }); + } + if (externalId) { + this.removeCache(`/series/${externalId}`); + } + if (title) { + this.removeCache('/series/lookup', { + term: title, + }); + } + }; } export default SonarrAPI; diff --git a/server/entity/MediaRequest.ts b/server/entity/MediaRequest.ts index 60da11764..2f0395c4e 100644 --- a/server/entity/MediaRequest.ts +++ b/server/entity/MediaRequest.ts @@ -1006,6 +1006,14 @@ export class MediaRequest { ); this.sendNotification(media, Notification.MEDIA_FAILED); + }) + .finally(() => { + radarr.clearCache({ + tmdbId: movie.id, + externalId: this.is4k + ? media.externalServiceId4k + : media.externalServiceId, + }); }); logger.info('Sent request to Radarr', { label: 'Media Request', @@ -1288,6 +1296,15 @@ export class MediaRequest { ); this.sendNotification(media, Notification.MEDIA_FAILED); + }) + .finally(() => { + sonarr.clearCache({ + tvdbId, + externalId: this.is4k + ? media.externalServiceId4k + : media.externalServiceId, + title: series.name, + }); }); logger.info('Sent request to Sonarr', { label: 'Media Request',