Skip to content

Commit 8f61f62

Browse files
committed
Refactor file processing and improve exception handling
Extracted file processing logic into a dedicated method for cleaner code organization. Enhanced exception handling with a file processing threshold to fail gracefully after multiple errors. Improved logging to provide better progress tracking.
1 parent d4f6654 commit 8f61f62

File tree

2 files changed

+40
-23
lines changed

2 files changed

+40
-23
lines changed

src/Elastic.Markdown/Diagnostics/DiagnosticsChannel.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ public void EmitError(string file, string message, Exception? e = null)
154154
};
155155
Channel.Write(d);
156156
}
157+
157158
public void EmitWarning(string file, string message)
158159
{
159160
var d = new Diagnostic

src/Elastic.Markdown/DocumentationGenerator.cs

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -85,33 +85,28 @@ public async Task GenerateAll(Cancel ctx)
8585
_logger.LogInformation("Resolved tree");
8686

8787

88-
var handledItems = 0;
89-
88+
var processedFileCount = 0;
89+
var exceptionCount = 0;
9090
_ = Context.Collector.StartAsync(ctx);
91-
9291
await Parallel.ForEachAsync(DocumentationSet.Files, ctx, async (file, token) =>
9392
{
94-
if (!Context.Force)
93+
var processedFiles = Interlocked.Increment(ref processedFileCount);
94+
try
9595
{
96-
if (offendingFiles.Contains(file.SourceFile.FullName))
97-
_logger.LogInformation($"Re-evaluating {file.SourceFile.FullName}");
98-
else if (file.SourceFile.LastWriteTimeUtc <= outputSeenChanges)
99-
return;
96+
await ProcessFile(offendingFiles, file, outputSeenChanges, token);
10097
}
101-
102-
_logger.LogTrace($"{file.SourceFile.FullName}");
103-
var item = Interlocked.Increment(ref handledItems);
104-
var outputFile = OutputFile(file.RelativePath);
105-
if (file is MarkdownFile markdown)
106-
await HtmlWriter.WriteAsync(outputFile, markdown, token);
107-
else
98+
catch (Exception e)
10899
{
109-
if (outputFile.Directory is { Exists: false })
110-
outputFile.Directory.Create();
111-
await CopyFileFsAware(file, outputFile, ctx);
100+
var currentCount = Interlocked.Increment(ref exceptionCount);
101+
// this is not the main error logging mechanism
102+
// if we hit this from too many files fail hard
103+
if (currentCount <= 25)
104+
Context.Collector.EmitError(file.RelativePath, "Uncaught exception while processing file", e);
105+
else throw;
112106
}
113-
if (item % 1_000 == 0)
114-
_logger.LogInformation($"Handled {handledItems} files");
107+
108+
if (processedFiles % 1_000 == 0)
109+
_logger.LogInformation($"Handled {processedFiles} files");
115110
});
116111

117112
var embeddedStaticFiles = Assembly.GetExecutingAssembly()
@@ -140,13 +135,34 @@ await Parallel.ForEachAsync(DocumentationSet.Files, ctx, async (file, token) =>
140135
await GenerateLinkReference(ctx);
141136

142137
await Context.Collector.StopAsync(ctx);
138+
}
139+
140+
private async Task ProcessFile(HashSet<string> offendingFiles, DocumentationFile file, DateTimeOffset outputSeenChanges, CancellationToken token)
141+
{
142+
if (!Context.Force)
143+
{
144+
if (offendingFiles.Contains(file.SourceFile.FullName))
145+
_logger.LogInformation($"Re-evaluating {file.SourceFile.FullName}");
146+
else if (file.SourceFile.LastWriteTimeUtc <= outputSeenChanges)
147+
return;
148+
}
143149

144-
IFileInfo OutputFile(string relativePath)
150+
_logger.LogTrace($"{file.SourceFile.FullName}");
151+
var outputFile = OutputFile(file.RelativePath);
152+
if (file is MarkdownFile markdown)
153+
await HtmlWriter.WriteAsync(outputFile, markdown, token);
154+
else
145155
{
146-
var outputFile = _writeFileSystem.FileInfo.New(Path.Combine(DocumentationSet.OutputPath.FullName, relativePath));
147-
return outputFile;
156+
if (outputFile.Directory is { Exists: false })
157+
outputFile.Directory.Create();
158+
await CopyFileFsAware(file, outputFile, token);
148159
}
160+
}
149161

162+
private IFileInfo OutputFile(string relativePath)
163+
{
164+
var outputFile = _writeFileSystem.FileInfo.New(Path.Combine(DocumentationSet.OutputPath.FullName, relativePath));
165+
return outputFile;
150166
}
151167

152168
private bool CompilationNotNeeded(GenerationState? generationState, out HashSet<string> offendingFiles,

0 commit comments

Comments
 (0)