From da116f641b872ecb693052c7bd155962b4635c27 Mon Sep 17 00:00:00 2001 From: Aleksandr Danchenko Date: Sun, 8 May 2022 13:46:26 +0300 Subject: [PATCH] =?UTF-8?q?[ROAD-904]=20fix=20=F0=9F=90=9B:=20run=20scan?= =?UTF-8?q?=20asynchronously=20(#150)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 5 ++ .../Service/SnykTasksService.cs | 84 +++++++++---------- 2 files changed, 45 insertions(+), 44 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ded5ce2b3..43a6bb8f7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Snyk Changelog +## [1.1.14] + +### Fixed +- Run a scan for OSS and for Snyk Code asynchronously. + ## [1.1.13] ### Fixed diff --git a/Snyk.VisualStudio.Extension.Shared/Service/SnykTasksService.cs b/Snyk.VisualStudio.Extension.Shared/Service/SnykTasksService.cs index cc2b50670..8d8efcbaf 100644 --- a/Snyk.VisualStudio.Extension.Shared/Service/SnykTasksService.cs +++ b/Snyk.VisualStudio.Extension.Shared/Service/SnykTasksService.cs @@ -8,7 +8,6 @@ using Microsoft.VisualStudio.Shell; using Serilog; using Snyk.Code.Library.Domain.Analysis; - using Snyk.Code.Library.Service; using Snyk.Common; using Snyk.VisualStudio.Extension.Shared.CLI; using Snyk.VisualStudio.Extension.Shared.CLI.Download; @@ -213,10 +212,12 @@ public async Task ScanAsync() this.serviceProvider.AnalyticsService.LogAnalysisIsTriggeredEvent(this.GetSelectedFeatures(selectedFeatures)); - await this.ScanOssAsync(selectedFeatures); + var ossScanTask = this.ScanOssAsync(selectedFeatures); + var snykCodeScanTask = this.ScanSnykCodeAsync(selectedFeatures); - this.ScanSnykCode(selectedFeatures); - } catch (Exception ex) + await Task.WhenAll(ossScanTask, snykCodeScanTask); + } + catch (Exception ex) { Logger.Error(ex, "Error on scan"); } @@ -446,7 +447,7 @@ private async Task RunOssScanAsync(FeaturesSettings featuresSettings) } } - private void ScanSnykCode(FeaturesSettings featuresSettings) + private async Task ScanSnykCodeAsync(FeaturesSettings featuresSettings) { try { @@ -479,57 +480,52 @@ private void ScanSnykCode(FeaturesSettings featuresSettings) Logger.Information("Start scan task"); - _ = Task.Run(async () => - { - try - { - try - { - this.isSnykCodeScanning = true; - - this.FireSnykCodeScanningStartedEvent(); + await Task.Run(() => this.RunSnykCodeScanAsync(progressWorker.TokenSource.Token)); + } + catch (Exception ex) + { + Logger.Error(ex, "Error on SnykCode scan"); + } + } - var fileProvider = this.serviceProvider.SolutionService.FileProvider; + private async Task RunSnykCodeScanAsync(CancellationToken cancellationToken) + { + try + { + this.isSnykCodeScanning = true; - var analysisResult = await this.serviceProvider.SnykCodeService.ScanAsync(fileProvider, progressWorker.TokenSource.Token); + this.FireSnykCodeScanningStartedEvent(); - this.FireScanningUpdateEvent(analysisResult); + var fileProvider = this.serviceProvider.SolutionService.FileProvider; - this.FireSnykCodeScanningFinishedEvent(); - } - catch (Exception e) - { - if (this.IsTaskCancelled(e)) - { - this.FireScanningCancelledEvent(); + var analysisResult = await this.serviceProvider.SnykCodeService.ScanAsync(fileProvider, cancellationToken); - return; - } + this.FireScanningUpdateEvent(analysisResult); - string errorMessage = this.serviceProvider.SnykCodeService.GetSnykCodeErrorMessage(e); + this.FireSnykCodeScanningFinishedEvent(); + } + catch (Exception e) + { + if (this.IsTaskCancelled(e)) + { + this.FireScanningCancelledEvent(); - this.OnSnykCodeError(errorMessage); - } - } - catch (Exception exception) - { - Logger.Error(exception, string.Empty); + return; + } - this.FireScanningCancelledEvent(); - } - finally - { - this.DisposeCancellationTokenSource(this.snykCodeScanTokenSource); + Logger.Error(e, "Error on Run Snyk Code scan"); - this.isSnykCodeScanning = false; + string errorMessage = this.serviceProvider.SnykCodeService.GetSnykCodeErrorMessage(e); - this.FireTaskFinished(); - } - }); + this.OnSnykCodeError(errorMessage); } - catch (Exception ex) + finally { - Logger.Error(ex, "Error on SnykCode scan"); + this.DisposeCancellationTokenSource(this.snykCodeScanTokenSource); + + this.isSnykCodeScanning = false; + + this.FireTaskFinished(); } }