Switch to Parallel.ForEach for list processing with MaxParallelism

(cherry picked from commit 0f93e04186f24abdb0cf0b3ba6a3505fda834e06)
pull/2859/head
Qstick 1 year ago committed by Bogdan
parent 3ec5d9b9fe
commit ebb2b4eca3

@ -3,7 +3,6 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using NLog; using NLog;
using NzbDrone.Common.TPL;
using NzbDrone.Core.Parser.Model; using NzbDrone.Core.Parser.Model;
namespace NzbDrone.Core.ImportLists namespace NzbDrone.Core.ImportLists
@ -41,10 +40,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);
@ -56,12 +52,10 @@ namespace NzbDrone.Core.ImportLists
if (DateTime.UtcNow < importListNextSync) if (DateTime.UtcNow < 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); _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;
} }
} }
var task = taskFactory.StartNew(() =>
{
try try
{ {
var importListReports = importListLocal.Fetch(); var importListReports = importListLocal.Fetch();
@ -79,12 +73,7 @@ namespace NzbDrone.Core.ImportLists
{ {
_logger.Error(e, "Error during Import List Sync of {0} ({1})", importList.Name, importListLocal.Definition.Name); _logger.Error(e, "Error during Import List Sync of {0} ({1})", importList.Name, importListLocal.Definition.Name);
} }
}).LogExceptions(); });
taskList.Add(task);
}
Task.WaitAll(taskList.ToArray());
result = result.DistinctBy(r => new { r.Author, r.Book }).ToList(); result = result.DistinctBy(r => new { r.Author, r.Book }).ToList();
@ -105,13 +94,8 @@ namespace NzbDrone.Core.ImportLists
return result; return result;
} }
var taskList = new List<Task>();
var taskFactory = new TaskFactory(TaskCreationOptions.LongRunning, TaskContinuationOptions.None);
var importListLocal = importList; var importListLocal = importList;
var task = taskFactory.StartNew(() =>
{
try try
{ {
var importListReports = importListLocal.Fetch(); var importListReports = importListLocal.Fetch();
@ -129,11 +113,6 @@ namespace NzbDrone.Core.ImportLists
{ {
_logger.Error(e, "Error during Import List Sync of {0} ({1})", importList.Name, importListLocal.Definition.Name); _logger.Error(e, "Error during Import List Sync of {0} ({1})", importList.Name, importListLocal.Definition.Name);
} }
}).LogExceptions();
taskList.Add(task);
Task.WaitAll(taskList.ToArray());
result = result.DistinctBy(r => new { r.Author, r.Book }).ToList(); result = result.DistinctBy(r => new { r.Author, r.Book }).ToList();

Loading…
Cancel
Save