-
Notifications
You must be signed in to change notification settings - Fork 391
Add task to download files from helix results container #2103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2e31c0b
Add task to download files from helix results container
safern b0fcfd9
PR Feedback
safern ae6703b
Fix warning conditions to only be emitted when feature is ON
safern 85c8b65
Implement ICancelableTask
safern 3e862cb
Add more detailed warning when blob not found
safern 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
116 changes: 116 additions & 0 deletions
116
src/Microsoft.DotNet.Helix/Sdk/DownloadFromResultsContainer.cs
This file contains hidden or 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,116 @@ | ||
| using Microsoft.Build.Framework; | ||
| using Microsoft.WindowsAzure.Storage; | ||
| using Microsoft.WindowsAzure.Storage.Auth; | ||
| using Microsoft.WindowsAzure.Storage.Blob; | ||
| using System; | ||
| using System.IO; | ||
| using System.Linq; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace Microsoft.DotNet.Helix.Sdk | ||
| { | ||
| public class DownloadFromResultsContainer : BaseTask, ICancelableTask | ||
| { | ||
| [Required] | ||
| public ITaskItem[] WorkItems { get; set; } | ||
|
|
||
| [Required] | ||
| public string ResultsContainer { get; set; } | ||
|
|
||
| [Required] | ||
| public string OutputDirectory { get; set; } | ||
|
|
||
| [Required] | ||
| public string JobId { get; set; } | ||
|
|
||
| [Required] | ||
| public ITaskItem[] MetadataToWrite { get; set; } | ||
|
|
||
| public string ResultsContainerReadSAS { get; set; } | ||
|
|
||
| private const string MetadataFile = "metadata.txt"; | ||
|
|
||
| private readonly CancellationTokenSource _cancellationSource = new CancellationTokenSource(); | ||
|
|
||
| public void Cancel() => _cancellationSource.Cancel(); | ||
|
|
||
| public override bool Execute() | ||
| { | ||
| if (string.IsNullOrEmpty(ResultsContainer)) | ||
| { | ||
| LogRequiredParameterError(nameof(ResultsContainer)); | ||
| } | ||
|
|
||
| if (string.IsNullOrEmpty(OutputDirectory)) | ||
| { | ||
| LogRequiredParameterError(nameof(OutputDirectory)); | ||
| } | ||
|
|
||
| if (string.IsNullOrEmpty(JobId)) | ||
| { | ||
| LogRequiredParameterError(nameof(JobId)); | ||
| } | ||
|
|
||
| if (!Log.HasLoggedErrors) | ||
| { | ||
| Log.LogMessage(MessageImportance.High, $"Downloading result files for job {JobId}"); | ||
| ExecuteCore().GetAwaiter().GetResult(); | ||
| } | ||
|
|
||
| return !Log.HasLoggedErrors; | ||
| } | ||
|
|
||
| private async Task ExecuteCore() | ||
| { | ||
| DirectoryInfo directory = Directory.CreateDirectory(Path.Combine(OutputDirectory, JobId)); | ||
| using (FileStream stream = File.Open(Path.Combine(directory.FullName, MetadataFile), FileMode.Create, FileAccess.Write)) | ||
| using (var writer = new StreamWriter(stream)) | ||
| { | ||
| foreach (ITaskItem metadata in MetadataToWrite) | ||
| { | ||
| await writer.WriteLineAsync(metadata.GetMetadata("Identity")); | ||
| } | ||
| } | ||
|
|
||
| ResultsContainer = ResultsContainer.EndsWith("/") ? ResultsContainer : ResultsContainer + "/"; | ||
| await Task.WhenAll(WorkItems.Select(wi => DownloadFilesForWorkItem(wi, directory.FullName, _cancellationSource.Token))); | ||
| return; | ||
| } | ||
|
|
||
| private async Task DownloadFilesForWorkItem(ITaskItem workItem, string directoryPath, CancellationToken ct) | ||
| { | ||
| ct.ThrowIfCancellationRequested(); | ||
|
|
||
| if (workItem.TryGetMetadata("DownloadFilesFromResults", out string files)) | ||
| { | ||
| string workItemName = workItem.GetMetadata("Identity"); | ||
| string[] filesToDownload = files.Split(';'); | ||
|
|
||
| DirectoryInfo destinationDir = Directory.CreateDirectory(Path.Combine(directoryPath, workItemName)); | ||
| foreach (var file in filesToDownload) | ||
| { | ||
| try | ||
| { | ||
| string destinationFile = Path.Combine(destinationDir.FullName, file); | ||
| Log.LogMessage(MessageImportance.Normal, $"Downloading {file} => {destinationFile}..."); | ||
|
|
||
| var uri = new Uri($"{ResultsContainer}{workItemName}/{file}"); | ||
| CloudBlob blob = string.IsNullOrEmpty(ResultsContainerReadSAS) ? new CloudBlob(uri) : new CloudBlob(uri, new StorageCredentials(ResultsContainerReadSAS)); | ||
| await blob.DownloadToFileAsync(destinationFile, FileMode.Create); | ||
| } | ||
| catch (StorageException e) | ||
| { | ||
| Log.LogWarning($"Failed to download {workItemName}/{file} blob from results container: {e.Message}"); | ||
| } | ||
| } | ||
| }; | ||
| return; | ||
| } | ||
|
|
||
| private void LogRequiredParameterError(string parameter) | ||
| { | ||
| Log.LogError($"Required parameter {parameter} string was null or empty"); | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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
5 changes: 5 additions & 0 deletions
5
src/Microsoft.DotNet.Helix/Sdk/tools/download-results/DownloadFromResultsContainer.props
This file contains hidden or 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,5 @@ | ||
| <Project> | ||
| <PropertyGroup> | ||
| <_HelixMultiQueueTargets>$(_HelixMultiQueueTargets);$(MSBuildThisFileDirectory)DownloadFromResultsContainer.targets</_HelixMultiQueueTargets> | ||
| </PropertyGroup> | ||
| </Project> |
33 changes: 33 additions & 0 deletions
33
src/Microsoft.DotNet.Helix/Sdk/tools/download-results/DownloadFromResultsContainer.targets
This file contains hidden or 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,33 @@ | ||
| <Project> | ||
| <Target Name="DownloadFromResultsContainer" | ||
| Condition="$(WaitForWorkItemCompletion)" | ||
| AfterTargets="CoreTest" | ||
| Inputs="unused" | ||
| Outputs="%(SentJob.Identity)"> | ||
| <ItemGroup> | ||
| <_workItemsWithDownloadMetadata Include="@(HelixWorkItem)" Condition="'%(HelixWorkItem.DownloadFilesFromResults)' != ''" /> | ||
|
|
||
| <HelixDownloadResultsMetadata Include="HelixQueue=%(SentJob.HelixTargetQueue)" /> | ||
| <HelixDownloadResultsMetadata Condition="'$(HelixConfiguration)' != ''" Include="Configuration=$(HelixConfiguration)" /> | ||
| <HelixDownloadResultsMetadata Condition="'$(HelixArchitecture)' != ''" Include="Architecture=$(HelixArchitecture)" /> | ||
| </ItemGroup> | ||
|
|
||
| <PropertyGroup> | ||
| <HelixResultsDestinationDir Condition="'$(HelixResultsDestinationDir)' == '' AND '$(BUILD_SOURCESDIRECTORY)' != ''">$([MSBuild]::NormalizePath('$(BUILD_SOURCESDIRECTORY)', 'artifacts', helixresults'))</HelixResultsDestinationDir> | ||
|
|
||
| <_shouldDownloadResults>false</_shouldDownloadResults> | ||
| <_shouldDownloadResults Condition="'@(_workItemsWithDownloadMetadata)' != '' AND '$(HelixResultsDestinationDir)' != ''">true</_shouldDownloadResults> | ||
| </PropertyGroup> | ||
|
|
||
| <Warning Text="DownloadFromResultsContainer will be skipped for job %(SentJob.Identity) becaue results container uri is empty" Condition="'%(SentJob.ResultsContainerUri)' == '' AND $(_shouldDownloadResults)" /> | ||
|
|
||
| <DownloadFromResultsContainer | ||
| Condition="$(_shouldDownloadResults) AND '%(SentJob.ResultsContainerUri)' != ''" | ||
| WorkItems="@(_workItemsWithDownloadMetadata)" | ||
| ResultsContainer="%(SentJob.ResultsContainerUri)" | ||
| OutputDirectory="$(HelixResultsDestinationDir)" | ||
| MetadataToWrite="@(HelixDownloadResultsMetadata)" | ||
| JobId="%(SentJob.Identity)" | ||
| ResultsContainerReadSAS="%(SentJob.ResultsContainerReadSAS)" /> | ||
| </Target> | ||
| </Project> | ||
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.
I think you actually need to empty these item groups before adding things to them. IIRC this will currently write the metadata for jobs 1 and 2 together in the file for 2 because the item group has been filled with both.
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 haven't seen this behavior locally, but it doesn't hurt to empty this item group.
Actually the metadata itemgroup was correctly written for multiple jobs. No duplication at all.
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.
Hrm, I wonder if there is some msbuild magic with batching that is making the items not show up.
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.
Maybe because the ItemGroup is within the scope of the target and since we're batching it creates 2 different copies and evaluations of it? Basically 2 different scopes? @ericstj might know.
Uh oh!
There was an error while loading. Please reload this page.
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 believe the side-effects of a batched target aren't observable until after all batches of that target run.
Try this:
Output of this is:
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.
Since we don't intend to use this items outside the target I guess we're safe on leaving it as is.