Switch to Parallel.ForEach for list processing with MaxParallelism

pull/9073/head
Qstick 1 year ago
parent fef666831f
commit 0f93e04186

@ -4,7 +4,6 @@ using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using NLog; using NLog;
using NzbDrone.Common.Instrumentation.Extensions; using NzbDrone.Common.Instrumentation.Extensions;
using NzbDrone.Common.TPL;
using NzbDrone.Core.ImportLists.ImportListMovies; using NzbDrone.Core.ImportLists.ImportListMovies;
using NzbDrone.Core.MetadataSource; using NzbDrone.Core.MetadataSource;
using NzbDrone.Core.Movies; using NzbDrone.Core.Movies;
@ -23,7 +22,6 @@ namespace NzbDrone.Core.ImportLists
private readonly IImportListStatusService _importListStatusService; private readonly IImportListStatusService _importListStatusService;
private readonly IImportListMovieService _listMovieService; private readonly IImportListMovieService _listMovieService;
private readonly ISearchForNewMovie _movieSearch; private readonly ISearchForNewMovie _movieSearch;
private readonly IProvideMovieInfo _movieInfoService;
private readonly IMovieMetadataService _movieMetadataService; private readonly IMovieMetadataService _movieMetadataService;
private readonly Logger _logger; private readonly Logger _logger;
@ -31,7 +29,6 @@ namespace NzbDrone.Core.ImportLists
IImportListStatusService importListStatusService, IImportListStatusService importListStatusService,
IImportListMovieService listMovieService, IImportListMovieService listMovieService,
ISearchForNewMovie movieSearch, ISearchForNewMovie movieSearch,
IProvideMovieInfo movieInfoService,
IMovieMetadataService movieMetadataService, IMovieMetadataService movieMetadataService,
Logger logger) Logger logger)
{ {
@ -39,7 +36,6 @@ namespace NzbDrone.Core.ImportLists
_importListStatusService = importListStatusService; _importListStatusService = importListStatusService;
_listMovieService = listMovieService; _listMovieService = listMovieService;
_movieSearch = movieSearch; _movieSearch = movieSearch;
_movieInfoService = movieInfoService;
_movieMetadataService = movieMetadataService; _movieMetadataService = movieMetadataService;
_logger = logger; _logger = logger;
} }
@ -58,10 +54,7 @@ namespace NzbDrone.Core.ImportLists
_logger.Debug("Available import lists {0}", importLists.Count); _logger.Debug("Available import lists {0}", importLists.Count);
var taskList = new List<Task>(); Parallel.ForEach(importLists, new ParallelOptions { MaxDegreeOfParallelism = 5 }, importList =>
var taskFactory = new TaskFactory(TaskCreationOptions.LongRunning, TaskContinuationOptions.None);
foreach (var importList in importLists)
{ {
var importListLocal = importList; var importListLocal = importList;
var importListStatus = _importListStatusService.GetLastSyncListInfo(importListLocal.Definition.Id); var importListStatus = _importListStatusService.GetLastSyncListInfo(importListLocal.Definition.Id);
@ -74,7 +67,7 @@ namespace NzbDrone.Core.ImportLists
{ {
_logger.Trace("Skipping refresh of Import List {0} ({1}) due to minimum refresh interval. Next Sync after {2}", importList.Name, importListLocal.Definition.Name, importListNextSync); _logger.Trace("Skipping refresh of Import List {0} ({1}) due to minimum refresh interval. Next Sync after {2}", importList.Name, importListLocal.Definition.Name, importListNextSync);
continue; return;
} }
} }
@ -86,48 +79,41 @@ namespace NzbDrone.Core.ImportLists
{ {
_logger.Debug("Temporarily ignoring Import List {0} ({1}) till {2} due to recent failures.", importList.Name, importListLocal.Definition.Name, blockedListStatus.DisabledTill.Value.ToLocalTime()); _logger.Debug("Temporarily ignoring Import List {0} ({1}) till {2} due to recent failures.", importList.Name, importListLocal.Definition.Name, blockedListStatus.DisabledTill.Value.ToLocalTime());
result.AnyFailure |= true; // Ensure we don't clean if a list is down result.AnyFailure |= true; // Ensure we don't clean if a list is down
continue; return;
} }
var task = taskFactory.StartNew(() => try
{ {
try var importListReports = importListLocal.Fetch();
lock (result)
{ {
var importListReports = importListLocal.Fetch(); _logger.Debug("Found {0} from Import List {1} ({2})", importListReports.Movies.Count, importList.Name, importListLocal.Definition.Name);
lock (result) if (!importListReports.AnyFailure)
{ {
_logger.Debug("Found {0} from Import List {1} ({2})", importListReports.Movies.Count, importList.Name, importListLocal.Definition.Name); var alreadyMapped = result.Movies.Where(x => importListReports.Movies.Any(r => r.TmdbId == x.TmdbId));
var listMovies = MapMovieReports(importListReports.Movies.Where(x => result.Movies.All(r => r.TmdbId != x.TmdbId)).ToList()).Where(x => x.TmdbId > 0).ToList();
if (!importListReports.AnyFailure)
{
var alreadyMapped = result.Movies.Where(x => importListReports.Movies.Any(r => r.TmdbId == x.TmdbId));
var listMovies = MapMovieReports(importListReports.Movies.Where(x => result.Movies.All(r => r.TmdbId != x.TmdbId)).ToList()).Where(x => x.TmdbId > 0).ToList();
listMovies.AddRange(alreadyMapped);
listMovies = listMovies.DistinctBy(x => x.TmdbId).ToList();
listMovies.ForEach(m => m.ListId = importList.Definition.Id);
result.Movies.AddRange(listMovies); listMovies.AddRange(alreadyMapped);
_listMovieService.SyncMoviesForList(listMovies, importList.Definition.Id); listMovies = listMovies.DistinctBy(x => x.TmdbId).ToList();
} listMovies.ForEach(m => m.ListId = importList.Definition.Id);
result.AnyFailure |= importListReports.AnyFailure; result.Movies.AddRange(listMovies);
result.SyncedLists++; _listMovieService.SyncMoviesForList(listMovies, importList.Definition.Id);
_importListStatusService.UpdateListSyncStatus(importList.Definition.Id);
} }
}
catch (Exception e)
{
_logger.Error(e, "Error during Import List Sync of {0} ({1})", importList.Name, importListLocal.Definition.Name);
}
}).LogExceptions();
taskList.Add(task); result.AnyFailure |= importListReports.AnyFailure;
} result.SyncedLists++;
Task.WaitAll(taskList.ToArray()); _importListStatusService.UpdateListSyncStatus(importList.Definition.Id);
}
}
catch (Exception e)
{
_logger.Error(e, "Error during Import List Sync of {0} ({1})", importList.Name, importListLocal.Definition.Name);
}
});
result.Movies = result.Movies.DistinctBy(r => new { r.TmdbId, r.ImdbId, r.Title }).ToList(); result.Movies = result.Movies.DistinctBy(r => new { r.TmdbId, r.ImdbId, r.Title }).ToList();

Loading…
Cancel
Save