Skip to content

Commit a63046d

Browse files
authored
Refactor file processing and improve exception handling (#154)
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 a63046d

File tree

2 files changed

+41
-23
lines changed

2 files changed

+41
-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: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -85,33 +85,29 @@ 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
106+
throw;
112107
}
113-
if (item % 1_000 == 0)
114-
_logger.LogInformation($"Handled {handledItems} files");
108+
109+
if (processedFiles % 1_000 == 0)
110+
_logger.LogInformation($"Handled {processedFiles} files");
115111
});
116112

117113
var embeddedStaticFiles = Assembly.GetExecutingAssembly()
@@ -140,13 +136,34 @@ await Parallel.ForEachAsync(DocumentationSet.Files, ctx, async (file, token) =>
140136
await GenerateLinkReference(ctx);
141137

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

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

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

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

0 commit comments

Comments
 (0)