Skip to content

Commit 58a3de5

Browse files
authored
Pass CancellationToken from commands (#1530)
1 parent 8216adf commit 58a3de5

File tree

9 files changed

+60
-42
lines changed

9 files changed

+60
-42
lines changed

src/Microsoft.ComponentDetection.Orchestrator/Commands/ListDetectorsCommand.cs

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,12 @@ namespace Microsoft.ComponentDetection.Orchestrator.Commands;
99
/// <summary>
1010
/// Lists available detectors.
1111
/// </summary>
12-
public sealed class ListDetectorsCommand : Command<ListDetectorsSettings>
12+
/// <param name="detectors">The detectors.</param>
13+
/// <param name="console">The console.</param>
14+
public sealed class ListDetectorsCommand(
15+
IEnumerable<IComponentDetector> detectors,
16+
IAnsiConsole console) : Command<ListDetectorsSettings>
1317
{
14-
private readonly IEnumerable<IComponentDetector> detectors;
15-
private readonly IAnsiConsole console;
16-
17-
/// <summary>
18-
/// Initializes a new instance of the <see cref="ListDetectorsCommand"/> class.
19-
/// </summary>
20-
/// <param name="detectors">The detectors.</param>
21-
/// <param name="console">The console.</param>
22-
public ListDetectorsCommand(
23-
IEnumerable<IComponentDetector> detectors,
24-
IAnsiConsole console)
25-
{
26-
this.detectors = detectors;
27-
this.console = console;
28-
}
29-
3018
/// <inheritdoc/>
3119
public override int Execute(
3220
CommandContext context,
@@ -36,12 +24,12 @@ public override int Execute(
3624
var table = new Table();
3725
table.AddColumn("Name");
3826

39-
foreach (var detector in this.detectors)
27+
foreach (var detector in detectors)
4028
{
4129
table.AddRow(detector.Id);
4230
}
4331

44-
this.console.Write(table);
32+
console.Write(table);
4533

4634
return (int)ProcessingResultCode.Success;
4735
}

src/Microsoft.ComponentDetection.Orchestrator/Commands/ListDetectorsSettings.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,4 @@ namespace Microsoft.ComponentDetection.Orchestrator.Commands;
33
/// <summary>
44
/// Settings for the ListDetectors command.
55
/// </summary>
6-
public class ListDetectorsSettings : BaseSettings
7-
{
8-
}
6+
public class ListDetectorsSettings : BaseSettings;

src/Microsoft.ComponentDetection.Orchestrator/Commands/ScanCommand.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public override async Task<int> ExecuteAsync(
4444
CancellationToken cancellationToken)
4545
{
4646
this.fileWritingService.Init(settings.Output);
47-
var result = await this.scanExecutionService.ExecuteScanAsync(settings);
47+
var result = await this.scanExecutionService.ExecuteScanAsync(settings, cancellationToken);
4848
this.WriteComponentManifest(settings, result);
4949
return 0;
5050
}
@@ -53,11 +53,12 @@ public override async Task<int> ExecuteAsync(
5353
/// Method to provide a way to execute the scan command and obtain the ScanResult object.
5454
/// </summary>
5555
/// <param name="settings">ScanSettings object specifying the parameters for the scan execution.</param>
56+
/// <param name="cancellationToken">CancellationToken to monitor for cancellation requests.</param>
5657
/// <returns>A ScanResult object.</returns>
57-
public async Task<ScanResult> ExecuteScanCommandAsync(ScanSettings settings)
58+
public async Task<ScanResult> ExecuteScanCommandAsync(ScanSettings settings, CancellationToken cancellationToken = default)
5859
{
5960
this.fileWritingService.Init(settings.Output);
60-
var result = await this.scanExecutionService.ExecuteScanAsync(settings);
61+
var result = await this.scanExecutionService.ExecuteScanAsync(settings, cancellationToken);
6162
this.WriteComponentManifest(settings, result);
6263
return result;
6364
}

src/Microsoft.ComponentDetection.Orchestrator/Services/DetectorProcessingService.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,12 @@ public DetectorProcessingService(
4141
this.logger = logger;
4242
}
4343

44+
/// <inheritdoc/>
4445
public async Task<DetectorProcessingResult> ProcessDetectorsAsync(
4546
ScanSettings settings,
4647
IEnumerable<IComponentDetector> detectors,
47-
DetectorRestrictions detectorRestrictions)
48+
DetectorRestrictions detectorRestrictions,
49+
CancellationToken cancellationToken = default)
4850
{
4951
using var scope = this.logger.BeginScope("Processing detectors");
5052
this.logger.LogInformation($"Finding components...");
Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,27 @@
11
namespace Microsoft.ComponentDetection.Orchestrator.Services;
22

33
using System.Collections.Generic;
4+
using System.Threading;
45
using System.Threading.Tasks;
56
using Microsoft.ComponentDetection.Contracts;
67
using Microsoft.ComponentDetection.Orchestrator.Commands;
78

9+
/// <summary>
10+
/// Defines a service for processing component detectors during a scan operation.
11+
/// </summary>
812
public interface IDetectorProcessingService
913
{
10-
Task<DetectorProcessingResult> ProcessDetectorsAsync(
14+
/// <summary>
15+
/// Processes the specified detectors asynchronously based on the provided scan settings and detector restrictions.
16+
/// </summary>
17+
/// <param name="settings">The scan settings that configure how the detection process should be executed.</param>
18+
/// <param name="detectors">The collection of component detectors to be processed.</param>
19+
/// <param name="detectorRestrictions">The restrictions that determine which detectors should be included or excluded from processing.</param>
20+
/// <param name="cancellationToken">A token to monitor for cancellation requests.</param>
21+
/// <returns>A task that represents the asynchronous operation. The task result contains the <see cref="DetectorProcessingResult"/> with information about the detection process outcome.</returns>
22+
public Task<DetectorProcessingResult> ProcessDetectorsAsync(
1123
ScanSettings settings,
1224
IEnumerable<IComponentDetector> detectors,
13-
DetectorRestrictions detectorRestrictions);
25+
DetectorRestrictions detectorRestrictions,
26+
CancellationToken cancellationToken = default);
1427
}
Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
11
namespace Microsoft.ComponentDetection.Orchestrator.Services;
22

3+
using System.Threading;
34
using System.Threading.Tasks;
45
using Microsoft.ComponentDetection.Contracts.BcdeModels;
56
using Microsoft.ComponentDetection.Orchestrator.Commands;
67

8+
/// <summary>
9+
/// Defines a service responsible for executing component detection scans.
10+
/// </summary>
711
public interface IScanExecutionService
812
{
9-
Task<ScanResult> ExecuteScanAsync(ScanSettings settings);
13+
/// <summary>
14+
/// Executes a scan asynchronously based on the provided scan settings.
15+
/// </summary>
16+
/// <param name="settings">The scan settings that configure how the scan should be executed.</param>
17+
/// <param name="cancellationToken">A token to monitor for cancellation requests.</param>
18+
/// <returns>A task that represents the asynchronous scan operation. The task result contains the <see cref="ScanResult"/> with information about the scan execution.</returns>
19+
public Task<ScanResult> ExecuteScanAsync(ScanSettings settings, CancellationToken cancellationToken = default);
1020
}

src/Microsoft.ComponentDetection.Orchestrator/Services/ScanExecutionService.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ namespace Microsoft.ComponentDetection.Orchestrator.Services;
44
using System.Collections.Generic;
55
using System.Collections.Immutable;
66
using System.Linq;
7+
using System.Threading;
78
using System.Threading.Tasks;
89
using Microsoft.ComponentDetection.Contracts;
910
using Microsoft.ComponentDetection.Contracts.BcdeModels;
@@ -33,7 +34,8 @@ public ScanExecutionService(
3334
this.logger = logger;
3435
}
3536

36-
public async Task<ScanResult> ExecuteScanAsync(ScanSettings settings)
37+
/// <inheritdoc />
38+
public async Task<ScanResult> ExecuteScanAsync(ScanSettings settings, CancellationToken cancellationToken = default)
3739
{
3840
using var scope = this.logger.BeginScope("Executing BCDE scan");
3941

@@ -44,7 +46,7 @@ public async Task<ScanResult> ExecuteScanAsync(ScanSettings settings)
4446

4547
this.logger.LogDebug("Finished applying restrictions to detectors.");
4648

47-
var processingResult = await this.detectorProcessingService.ProcessDetectorsAsync(settings, detectorsWithAppliedRestrictions, detectorRestrictions);
49+
var processingResult = await this.detectorProcessingService.ProcessDetectorsAsync(settings, detectorsWithAppliedRestrictions, detectorRestrictions, cancellationToken);
4850
var scanResult = this.graphTranslationService.GenerateScanResultFromProcessingResult(processingResult, settings);
4951

5052
scanResult.DetectorsInScan = detectorsWithAppliedRestrictions.Select(ConvertToContract).ToList();

test/Microsoft.ComponentDetection.Orchestrator.Tests/Commands/ScanCommandTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public async Task ScanCommand_ExecutesScanAndWritesManifestAsync()
4444
var result = await this.command.ExecuteAsync(null, settings, CancellationToken.None);
4545

4646
this.fileWritingServiceMock.Verify(x => x.Init(settings.Output), Times.Once);
47-
this.scanExecutionServiceMock.Verify(x => x.ExecuteScanAsync(settings), Times.Once);
47+
this.scanExecutionServiceMock.Verify(x => x.ExecuteScanAsync(settings, CancellationToken.None), Times.Once);
4848
this.fileWritingServiceMock.Verify(x => x.ResolveFilePath(It.IsAny<string>()), Times.Once);
4949
this.fileWritingServiceMock.Verify(x => x.AppendToFile(It.IsAny<string>(), It.IsAny<ScanResult>()));
5050
result.Should().Be(0);
@@ -58,7 +58,7 @@ public async Task ScanCommand_ExecutesScanAndWritesUserManifestAsync()
5858
var result = await this.command.ExecuteAsync(null, settings, CancellationToken.None);
5959

6060
this.fileWritingServiceMock.Verify(x => x.Init(settings.Output), Times.Once);
61-
this.scanExecutionServiceMock.Verify(x => x.ExecuteScanAsync(settings), Times.Once);
61+
this.scanExecutionServiceMock.Verify(x => x.ExecuteScanAsync(settings, CancellationToken.None), Times.Once);
6262
this.fileWritingServiceMock.Verify(x => x.WriteFile(It.Is<FileInfo>(x => x == settings.ManifestFile), It.IsAny<ScanResult>()));
6363

6464
result.Should().Be(0);
@@ -75,7 +75,7 @@ public async Task ScanCommand_ExecutesScanAndPrintsManifestAsync()
7575
var result = await this.command.ExecuteAsync(null, settings, CancellationToken.None);
7676

7777
this.fileWritingServiceMock.Verify(x => x.Init(settings.Output), Times.Once);
78-
this.scanExecutionServiceMock.Verify(x => x.ExecuteScanAsync(settings), Times.Once);
78+
this.scanExecutionServiceMock.Verify(x => x.ExecuteScanAsync(settings, CancellationToken.None), Times.Once);
7979
this.fileWritingServiceMock.Verify(x => x.ResolveFilePath(It.IsAny<string>()), Times.Once);
8080
this.fileWritingServiceMock.Verify(x => x.AppendToFile(It.IsAny<string>(), It.IsAny<ScanResult>()));
8181

@@ -95,7 +95,7 @@ public async Task ExecuteScanCommandAsync_PrintsManifestAsync()
9595
var result = await this.command.ExecuteScanCommandAsync(settings);
9696

9797
this.fileWritingServiceMock.Verify(x => x.Init(settings.Output), Times.Once);
98-
this.scanExecutionServiceMock.Verify(x => x.ExecuteScanAsync(settings), Times.Once);
98+
this.scanExecutionServiceMock.Verify(x => x.ExecuteScanAsync(settings, CancellationToken.None), Times.Once);
9999
this.fileWritingServiceMock.Verify(x => x.ResolveFilePath(It.IsAny<string>()), Times.Once);
100100
this.fileWritingServiceMock.Verify(x => x.AppendToFile(It.IsAny<string>(), It.IsAny<ScanResult>()));
101101

@@ -111,7 +111,7 @@ public async Task ExecuteScanCommandAsync_WritesUserManifestAsync()
111111
var result = await this.command.ExecuteScanCommandAsync(settings);
112112

113113
this.fileWritingServiceMock.Verify(x => x.Init(settings.Output), Times.Once);
114-
this.scanExecutionServiceMock.Verify(x => x.ExecuteScanAsync(settings), Times.Once);
114+
this.scanExecutionServiceMock.Verify(x => x.ExecuteScanAsync(settings, CancellationToken.None), Times.Once);
115115
this.fileWritingServiceMock.Verify(x => x.WriteFile(It.Is<FileInfo>(x => x == settings.ManifestFile), It.IsAny<ScanResult>()));
116116
}
117117
}

test/Microsoft.ComponentDetection.Orchestrator.Tests/Services/BcdeScanExecutionServiceTests.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ namespace Microsoft.ComponentDetection.Orchestrator.Tests.Services;
44
using System.Collections.Generic;
55
using System.IO;
66
using System.Linq;
7+
using System.Threading;
78
using System.Threading.Tasks;
89
using AwesomeAssertions;
910
using Microsoft.ComponentDetection.Common.DependencyGraph;
@@ -765,10 +766,11 @@ private async Task<TestOutput> DetectComponentsHappyPathAsync(
765766
x.ProcessDetectorsAsync(
766767
settings,
767768
It.Is<IEnumerable<IComponentDetector>>(inputDetectors => restrictedDetectors.Intersect(inputDetectors).Count() == restrictedDetectors.Length),
768-
Match.Create<DetectorRestrictions>(restriction => true)))
769+
Match.Create<DetectorRestrictions>(restriction => true),
770+
CancellationToken.None))
769771
.ReturnsAsync(processingResult);
770772

771-
var result = await this.serviceUnderTest.ExecuteScanAsync(settings);
773+
var result = await this.serviceUnderTest.ExecuteScanAsync(settings, CancellationToken.None);
772774
result.ResultCode.Should().Be(ProcessingResultCode.Success);
773775
result.SourceDirectory.Should().NotBeNull();
774776
result.SourceDirectory.Should().Be(settings.SourceDirectory.ToString());
@@ -824,10 +826,11 @@ private async Task<TestOutput> DetectComponentsHappyPathAsync(
824826
x.ProcessDetectorsAsync(
825827
settings,
826828
It.Is<IEnumerable<IComponentDetector>>(inputDetectors => restrictedDetectors.Intersect(inputDetectors).Count() == restrictedDetectors.Length),
827-
Match.Create<DetectorRestrictions>(restriction => true)))
829+
Match.Create<DetectorRestrictions>(restriction => true),
830+
CancellationToken.None))
828831
.ReturnsAsync(processingResult);
829832

830-
var result = await this.serviceUnderTest.ExecuteScanAsync(settings);
833+
var result = await this.serviceUnderTest.ExecuteScanAsync(settings, CancellationToken.None);
831834
result.ResultCode.Should().Be(ProcessingResultCode.Success);
832835
result.SourceDirectory.Should().NotBeNull();
833836
result.SourceDirectory.Should().Be(settings.SourceDirectory.ToString());
@@ -880,10 +883,11 @@ private async Task<ScanResult> SetupRecorderBasedScanningAsync(
880883
x.ProcessDetectorsAsync(
881884
settings,
882885
It.Is<IEnumerable<IComponentDetector>>(inputDetectors => restrictedDetectors.Intersect(inputDetectors).Count() == restrictedDetectors.Length),
883-
Match.Create<DetectorRestrictions>(restriction => true)))
886+
Match.Create<DetectorRestrictions>(restriction => true),
887+
CancellationToken.None))
884888
.ReturnsAsync(processingResult);
885889

886-
var result = await this.serviceUnderTest.ExecuteScanAsync(settings);
890+
var result = await this.serviceUnderTest.ExecuteScanAsync(settings, CancellationToken.None);
887891
result.ResultCode.Should().Be(ProcessingResultCode.Success);
888892
result.SourceDirectory.Should().NotBeNull();
889893
result.SourceDirectory.Should().Be(settings.SourceDirectory.ToString());

0 commit comments

Comments
 (0)