Merge pull request #260 from Mbucari/master
Throttle episode scanning to 10 concurrent scans.
This commit is contained in:
commit
99b77decff
@ -1,6 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using AudibleApi;
|
using AudibleApi;
|
||||||
using AudibleApi.Common;
|
using AudibleApi.Common;
|
||||||
@ -119,18 +120,19 @@ namespace AudibleUtilities
|
|||||||
{
|
{
|
||||||
var items = new List<Item>();
|
var items = new List<Item>();
|
||||||
|
|
||||||
Serilog.Log.Logger.Debug("Begin library scan");
|
Serilog.Log.Logger.Debug("Beginning library scan.");
|
||||||
|
|
||||||
List<Task<List<Item>>> getChildEpisodesTasks = new();
|
List<Task<List<Item>>> getChildEpisodesTasks = new();
|
||||||
|
|
||||||
int count = 0;
|
int count = 0, maxConcurrentEpisodeScans = 5;
|
||||||
|
using SemaphoreSlim concurrencySemaphore = new(maxConcurrentEpisodeScans);
|
||||||
|
|
||||||
await foreach (var item in Api.GetLibraryItemAsyncEnumerable(libraryOptions))
|
await foreach (var item in Api.GetLibraryItemAsyncEnumerable(libraryOptions))
|
||||||
{
|
{
|
||||||
if (item.IsEpisodes && importEpisodes)
|
if (item.IsEpisodes && importEpisodes)
|
||||||
{
|
{
|
||||||
//Get child episodes asynchronously and await all at the end
|
//Get child episodes asynchronously and await all at the end
|
||||||
getChildEpisodesTasks.Add(getChildEpisodesAsync(item));
|
getChildEpisodesTasks.Add(getChildEpisodesAsync(concurrencySemaphore, item));
|
||||||
}
|
}
|
||||||
else if (!item.IsEpisodes)
|
else if (!item.IsEpisodes)
|
||||||
items.Add(item);
|
items.Add(item);
|
||||||
@ -138,13 +140,13 @@ namespace AudibleUtilities
|
|||||||
count++;
|
count++;
|
||||||
}
|
}
|
||||||
|
|
||||||
Serilog.Log.Logger.Debug("Library scan complete. Found {count} books. Waiting on episode scans to complete", count);
|
Serilog.Log.Logger.Debug("Library scan complete. Found {count} books and series. Waiting on {getChildEpisodesTasksCount} series episode scans to complete.", count, getChildEpisodesTasks.Count);
|
||||||
|
|
||||||
//await and add all episides from all parents
|
//await and add all episides from all parents
|
||||||
foreach (var epList in await Task.WhenAll(getChildEpisodesTasks))
|
foreach (var epList in await Task.WhenAll(getChildEpisodesTasks))
|
||||||
items.AddRange(epList);
|
items.AddRange(epList);
|
||||||
|
|
||||||
Serilog.Log.Logger.Debug("Scan complete");
|
Serilog.Log.Logger.Debug("Completed library scan.");
|
||||||
|
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
//System.IO.File.WriteAllText(library_json, AudibleApi.Common.Converter.ToJson(items));
|
//System.IO.File.WriteAllText(library_json, AudibleApi.Common.Converter.ToJson(items));
|
||||||
@ -163,7 +165,11 @@ namespace AudibleUtilities
|
|||||||
|
|
||||||
#region episodes and podcasts
|
#region episodes and podcasts
|
||||||
|
|
||||||
private async Task<List<Item>> getChildEpisodesAsync(Item parent)
|
private async Task<List<Item>> getChildEpisodesAsync(SemaphoreSlim concurrencySemaphore, Item parent)
|
||||||
|
{
|
||||||
|
await concurrencySemaphore.WaitAsync();
|
||||||
|
|
||||||
|
try
|
||||||
{
|
{
|
||||||
Serilog.Log.Logger.Debug("Beginning episode scan for {parent}", parent);
|
Serilog.Log.Logger.Debug("Beginning episode scan for {parent}", parent);
|
||||||
|
|
||||||
@ -199,8 +205,16 @@ namespace AudibleUtilities
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Serilog.Log.Logger.Debug("Completed episode scan for {parent}", parent);
|
||||||
|
|
||||||
return children;
|
return children;
|
||||||
}
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
concurrencySemaphore.Release();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private async Task<List<Item>> getEpisodeChildrenAsync(Item parent)
|
private async Task<List<Item>> getEpisodeChildrenAsync(Item parent)
|
||||||
{
|
{
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user