Small memory improvements in the Plex Sync

pull/2067/head
Jamie 7 years ago
parent 3e2cdb9823
commit 241e6fca67

@ -112,7 +112,7 @@ namespace Ombi.Schedule.Jobs.Plex
Logger.LogInformation("We found {0} items", allContent.Count); Logger.LogInformation("We found {0} items", allContent.Count);
// Let's now process this. // Let's now process this.
var contentToAdd = new List<PlexServerContent>(); var contentToAdd = new HashSet<PlexServerContent>();
foreach (var content in allContent) foreach (var content in allContent)
{ {
if (content.viewGroup.Equals(PlexMediaType.Show.ToString(), StringComparison.CurrentCultureIgnoreCase)) if (content.viewGroup.Equals(PlexMediaType.Show.ToString(), StringComparison.CurrentCultureIgnoreCase))
@ -291,6 +291,11 @@ namespace Ombi.Schedule.Jobs.Plex
show.title); show.title);
} }
} }
if (contentToAdd.Count > 500)
{
await Repo.AddRange(contentToAdd);
contentToAdd.Clear();
}
} }
} }
if (content.viewGroup.Equals(PlexMediaType.Movie.ToString(), StringComparison.CurrentCultureIgnoreCase)) if (content.viewGroup.Equals(PlexMediaType.Movie.ToString(), StringComparison.CurrentCultureIgnoreCase))
@ -356,12 +361,18 @@ namespace Ombi.Schedule.Jobs.Plex
Logger.LogError(LoggingEvents.PlexContentCacher, e, "Exception when adding new Movie {0}", Logger.LogError(LoggingEvents.PlexContentCacher, e, "Exception when adding new Movie {0}",
movie.title); movie.title);
} }
if (contentToAdd.Count > 500)
{
await Repo.AddRange(contentToAdd);
contentToAdd.Clear();
}
} }
} }
if (contentToAdd.Count > 500) if (contentToAdd.Count > 500)
{ {
await Repo.AddRange(contentToAdd); await Repo.AddRange(contentToAdd);
contentToAdd = new List<PlexServerContent>(); contentToAdd.Clear();
} }
} }

@ -62,7 +62,6 @@ namespace Ombi.Schedule.Jobs.Plex
{ {
if (!Validate(settings)) if (!Validate(settings))
{ {
_log.LogWarning("Validation failed"); _log.LogWarning("Validation failed");
return; return;
} }
@ -101,21 +100,25 @@ namespace Ombi.Schedule.Jobs.Plex
{ {
var currentPosition = 0; var currentPosition = 0;
var resultCount = settings.EpisodeBatchSize == 0 ? 150 : settings.EpisodeBatchSize; var resultCount = settings.EpisodeBatchSize == 0 ? 150 : settings.EpisodeBatchSize;
var currentEpisodes = _repo.GetAllEpisodes();
var episodes = await _api.GetAllEpisodes(settings.PlexAuthToken, settings.FullUri, section.key, currentPosition, resultCount); var episodes = await _api.GetAllEpisodes(settings.PlexAuthToken, settings.FullUri, section.key, currentPosition, resultCount);
_log.LogInformation(LoggingEvents.PlexEpisodeCacher, $"Total Epsiodes found for {episodes.MediaContainer.librarySectionTitle} = {episodes.MediaContainer.totalSize}"); _log.LogInformation(LoggingEvents.PlexEpisodeCacher, $"Total Epsiodes found for {episodes.MediaContainer.librarySectionTitle} = {episodes.MediaContainer.totalSize}");
// Delete all the episodes because we cannot uniquly match an episode to series every time, // Delete all the episodes because we cannot uniquly match an episode to series every time,
// see comment below. // see comment below.
await _repo.ExecuteSql("DELETE FROM PlexEpisode");
await ProcessEpsiodes(episodes); // 12.03.2017 - I think we should be able to match them now
//await _repo.ExecuteSql("DELETE FROM PlexEpisode");
await ProcessEpsiodes(episodes, currentEpisodes);
currentPosition += resultCount; currentPosition += resultCount;
while (currentPosition < episodes.MediaContainer.totalSize) while (currentPosition < episodes.MediaContainer.totalSize)
{ {
var ep = await _api.GetAllEpisodes(settings.PlexAuthToken, settings.FullUri, section.key, currentPosition, var ep = await _api.GetAllEpisodes(settings.PlexAuthToken, settings.FullUri, section.key, currentPosition,
resultCount); resultCount);
await ProcessEpsiodes(ep);
await ProcessEpsiodes(ep, currentEpisodes);
_log.LogInformation(LoggingEvents.PlexEpisodeCacher, $"Processed {resultCount} more episodes. Total Remaining {episodes.MediaContainer.totalSize - currentPosition}"); _log.LogInformation(LoggingEvents.PlexEpisodeCacher, $"Processed {resultCount} more episodes. Total Remaining {episodes.MediaContainer.totalSize - currentPosition}");
currentPosition += resultCount; currentPosition += resultCount;
} }
@ -125,58 +128,56 @@ namespace Ombi.Schedule.Jobs.Plex
await _repo.SaveChangesAsync(); await _repo.SaveChangesAsync();
} }
private async Task ProcessEpsiodes(PlexContainer episodes) private async Task ProcessEpsiodes(PlexContainer episodes, IQueryable<PlexEpisode> currentEpisodes)
{ {
var ep = new HashSet<PlexEpisode>(); var ep = new HashSet<PlexEpisode>();
try try
{ {
foreach (var episode in episodes?.MediaContainer?.Metadata ?? new Metadata[] { })
foreach (var episode in episodes?.MediaContainer?.Metadata ?? new Metadata[]{})
{
// I don't think we need to get the metadata, we only need to get the metadata if we need the provider id (TheTvDbid). Why do we need it for episodes?
// We have the parent and grandparent rating keys to link up to the season and series
//var metadata = _api.GetEpisodeMetaData(server.PlexAuthToken, server.FullUri, episode.ratingKey);
// This does seem to work, it looks like we can somehow get different rating, grandparent and parent keys with episodes. Not sure how.
//var epExists = currentEpisodes.Any(x => episode.ratingKey == x.Key &&
// episode.grandparentRatingKey == x.GrandparentKey);
//if (epExists)
//{
// continue;
//}
// Let's check if we have the parent
var seriesExists = await _repo.GetByKey(episode.grandparentRatingKey);
if (seriesExists == null)
{ {
// Ok let's try and match it to a title. TODO (This is experimental) // I don't think we need to get the metadata, we only need to get the metadata if we need the provider id (TheTvDbid). Why do we need it for episodes?
var seriesMatch = await _repo.GetAll().FirstOrDefaultAsync(x => // We have the parent and grandparent rating keys to link up to the season and series
x.Title.Equals(episode.grandparentTitle, StringComparison.CurrentCultureIgnoreCase)); //var metadata = _api.GetEpisodeMetaData(server.PlexAuthToken, server.FullUri, episode.ratingKey);
if (seriesMatch == null)
// This does seem to work, it looks like we can somehow get different rating, grandparent and parent keys with episodes. Not sure how.
var epExists = currentEpisodes.Any(x => episode.ratingKey == x.Key &&
episode.grandparentRatingKey == x.GrandparentKey);
if (epExists)
{ {
_log.LogWarning(
"The episode title {0} we cannot find the parent series. The episode grandparentKey = {1}, grandparentTitle = {2}",
episode.title, episode.grandparentRatingKey, episode.grandparentTitle);
continue; continue;
} }
// Set the rating key to the correct one // Let's check if we have the parent
episode.grandparentRatingKey = seriesMatch.Key; var seriesExists = await _repo.GetByKey(episode.grandparentRatingKey);
} if (seriesExists == null)
{
// Ok let's try and match it to a title. TODO (This is experimental)
seriesExists = await _repo.GetAll().FirstOrDefaultAsync(x =>
x.Title.Equals(episode.grandparentTitle, StringComparison.CurrentCultureIgnoreCase));
if (seriesExists == null)
{
_log.LogWarning(
"The episode title {0} we cannot find the parent series. The episode grandparentKey = {1}, grandparentTitle = {2}",
episode.title, episode.grandparentRatingKey, episode.grandparentTitle);
continue;
}
ep.Add(new PlexEpisode // Set the rating key to the correct one
{ episode.grandparentRatingKey = seriesExists.Key;
EpisodeNumber = episode.index, }
SeasonNumber = episode.parentIndex,
GrandparentKey = episode.grandparentRatingKey, ep.Add(new PlexEpisode
ParentKey = episode.parentRatingKey, {
Key = episode.ratingKey, EpisodeNumber = episode.index,
Title = episode.title SeasonNumber = episode.parentIndex,
}); GrandparentKey = episode.grandparentRatingKey,
} ParentKey = episode.parentRatingKey,
Key = episode.ratingKey,
Title = episode.title
});
}
await _repo.AddRange(ep); await _repo.AddRange(ep);
} }
catch (Exception e) catch (Exception e)
{ {
@ -189,7 +190,7 @@ namespace Ombi.Schedule.Jobs.Plex
{ {
if (string.IsNullOrEmpty(settings.PlexAuthToken)) if (string.IsNullOrEmpty(settings.PlexAuthToken))
{ {
return false ; return false;
} }
return true; return true;

Loading…
Cancel
Save