This repository has been archived by the owner on Nov 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 520
Build queue for --watch #1189
Merged
Merged
Build queue for --watch #1189
Changes from 4 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
cebf823
Central build queue, w/ build from solution
06bdc5d
Fix some warnings
5c47ab4
Need to use -target and not -targets
7f518b5
Remove unnecessary function
298a24c
Handle failures building solution better
6835a9b
Merge branch 'dotnet:main' into build-queue
james-allan-lloyd 55129eb
Merge branch 'main' into build-queue
philliphoff 1616660
Styling updates.
philliphoff 3736765
Scaffold explicit start/stop of builds.
philliphoff e825a0e
More styling updates.
philliphoff 8ff450c
Cleanup working directory.
philliphoff 181a239
Make build result more consistent.
philliphoff b562362
Tweak logging.
philliphoff 1711ea5
Update formatting.
philliphoff 85440d5
Update more formatting.
philliphoff bb1ae72
Add locks for critical areas.
philliphoff b901e2d
Update schema and reference doc.
philliphoff 3acd282
Fix formatting.
philliphoff 5a7b5cd
Updates per PR feedback.
philliphoff 931558a
More updates per PR feedback.
philliphoff File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading.Channels; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Build.Construction; | ||
|
||
namespace Microsoft.Tye.Hosting | ||
{ | ||
class WatchBuilderWorker | ||
{ | ||
private readonly ILogger _logger; | ||
private readonly Channel<BuildRequest> _queue; | ||
private Task _processor; | ||
private string? _solutionPath; | ||
|
||
public string? SolutionPath { get => _solutionPath; set => _solutionPath = value; } | ||
|
||
public WatchBuilderWorker(ILogger logger) | ||
{ | ||
_logger = logger; | ||
_queue = Channel.CreateUnbounded<BuildRequest>(); | ||
_processor = Task.Run(ProcessTaskQueueAsync); | ||
} | ||
|
||
class BuildRequest | ||
{ | ||
public BuildRequest(string projectFilePath, string workingDirectory) | ||
{ | ||
this.projectFilePath = projectFilePath; | ||
this.workingDirectory = workingDirectory; | ||
} | ||
|
||
public string projectFilePath { get; set; } | ||
|
||
public string workingDirectory { get; set; } | ||
|
||
private TaskCompletionSource<int> _result = new TaskCompletionSource<int>(); | ||
|
||
public Task<int> task() | ||
{ | ||
return _result.Task; | ||
} | ||
|
||
public void complete(int exitCode) | ||
{ | ||
if(!_result.TrySetResult(exitCode)) | ||
throw new Exception("failed to set result"); | ||
} | ||
} | ||
|
||
public Task<int> buildProjectFileAsync(string projectFilePath, string workingDirectory) { | ||
var buildRequest = new BuildRequest(projectFilePath, workingDirectory); | ||
_queue.Writer.WriteAsync(buildRequest); | ||
return buildRequest.task(); | ||
} | ||
|
||
public Task<int> buildProjectFileAsyncImpl(string projectFilePath, string workingDirectory) { | ||
_logger.LogInformation($"Building project ${projectFilePath}..."); | ||
return ProcessUtil.RunAsync("dotnet", $"build \"{projectFilePath}\" /nologo", throwOnError: false, workingDirectory: workingDirectory) | ||
.ContinueWith((processTask) => { | ||
var buildResult = processTask.Result; | ||
if (buildResult.ExitCode != 0) | ||
{ | ||
_logger.LogInformation("Building projects failed with exit code {ExitCode}: \r\n" + buildResult.StandardOutput, buildResult.ExitCode); | ||
} | ||
return buildResult.ExitCode; | ||
}); | ||
} | ||
|
||
private string GetProjectName(SolutionFile solution, string projectFile) | ||
{ | ||
foreach(var project in solution.ProjectsInOrder) { | ||
if(project.AbsolutePath == projectFile) | ||
{ | ||
return project.ProjectName; | ||
} | ||
} | ||
// TODO: error | ||
return ""; | ||
} | ||
|
||
private async Task ProcessTaskQueueAsync() | ||
{ | ||
try | ||
{ | ||
while (await _queue.Reader.WaitToReadAsync()) | ||
{ | ||
var solutionBatch = new Dictionary<string, List<BuildRequest>>(); // store the list of promises | ||
var projectBatch = new Dictionary<string, List<BuildRequest>>(); | ||
// TODO: quiet time... maybe wait both...? | ||
await Task.Delay(100); | ||
|
||
var solution = (SolutionPath != null) ? SolutionFile.Parse(SolutionPath) : null; | ||
string targets = ""; | ||
string workingDirectory = ""; // FIXME: should be set in the worker constructor | ||
while (_queue.Reader.TryRead(out BuildRequest item)) | ||
{ | ||
try { | ||
if(workingDirectory.Length == 0) | ||
{ | ||
workingDirectory = item.workingDirectory; | ||
} | ||
if(solution != null && solution.ProjectShouldBuild(item.projectFilePath)) | ||
{ | ||
if(!solutionBatch.ContainsKey(item.projectFilePath)) | ||
{ | ||
if(targets.Length > 0) | ||
{ | ||
targets += ","; | ||
} | ||
targets += GetProjectName(solution, item.projectFilePath); // note, assuming the default target is Build | ||
solutionBatch.Add(item.projectFilePath, new List<BuildRequest>()); | ||
} | ||
solutionBatch[item.projectFilePath].Add(item); | ||
} | ||
else | ||
{ | ||
// this will also prevent us building multiple times if a project is used by multiple services | ||
if(!projectBatch.ContainsKey(item.projectFilePath)) | ||
{ | ||
projectBatch.Add(item.projectFilePath, new List<BuildRequest>()); | ||
} | ||
projectBatch[item.projectFilePath].Add(item); | ||
} | ||
} | ||
catch (Exception) | ||
{ | ||
item.complete(-1); | ||
} | ||
} | ||
|
||
var tasks = new List<Task>(); | ||
if(solutionBatch.Count > 0) | ||
{ | ||
tasks.Add(Task.Run(async () => { | ||
_logger.LogInformation("Building projects from solution: " + targets); | ||
var buildResult = await ProcessUtil.RunAsync("dotnet", $"msbuild {SolutionPath} -target:{targets}", throwOnError: false, workingDirectory: workingDirectory); | ||
if (buildResult.ExitCode != 0) | ||
{ | ||
_logger.LogInformation("Building solution failed with exit code {ExitCode}: \r\n" + buildResult.StandardOutput, buildResult.ExitCode); | ||
} | ||
foreach(var project in solutionBatch) | ||
{ | ||
foreach(var buildRequest in project.Value) | ||
{ | ||
buildRequest.complete(buildResult.ExitCode); | ||
} | ||
} | ||
})); | ||
} | ||
else | ||
{ | ||
foreach(var project in projectBatch) | ||
{ | ||
// FIXME: this is serial | ||
tasks.Add(Task.Run(async () => { | ||
var exitCode = await buildProjectFileAsyncImpl(project.Key, workingDirectory); | ||
foreach(var buildRequest in project.Value) | ||
{ | ||
buildRequest.complete(exitCode); | ||
} | ||
})); | ||
} | ||
} | ||
|
||
Task.WaitAll(tasks.ToArray()); | ||
} | ||
} | ||
catch (OperationCanceledException) | ||
{ | ||
// Prevent throwing if stoppingToken was signaled | ||
} | ||
catch (Exception ex) | ||
{ | ||
_logger.LogError(ex, "Error occurred executing task work item."); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is there a delay here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had the idea of having a quiet time where we wait to see if more files will be saved, then schedule the builds at once... it would be better to have it waiting 100ms (or a more sensible duration) after the last WaitToReadAsync(). Unsure what would happen if it was removed; might still need to yield to let duplicate project build requests enter the queue?