From 85c8b655761f349fe1914101e1e2a90a47d8d96c Mon Sep 17 00:00:00 2001 From: Santiago Fernandez Madero Date: Tue, 26 Feb 2019 16:16:41 -0800 Subject: [PATCH] Implement ICancelableTask --- .../Sdk/DownloadFromResultsContainer.cs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.DotNet.Helix/Sdk/DownloadFromResultsContainer.cs b/src/Microsoft.DotNet.Helix/Sdk/DownloadFromResultsContainer.cs index 19e3a460122..7cc8ff1f518 100644 --- a/src/Microsoft.DotNet.Helix/Sdk/DownloadFromResultsContainer.cs +++ b/src/Microsoft.DotNet.Helix/Sdk/DownloadFromResultsContainer.cs @@ -5,11 +5,12 @@ using System; using System.IO; using System.Linq; +using System.Threading; using System.Threading.Tasks; namespace Microsoft.DotNet.Helix.Sdk { - public class DownloadFromResultsContainer : BaseTask + public class DownloadFromResultsContainer : BaseTask, ICancelableTask { [Required] public ITaskItem[] WorkItems { get; set; } @@ -30,6 +31,10 @@ public class DownloadFromResultsContainer : BaseTask 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)) @@ -69,12 +74,14 @@ private async Task ExecuteCore() } ResultsContainer = ResultsContainer.EndsWith("/") ? ResultsContainer : ResultsContainer + "/"; - await Task.WhenAll(WorkItems.Select(wi => DownloadFilesForWorkItem(wi, directory.FullName))); + await Task.WhenAll(WorkItems.Select(wi => DownloadFilesForWorkItem(wi, directory.FullName, _cancellationSource.Token))); return; } - private async Task DownloadFilesForWorkItem(ITaskItem workItem, string directoryPath) + private async Task DownloadFilesForWorkItem(ITaskItem workItem, string directoryPath, CancellationToken ct) { + ct.ThrowIfCancellationRequested(); + if (workItem.TryGetMetadata("DownloadFilesFromResults", out string files)) { string workItemName = workItem.GetMetadata("Identity");