Skip to content

Commit

Permalink
feat: add CancellationToken support for RunBuild.Exec
Browse files Browse the repository at this point in the history
  • Loading branch information
filzrev committed Sep 4, 2024
1 parent b9ba33b commit a5a88d0
Show file tree
Hide file tree
Showing 22 changed files with 186 additions and 101 deletions.
4 changes: 2 additions & 2 deletions src/Docfx.App/Helpers/DocumentBuilderWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ internal static class DocumentBuilderWrapper
{
private static readonly Assembly[] s_pluginAssemblies = LoadPluginAssemblies(AppContext.BaseDirectory).ToArray();

public static void BuildDocument(BuildJsonConfig config, BuildOptions options, TemplateManager templateManager, string baseDirectory, string outputDirectory, string templateDirectory)
public static void BuildDocument(BuildJsonConfig config, BuildOptions options, TemplateManager templateManager, string baseDirectory, string outputDirectory, string templateDirectory, CancellationToken cancellationToken)
{
var postProcessorNames = config.PostProcessors.ToImmutableArray();
var metadata = config.GlobalMetadata?.ToImmutableDictionary();
Expand All @@ -39,7 +39,7 @@ public static void BuildDocument(BuildJsonConfig config, BuildOptions options, T
using var builder = new DocumentBuilder(s_pluginAssemblies.Concat(pluginAssemblies), postProcessorNames);

var parameters = ConfigToParameter(config, options, templateManager, baseDirectory, outputDirectory, templateDirectory);
builder.Build(parameters, outputDirectory);
builder.Build(parameters, outputDirectory, cancellationToken);
}

private static IEnumerable<Assembly> LoadPluginAssemblies(string pluginDirectory)
Expand Down
4 changes: 2 additions & 2 deletions src/Docfx.App/RunBuild.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ internal static class RunBuild
/// <summary>
/// Build document with specified settings.
/// </summary>
public static string Exec(BuildJsonConfig config, BuildOptions options, string configDirectory, string outputDirectory = null)
public static string Exec(BuildJsonConfig config, BuildOptions options, string configDirectory, string outputDirectory = null, CancellationToken cancellationToken = default)
{
var stopwatch = Stopwatch.StartNew();
if (config.Template == null || config.Template.Count == 0)
Expand All @@ -36,7 +36,7 @@ public static string Exec(BuildJsonConfig config, BuildOptions options, string c
{
var templateManager = new TemplateManager(config.Template, config.Theme, configDirectory);

DocumentBuilderWrapper.BuildDocument(config, options, templateManager, baseDirectory, outputFolder, null);
DocumentBuilderWrapper.BuildDocument(config, options, templateManager, baseDirectory, outputFolder, null, cancellationToken);

templateManager.ProcessTheme(outputFolder, true);
}
Expand Down
20 changes: 12 additions & 8 deletions src/Docfx.Build/CompilePhaseHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void Handle(List<HostService> hostServices, int maxParallelism)
_restructions.AddRange(hostService.TableOfContentRestructions);
}
}
}, maxParallelism);
}, maxParallelism, Context.CancellationToken);

DistributeTocRestructions(hostServices);

Expand All @@ -49,7 +49,7 @@ public void Handle(List<HostService> hostServices, int maxParallelism)
Build(hostService, maxParallelism);
}

hostServices.RunAll(Postbuild, maxParallelism);
hostServices.RunAll(Postbuild, maxParallelism, Context.CancellationToken);
}

private void Prepare(List<HostService> hostServices, int maxParallelism)
Expand All @@ -66,7 +66,8 @@ private void Prepare(List<HostService> hostServices, int maxParallelism)
{
m.LocalPathFromRoot ??= StringExtension.ToDisplayPath(Path.Combine(m.BaseDir, m.File));
},
maxParallelism);
maxParallelism,
Context.CancellationToken);
}
}

Expand All @@ -83,7 +84,7 @@ private void DistributeTocRestructions(List<HostService> hostServices)
}
}

private static void Prebuild(HostService hostService)
private void Prebuild(HostService hostService)
{
RunBuildSteps(
hostService.Processor.BuildSteps,
Expand All @@ -99,7 +100,7 @@ private static void Prebuild(HostService hostService)
});
}

private static void Build(HostService hostService, int maxParallelism)
private void Build(HostService hostService, int maxParallelism)
{
hostService.Models.RunAll(
m =>
Expand All @@ -124,10 +125,11 @@ private static void Build(HostService hostService, int maxParallelism)
});
}
},
maxParallelism);
maxParallelism,
Context.CancellationToken);
}

private static void Postbuild(HostService hostService)
private void Postbuild(HostService hostService)
{
hostService.Reload(hostService.Models);

Expand All @@ -140,12 +142,14 @@ private static void Postbuild(HostService hostService)
});
}

private static void RunBuildSteps(IEnumerable<IDocumentBuildStep> buildSteps, Action<IDocumentBuildStep> action)
private void RunBuildSteps(IEnumerable<IDocumentBuildStep> buildSteps, Action<IDocumentBuildStep> action)
{
if (buildSteps != null)
{
foreach (var buildStep in buildSteps.OrderBy(static step => step.BuildOrder))
{
Context.CancellationToken.ThrowIfCancellationRequested();

action(buildStep);
}
}
Expand Down
34 changes: 21 additions & 13 deletions src/Docfx.Build/DocumentBuildContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,7 @@ public sealed class DocumentBuildContext : IDocumentBuildContext
private readonly ConcurrentDictionary<string, TocInfo> _tableOfContents = new(FilePathComparer.OSPlatformSensitiveStringComparer);
private readonly Task<IXRefContainerReader> _reader;

public DocumentBuildContext(string buildOutputFolder)
: this(buildOutputFolder, Enumerable.Empty<FileAndType>(), ImmutableArray<string>.Empty, ImmutableArray<string>.Empty, 1, Directory.GetCurrentDirectory(), string.Empty, null, null) { }

public DocumentBuildContext(string buildOutputFolder, IEnumerable<FileAndType> allSourceFiles, ImmutableArray<string> externalReferencePackages, ImmutableArray<string> xrefMaps, int maxParallelism, string baseFolder, string versionName, ApplyTemplateSettings applyTemplateSetting, string rootTocPath)
: this(buildOutputFolder, allSourceFiles, externalReferencePackages, xrefMaps, maxParallelism, baseFolder, versionName, applyTemplateSetting, rootTocPath, null, null) { }

public DocumentBuildContext(DocumentBuildParameters parameters)
public DocumentBuildContext(DocumentBuildParameters parameters, CancellationToken cancellationToken)
{
BuildOutputFolder = Path.Combine(Path.GetFullPath(EnvironmentContext.BaseDirectory), parameters.OutputBaseDir);
VersionName = parameters.VersionName;
Expand All @@ -34,9 +28,10 @@ public DocumentBuildContext(DocumentBuildParameters parameters)

if (parameters.XRefMaps.Length > 0)
{
// Note: `_reader` task is processed asyncronously and await is called later. So OperationCancellationException is not thrown by this lines.
_reader = new XRefCollection(
from u in parameters.XRefMaps
select new Uri(u, UriKind.RelativeOrAbsolute)).GetReaderAsync(parameters.Files.DefaultBaseDir, parameters.MarkdownEngineParameters?.FallbackFolders);
select new Uri(u, UriKind.RelativeOrAbsolute)).GetReaderAsync(parameters.Files.DefaultBaseDir, parameters.MarkdownEngineParameters?.FallbackFolders, cancellationToken);
}
RootTocPath = parameters.RootTocPath;

Expand All @@ -54,9 +49,18 @@ from u in parameters.XRefMaps
}
}
VersionFolder = versionDir;
CancellationToken = cancellationToken;
}

public DocumentBuildContext(
#region Constructors that used by test code.

internal DocumentBuildContext(string buildOutputFolder)
: this(buildOutputFolder, Enumerable.Empty<FileAndType>(), ImmutableArray<string>.Empty, ImmutableArray<string>.Empty, 1, Directory.GetCurrentDirectory(), string.Empty, null, null) { }

private DocumentBuildContext(string buildOutputFolder, IEnumerable<FileAndType> allSourceFiles, ImmutableArray<string> externalReferencePackages, ImmutableArray<string> xrefMaps, int maxParallelism, string baseFolder, string versionName, ApplyTemplateSettings applyTemplateSetting, string rootTocPath)
: this(buildOutputFolder, allSourceFiles, externalReferencePackages, xrefMaps, maxParallelism, baseFolder, versionName, applyTemplateSetting, rootTocPath, null, null) { }

private DocumentBuildContext(
string buildOutputFolder,
IEnumerable<FileAndType> allSourceFiles,
ImmutableArray<string> externalReferencePackages,
Expand Down Expand Up @@ -98,6 +102,7 @@ from u in xrefMaps
}
VersionFolder = versionFolder;
}
#endregion

public string BuildOutputFolder { get; }

Expand Down Expand Up @@ -129,6 +134,8 @@ from u in xrefMaps

public ICustomHrefGenerator HrefGenerator { get; }

public CancellationToken CancellationToken { get; } = CancellationToken.None;

internal ConcurrentBag<ManifestItem> ManifestItems { get; } = new();

private ConcurrentDictionary<string, XRefSpec> ExternalXRefSpec { get; } = new();
Expand All @@ -150,9 +157,10 @@ public void ReportExternalXRefSpec(XRefSpec spec)

public void ResolveExternalXRefSpec()
{
Task.WaitAll(
Task.WaitAll([
Task.Run(ResolveExternalXRefSpecForSpecs),
Task.Run(ResolveExternalXRefSpecForNoneSpecsAsync));
Task.Run(ResolveExternalXRefSpecForNoneSpecsAsync)
], CancellationToken);
}

private void ResolveExternalXRefSpecForSpecs()
Expand Down Expand Up @@ -205,7 +213,7 @@ private List<string> ResolveByExternalReferencePackages(List<string> uidList, Co

var oldSpecCount = externalXRefSpec.Count;
var list = new List<string>();
using (var externalReferences = new ExternalReferencePackageCollection(ExternalReferencePackages, MaxParallelism))
using (var externalReferences = new ExternalReferencePackageCollection(ExternalReferencePackages, MaxParallelism, CancellationToken))
{
foreach (var uid in uidList)
{
Expand Down Expand Up @@ -401,7 +409,7 @@ public XRefSpec GetXrefSpec(string uid)

if (ExternalReferencePackages.Length > 0)
{
using (var externalReferences = new ExternalReferencePackageCollection(ExternalReferencePackages, MaxParallelism))
using (var externalReferences = new ExternalReferencePackageCollection(ExternalReferencePackages, MaxParallelism, CancellationToken))
{
xref = GetExternalReference(externalReferences, uid);
}
Expand Down
6 changes: 4 additions & 2 deletions src/Docfx.Build/DocumentBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,12 @@ public void Build(DocumentBuildParameters parameter)
Build(new DocumentBuildParameters[] { parameter }, parameter.OutputBaseDir);
}

public void Build(IList<DocumentBuildParameters> parameters, string outputDirectory)
public void Build(IList<DocumentBuildParameters> parameters, string outputDirectory, CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(parameters);

cancellationToken.ThrowIfCancellationRequested();

if (parameters.Count == 0)
{
throw new ArgumentException("Parameters are empty.", nameof(parameters));
Expand Down Expand Up @@ -119,7 +121,7 @@ public void Build(IList<DocumentBuildParameters> parameters, string outputDirect
MetadataValidators = MetadataValidators.ToList(),
Processors = Processors,
};
manifests.Add(builder.Build(parameter, markdownService));
manifests.Add(builder.Build(parameter, markdownService, cancellationToken));
}
}
if (noContentFound)
Expand Down
4 changes: 3 additions & 1 deletion src/Docfx.Build/HostServiceCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ bool NeedApplyMetadata()
{
invalidFiles.Add(file.File);
}
}, _context.MaxParallelism);
},
_context.MaxParallelism,
_context.CancellationToken);

return (models.OrderBy(m => m.File, StringComparer.Ordinal).ToArray(), invalidFiles);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Docfx.Build/LinkPhaseHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ private IEnumerable<ManifestItemWithContext> ExportManifest(HostService hostServ
}
}
}
});
}, Context.CancellationToken);
return manifestItems;
}

Expand Down Expand Up @@ -136,7 +136,7 @@ private void CheckFileLink(FileModel model, HostService hostService, SaveResult
Logger.LogWarning($"Invalid file link:({fileLink}).", code: WarningCodes.Build.InvalidFileLink);
}
}
});
}, Context.CancellationToken);
}

private void HandleUids(SaveResult result)
Expand Down
12 changes: 8 additions & 4 deletions src/Docfx.Build/ManifestProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ private void NormalizeToObject()
}
}
},
_context.MaxParallelism);
_context.MaxParallelism,
_context.CancellationToken);
}

private void FeedOptions()
Expand All @@ -113,7 +114,8 @@ private void FeedOptions()
}
}
},
_context.MaxParallelism);
_context.MaxParallelism,
_context.CancellationToken);
}

private void UpdateHref()
Expand All @@ -130,7 +132,8 @@ private void UpdateHref()
m.Item.Content = m.FileModel.Content;
}
},
_context.MaxParallelism);
_context.MaxParallelism,
_context.CancellationToken);
}

private void ApplySystemMetadata()
Expand Down Expand Up @@ -171,7 +174,8 @@ private void ApplySystemMetadata()
}
}
},
_context.MaxParallelism);
_context.MaxParallelism,
_context.CancellationToken);

_globalMetadata["_shared"] = sharedObjects;
}
Expand Down
4 changes: 3 additions & 1 deletion src/Docfx.Build/PostProcessors/ExtractSearchIndex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public ImmutableDictionary<string, object> PrepareMetadata(ImmutableDictionary<s
return metadata;
}

public Manifest Process(Manifest manifest, string outputFolder)
public Manifest Process(Manifest manifest, string outputFolder, CancellationToken cancellationToken = default)
{
if (outputFolder == null)
{
Expand All @@ -57,6 +57,8 @@ from output in item.Output
Logger.LogInfo($"Extracting index data from {htmlFiles.Count} html files");
foreach (var relativePath in htmlFiles)
{
cancellationToken.ThrowIfCancellationRequested();

var filePath = Path.Combine(outputFolder, relativePath);
var html = new HtmlDocument();
Logger.LogDiagnostic($"Extracting index data from {filePath}");
Expand Down
4 changes: 3 additions & 1 deletion src/Docfx.Build/PostProcessors/HtmlPostProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public ImmutableDictionary<string, object> PrepareMetadata(ImmutableDictionary<s
return metadata;
}

public Manifest Process(Manifest manifest, string outputFolder)
public Manifest Process(Manifest manifest, string outputFolder, CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(manifest);
ArgumentNullException.ThrowIfNull(outputFolder);
Expand All @@ -58,6 +58,8 @@ where output.Key.Equals(".html", StringComparison.OrdinalIgnoreCase)
OutputFile = output.Value.RelativePath,
})
{
cancellationToken.ThrowIfCancellationRequested();

if (!EnvironmentContext.FileAbstractLayer.Exists(tuple.OutputFile))
{
continue;
Expand Down
4 changes: 2 additions & 2 deletions src/Docfx.Build/PostProcessors/PostProcessorsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ public ImmutableDictionary<string, object> PrepareMetadata(ImmutableDictionary<s
return updatedMetadata;
}

public void Process(Manifest manifest, string outputFolder)
public void Process(Manifest manifest, string outputFolder, CancellationToken cancellationToken = default)
{
foreach (var postProcessor in _postProcessors)
{
manifest = postProcessor.Processor.Process(manifest, outputFolder) ??
manifest = postProcessor.Processor.Process(manifest, outputFolder, cancellationToken) ??
throw new DocfxException($"Post processor {postProcessor.ContractName} should not return null manifest");

// To make sure post processor won't generate duplicate output files
Expand Down
2 changes: 1 addition & 1 deletion src/Docfx.Build/PostProcessors/SitemapGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public ImmutableDictionary<string, object> PrepareMetadata(ImmutableDictionary<s
return metadata;
}

public Manifest Process(Manifest manifest, string outputFolder)
public Manifest Process(Manifest manifest, string outputFolder, CancellationToken cancellationToken = default)
{
if (string.IsNullOrEmpty(manifest.Sitemap?.BaseUrl))
{
Expand Down
Loading

0 comments on commit a5a88d0

Please sign in to comment.