Added a retry policy around the emby newsletter

pull/1334/head
Jamie.Rees 8 years ago
parent 59f3ce35f3
commit d0c994a925

@ -45,6 +45,7 @@ using Ombi.Store.Models.Emby;
using Ombi.Store.Repository; using Ombi.Store.Repository;
using TMDbLib.Objects.Exceptions; using TMDbLib.Objects.Exceptions;
using EmbyMediaType = Ombi.Store.Models.Plex.EmbyMediaType; using EmbyMediaType = Ombi.Store.Models.Plex.EmbyMediaType;
using Polly;
namespace Ombi.Services.Jobs.RecentlyAddedNewsletter namespace Ombi.Services.Jobs.RecentlyAddedNewsletter
{ {
@ -121,14 +122,20 @@ namespace Ombi.Services.Jobs.RecentlyAddedNewsletter
var info = new List<EmbyRecentlyAddedModel>(); var info = new List<EmbyRecentlyAddedModel>();
foreach (var m in filteredMovies) foreach (var m in filteredMovies)
{ {
var policy = RetryHandler.RetryAndWaitPolicy((exception, timespan) =>
var i = Api.GetInformation(m.EmbyId, Ombi.Api.Models.Emby.EmbyMediaType.Movie, Log.Error(exception, "Exception thrown when processing an emby movie for the newsletter, Retrying {0}", timespan));
embySettings.ApiKey, embySettings.AdministratorId, embySettings.FullUri); var result = policy.Execute(() =>
info.Add(new EmbyRecentlyAddedModel
{ {
EmbyInformation = i, var i = Api.GetInformation(m.EmbyId, Ombi.Api.Models.Emby.EmbyMediaType.Movie,
EmbyContent = m embySettings.ApiKey, embySettings.AdministratorId, embySettings.FullUri);
return new EmbyRecentlyAddedModel
{
EmbyInformation = i,
EmbyContent = m
};
}); });
info.Add(result);
} }
GenerateMovieHtml(info, sb); GenerateMovieHtml(info, sb);
newsletter.MovieCount = info.Count; newsletter.MovieCount = info.Count;
@ -142,46 +149,49 @@ namespace Ombi.Services.Jobs.RecentlyAddedNewsletter
var recentlyAddedModel = new List<EmbyRecentlyAddedModel>(); var recentlyAddedModel = new List<EmbyRecentlyAddedModel>();
foreach (var embyEpisodes in filteredEp) foreach (var embyEpisodes in filteredEp)
{ {
// Let's sleep, Emby can't keep up with us.
Thread.Sleep(1000);
try 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 policy.Execute(() =>
var relatedSeries = series.FirstOrDefault(x => x.EmbyId == embyEpisodes.ParentId);
if (relatedSeries == null)
{ {
continue; // Find related series item
} var relatedSeries = series.FirstOrDefault(x => x.EmbyId == embyEpisodes.ParentId);
// Get series information if (relatedSeries == null)
var i = Api.GetInformation(relatedSeries.EmbyId, Ombi.Api.Models.Emby.EmbyMediaType.Series, {
embySettings.ApiKey, embySettings.AdministratorId, embySettings.FullUri); return;
}
Thread.Sleep(200); // Get series information
var episodeInfo = Api.GetInformation(embyEpisodes.EmbyId, var i = Api.GetInformation(relatedSeries.EmbyId, Ombi.Api.Models.Emby.EmbyMediaType.Series,
Ombi.Api.Models.Emby.EmbyMediaType.Episode, embySettings.ApiKey, embySettings.AdministratorId, embySettings.FullUri);
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) Thread.Sleep(200);
{ var episodeInfo = Api.GetInformation(embyEpisodes.EmbyId,
existingSeries.EpisodeInformation.Add(episodeInfo.EpisodeInformation); Ombi.Api.Models.Emby.EmbyMediaType.Episode,
} embySettings.ApiKey, embySettings.AdministratorId, embySettings.FullUri);
else
{ // Check if we already have this series
recentlyAddedModel.Add(new EmbyRecentlyAddedModel var existingSeries = recentlyAddedModel.FirstOrDefault(x =>
x.EmbyInformation.SeriesInformation.Id.Equals(i.SeriesInformation.Id,
StringComparison.CurrentCultureIgnoreCase));
if (existingSeries != null)
{ {
EmbyInformation = i, existingSeries.EpisodeInformation.Add(episodeInfo.EpisodeInformation);
EpisodeInformation = new List<EmbyEpisodeInformation>() { episodeInfo.EpisodeInformation }, }
EmbyContent = relatedSeries else
}); {
} recentlyAddedModel.Add(new EmbyRecentlyAddedModel
{
EmbyInformation = i,
EpisodeInformation = new List<EmbyEpisodeInformation>() { episodeInfo.EpisodeInformation },
EmbyContent = relatedSeries
});
}
});
} }
catch (JsonReaderException) catch (JsonReaderException)
@ -197,13 +207,21 @@ namespace Ombi.Services.Jobs.RecentlyAddedNewsletter
{ {
foreach (var t in filteredSeries) 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, var i = Api.GetInformation(t.EmbyId, Ombi.Api.Models.Emby.EmbyMediaType.Series,
EmbyInformation = i, embySettings.ApiKey, embySettings.AdministratorId, embySettings.FullUri);
}; var model = new EmbyRecentlyAddedModel
{
EmbyContent = t,
EmbyInformation = i,
};
return model;
});
info.Add(item); info.Add(item);
} }
} }

@ -48,6 +48,7 @@
<HintPath>..\packages\NLog.4.3.6\lib\net45\NLog.dll</HintPath> <HintPath>..\packages\NLog.4.3.6\lib\net45\NLog.dll</HintPath>
<Private>True</Private> <Private>True</Private>
</Reference> </Reference>
<Reference Include="Polly, Version=4.3.0.0, Culture=neutral, PublicKeyToken=c8a3ffc3f8f825cc" />
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Core" /> <Reference Include="System.Core" />
<Reference Include="System.Security" /> <Reference Include="System.Security" />

Loading…
Cancel
Save