Skip to content

Commit

Permalink
[ROAD-904] fix 🐛: run scan asynchronously (#150)
Browse files Browse the repository at this point in the history
  • Loading branch information
aldanchenko authored May 8, 2022
1 parent 464acca commit da116f6
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 44 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Snyk Changelog

## [1.1.14]

### Fixed
- Run a scan for OSS and for Snyk Code asynchronously.

## [1.1.13]

### Fixed
Expand Down
84 changes: 40 additions & 44 deletions Snyk.VisualStudio.Extension.Shared/Service/SnykTasksService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
}
Expand Down Expand Up @@ -446,7 +447,7 @@ private async Task RunOssScanAsync(FeaturesSettings featuresSettings)
}
}

private void ScanSnykCode(FeaturesSettings featuresSettings)
private async Task ScanSnykCodeAsync(FeaturesSettings featuresSettings)
{
try
{
Expand Down Expand Up @@ -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();
}
}

Expand Down

0 comments on commit da116f6

Please sign in to comment.