From d0c994a9253b570b60568d333ae826786c2a21b9 Mon Sep 17 00:00:00 2001 From: "Jamie.Rees" Date: Mon, 3 Apr 2017 11:14:22 +0100 Subject: [PATCH] Added a retry policy around the emby newsletter --- .../EmbyRecentlyAddedNewsletter.cs | 106 ++++++++++-------- Ombi.Services/Ombi.Services.csproj | 1 + 2 files changed, 63 insertions(+), 44 deletions(-) diff --git a/Ombi.Services/Jobs/RecentlyAddedNewsletter/EmbyRecentlyAddedNewsletter.cs b/Ombi.Services/Jobs/RecentlyAddedNewsletter/EmbyRecentlyAddedNewsletter.cs index b3830fc58..5dda578ca 100644 --- a/Ombi.Services/Jobs/RecentlyAddedNewsletter/EmbyRecentlyAddedNewsletter.cs +++ b/Ombi.Services/Jobs/RecentlyAddedNewsletter/EmbyRecentlyAddedNewsletter.cs @@ -45,6 +45,7 @@ using Ombi.Store.Models.Emby; using Ombi.Store.Repository; using TMDbLib.Objects.Exceptions; using EmbyMediaType = Ombi.Store.Models.Plex.EmbyMediaType; +using Polly; namespace Ombi.Services.Jobs.RecentlyAddedNewsletter { @@ -121,14 +122,20 @@ namespace Ombi.Services.Jobs.RecentlyAddedNewsletter var info = new List(); foreach (var m in filteredMovies) { - - var i = Api.GetInformation(m.EmbyId, Ombi.Api.Models.Emby.EmbyMediaType.Movie, - embySettings.ApiKey, embySettings.AdministratorId, embySettings.FullUri); - info.Add(new EmbyRecentlyAddedModel + var policy = RetryHandler.RetryAndWaitPolicy((exception, timespan) => + Log.Error(exception, "Exception thrown when processing an emby movie for the newsletter, Retrying {0}", timespan)); + var result = policy.Execute(() => { - EmbyInformation = i, - EmbyContent = m + var i = Api.GetInformation(m.EmbyId, Ombi.Api.Models.Emby.EmbyMediaType.Movie, + embySettings.ApiKey, embySettings.AdministratorId, embySettings.FullUri); + + return new EmbyRecentlyAddedModel + { + EmbyInformation = i, + EmbyContent = m + }; }); + info.Add(result); } GenerateMovieHtml(info, sb); newsletter.MovieCount = info.Count; @@ -142,46 +149,49 @@ namespace Ombi.Services.Jobs.RecentlyAddedNewsletter var recentlyAddedModel = new List(); foreach (var embyEpisodes in filteredEp) { - // Let's sleep, Emby can't keep up with us. - Thread.Sleep(1000); try { + var policy = RetryHandler.RetryAndWaitPolicy((exception, timespan) => + Log.Error(exception, "Exception thrown when processing an emby episode for the newsletter, Retrying {0}", timespan)); - // Find related series item - var relatedSeries = series.FirstOrDefault(x => x.EmbyId == embyEpisodes.ParentId); - - if (relatedSeries == null) + policy.Execute(() => { - continue; - } + // Find related series item + var relatedSeries = series.FirstOrDefault(x => x.EmbyId == embyEpisodes.ParentId); - // Get series information - var i = Api.GetInformation(relatedSeries.EmbyId, Ombi.Api.Models.Emby.EmbyMediaType.Series, - embySettings.ApiKey, embySettings.AdministratorId, embySettings.FullUri); + if (relatedSeries == null) + { + return; + } - Thread.Sleep(200); - var episodeInfo = Api.GetInformation(embyEpisodes.EmbyId, - Ombi.Api.Models.Emby.EmbyMediaType.Episode, - embySettings.ApiKey, embySettings.AdministratorId, embySettings.FullUri); - - // Check if we already have this series - var existingSeries = recentlyAddedModel.FirstOrDefault(x => - x.EmbyInformation.SeriesInformation.Id.Equals(i.SeriesInformation.Id, - StringComparison.CurrentCultureIgnoreCase)); + // Get series information + var i = Api.GetInformation(relatedSeries.EmbyId, Ombi.Api.Models.Emby.EmbyMediaType.Series, + embySettings.ApiKey, embySettings.AdministratorId, embySettings.FullUri); - if (existingSeries != null) - { - existingSeries.EpisodeInformation.Add(episodeInfo.EpisodeInformation); - } - else - { - recentlyAddedModel.Add(new EmbyRecentlyAddedModel + Thread.Sleep(200); + var episodeInfo = Api.GetInformation(embyEpisodes.EmbyId, + Ombi.Api.Models.Emby.EmbyMediaType.Episode, + embySettings.ApiKey, embySettings.AdministratorId, embySettings.FullUri); + + // Check if we already have this series + var existingSeries = recentlyAddedModel.FirstOrDefault(x => + x.EmbyInformation.SeriesInformation.Id.Equals(i.SeriesInformation.Id, + StringComparison.CurrentCultureIgnoreCase)); + + if (existingSeries != null) { - EmbyInformation = i, - EpisodeInformation = new List() { episodeInfo.EpisodeInformation }, - EmbyContent = relatedSeries - }); - } + existingSeries.EpisodeInformation.Add(episodeInfo.EpisodeInformation); + } + else + { + recentlyAddedModel.Add(new EmbyRecentlyAddedModel + { + EmbyInformation = i, + EpisodeInformation = new List() { episodeInfo.EpisodeInformation }, + EmbyContent = relatedSeries + }); + } + }); } catch (JsonReaderException) @@ -197,13 +207,21 @@ namespace Ombi.Services.Jobs.RecentlyAddedNewsletter { foreach (var t in filteredSeries) { - var i = Api.GetInformation(t.EmbyId, Ombi.Api.Models.Emby.EmbyMediaType.Series, - embySettings.ApiKey, embySettings.AdministratorId, embySettings.FullUri); - var item = new EmbyRecentlyAddedModel + + + var policy = RetryHandler.RetryAndWaitPolicy((exception, timespan) => + Log.Error(exception, "Exception thrown when processing an emby series for the newsletter, Retrying {0}", timespan)); + var item = policy.Execute(() => { - EmbyContent = t, - EmbyInformation = i, - }; + var i = Api.GetInformation(t.EmbyId, Ombi.Api.Models.Emby.EmbyMediaType.Series, + embySettings.ApiKey, embySettings.AdministratorId, embySettings.FullUri); + var model = new EmbyRecentlyAddedModel + { + EmbyContent = t, + EmbyInformation = i, + }; + return model; + }); info.Add(item); } } diff --git a/Ombi.Services/Ombi.Services.csproj b/Ombi.Services/Ombi.Services.csproj index 725f1a4ab..d3ca25f5d 100644 --- a/Ombi.Services/Ombi.Services.csproj +++ b/Ombi.Services/Ombi.Services.csproj @@ -48,6 +48,7 @@ ..\packages\NLog.4.3.6\lib\net45\NLog.dll True +