Skip to content

Commit da116f6

Browse files
authored
[ROAD-904] fix 🐛: run scan asynchronously (#150)
1 parent 464acca commit da116f6

2 files changed

Lines changed: 45 additions & 44 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Snyk Changelog
22

3+
## [1.1.14]
4+
5+
### Fixed
6+
- Run a scan for OSS and for Snyk Code asynchronously.
7+
38
## [1.1.13]
49

510
### Fixed

Snyk.VisualStudio.Extension.Shared/Service/SnykTasksService.cs

Lines changed: 40 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
using Microsoft.VisualStudio.Shell;
99
using Serilog;
1010
using Snyk.Code.Library.Domain.Analysis;
11-
using Snyk.Code.Library.Service;
1211
using Snyk.Common;
1312
using Snyk.VisualStudio.Extension.Shared.CLI;
1413
using Snyk.VisualStudio.Extension.Shared.CLI.Download;
@@ -213,10 +212,12 @@ public async Task ScanAsync()
213212

214213
this.serviceProvider.AnalyticsService.LogAnalysisIsTriggeredEvent(this.GetSelectedFeatures(selectedFeatures));
215214

216-
await this.ScanOssAsync(selectedFeatures);
215+
var ossScanTask = this.ScanOssAsync(selectedFeatures);
216+
var snykCodeScanTask = this.ScanSnykCodeAsync(selectedFeatures);
217217

218-
this.ScanSnykCode(selectedFeatures);
219-
} catch (Exception ex)
218+
await Task.WhenAll(ossScanTask, snykCodeScanTask);
219+
}
220+
catch (Exception ex)
220221
{
221222
Logger.Error(ex, "Error on scan");
222223
}
@@ -446,7 +447,7 @@ private async Task RunOssScanAsync(FeaturesSettings featuresSettings)
446447
}
447448
}
448449

449-
private void ScanSnykCode(FeaturesSettings featuresSettings)
450+
private async Task ScanSnykCodeAsync(FeaturesSettings featuresSettings)
450451
{
451452
try
452453
{
@@ -479,57 +480,52 @@ private void ScanSnykCode(FeaturesSettings featuresSettings)
479480

480481
Logger.Information("Start scan task");
481482

482-
_ = Task.Run(async () =>
483-
{
484-
try
485-
{
486-
try
487-
{
488-
this.isSnykCodeScanning = true;
489-
490-
this.FireSnykCodeScanningStartedEvent();
483+
await Task.Run(() => this.RunSnykCodeScanAsync(progressWorker.TokenSource.Token));
484+
}
485+
catch (Exception ex)
486+
{
487+
Logger.Error(ex, "Error on SnykCode scan");
488+
}
489+
}
491490

492-
var fileProvider = this.serviceProvider.SolutionService.FileProvider;
491+
private async Task RunSnykCodeScanAsync(CancellationToken cancellationToken)
492+
{
493+
try
494+
{
495+
this.isSnykCodeScanning = true;
493496

494-
var analysisResult = await this.serviceProvider.SnykCodeService.ScanAsync(fileProvider, progressWorker.TokenSource.Token);
497+
this.FireSnykCodeScanningStartedEvent();
495498

496-
this.FireScanningUpdateEvent(analysisResult);
499+
var fileProvider = this.serviceProvider.SolutionService.FileProvider;
497500

498-
this.FireSnykCodeScanningFinishedEvent();
499-
}
500-
catch (Exception e)
501-
{
502-
if (this.IsTaskCancelled(e))
503-
{
504-
this.FireScanningCancelledEvent();
501+
var analysisResult = await this.serviceProvider.SnykCodeService.ScanAsync(fileProvider, cancellationToken);
505502

506-
return;
507-
}
503+
this.FireScanningUpdateEvent(analysisResult);
508504

509-
string errorMessage = this.serviceProvider.SnykCodeService.GetSnykCodeErrorMessage(e);
505+
this.FireSnykCodeScanningFinishedEvent();
506+
}
507+
catch (Exception e)
508+
{
509+
if (this.IsTaskCancelled(e))
510+
{
511+
this.FireScanningCancelledEvent();
510512

511-
this.OnSnykCodeError(errorMessage);
512-
}
513-
}
514-
catch (Exception exception)
515-
{
516-
Logger.Error(exception, string.Empty);
513+
return;
514+
}
517515

518-
this.FireScanningCancelledEvent();
519-
}
520-
finally
521-
{
522-
this.DisposeCancellationTokenSource(this.snykCodeScanTokenSource);
516+
Logger.Error(e, "Error on Run Snyk Code scan");
523517

524-
this.isSnykCodeScanning = false;
518+
string errorMessage = this.serviceProvider.SnykCodeService.GetSnykCodeErrorMessage(e);
525519

526-
this.FireTaskFinished();
527-
}
528-
});
520+
this.OnSnykCodeError(errorMessage);
529521
}
530-
catch (Exception ex)
522+
finally
531523
{
532-
Logger.Error(ex, "Error on SnykCode scan");
524+
this.DisposeCancellationTokenSource(this.snykCodeScanTokenSource);
525+
526+
this.isSnykCodeScanning = false;
527+
528+
this.FireTaskFinished();
533529
}
534530
}
535531

0 commit comments

Comments
 (0)