fix: await ForEachAsync

This commit is contained in:
jackfiled 2024-01-25 11:53:08 +08:00
parent 9d68c675cf
commit 0f58e4ce4b

View File

@ -29,8 +29,7 @@ public class RendererService(ILogger<RendererService> logger,
logger.LogInformation("Render essays start."); logger.LogInformation("Render essays start.");
List<BlogContent> contents = await essayScanService.ScanAsync(); List<BlogContent> contents = await essayScanService.ScanAsync();
IEnumerable<BlogContent> preProcessedContents = IEnumerable<BlogContent> preProcessedContents = await PreProcess(contents);
PreProcess(contents);
List<BlogEssay> essays = []; List<BlogEssay> essays = [];
await Task.Run(() => await Task.Run(() =>
@ -59,23 +58,14 @@ public class RendererService(ILogger<RendererService> logger,
ConcurrentBag<BlogEssay> postProcessEssays = []; ConcurrentBag<BlogEssay> postProcessEssays = [];
Parallel.ForEach(essays, essay => Parallel.ForEach(essays, essay =>
{ {
BlogEssay newEssay =
BlogEssay newEssay = new() essay.WithNewHtmlContent(Markdown.ToHtml(essay.HtmlContent, markdownPipeline));
{
Title = essay.Title,
FileName = essay.FileName,
Description = essay.Description,
WordCount = essay.WordCount,
PublishTime = essay.PublishTime,
HtmlContent = Markdown.ToHtml(essay.HtmlContent, markdownPipeline)
};
newEssay.Tags.AddRange(essay.Tags);
postProcessEssays.Add(newEssay); postProcessEssays.Add(newEssay);
logger.LogDebug("Render markdown file {}.", newEssay); logger.LogDebug("Render markdown file {}.", newEssay);
}); });
PostProcess(postProcessEssays); await PostProcess(postProcessEssays);
_stopwatch.Stop(); _stopwatch.Stop();
logger.LogInformation("Render finished, consuming {} s.", logger.LogInformation("Render finished, consuming {} s.",
@ -108,11 +98,11 @@ public class RendererService(ILogger<RendererService> logger,
_postRenderProcessors.Add(processor); _postRenderProcessors.Add(processor);
} }
private IEnumerable<BlogContent> PreProcess(IEnumerable<BlogContent> contents) private async Task<IEnumerable<BlogContent>> PreProcess(IEnumerable<BlogContent> contents)
{ {
ConcurrentBag<BlogContent> processedContents = []; ConcurrentBag<BlogContent> processedContents = [];
Parallel.ForEachAsync(contents, async (content, _) => await Parallel.ForEachAsync(contents, async (content, _) =>
{ {
foreach (var processor in _preRenderProcessors) foreach (var processor in _preRenderProcessors)
{ {
@ -125,9 +115,9 @@ public class RendererService(ILogger<RendererService> logger,
return processedContents; return processedContents;
} }
private void PostProcess(IEnumerable<BlogEssay> essays) private async Task PostProcess(IEnumerable<BlogEssay> essays)
{ {
Parallel.ForEachAsync(essays, async (essay, _) => await Parallel.ForEachAsync(essays, async (essay, _) =>
{ {
foreach (IPostRenderProcessor processor in _postRenderProcessors) foreach (IPostRenderProcessor processor in _postRenderProcessors)
{ {