@@ -6,6 +6,7 @@ namespace Workleap.OpenApi.MSBuild;
66internal sealed class SwaggerManager : ISwaggerManager
77{
88 private const string SwaggerVersion = "6.5.0" ;
9+ private const int MaxRetryCount = 3 ;
910 private readonly IProcessWrapper _processWrapper ;
1011 private readonly ILoggerWrapper _loggerWrapper ;
1112 private readonly string _openApiWebApiAssemblyPath ;
@@ -46,43 +47,58 @@ public async Task InstallSwaggerCliAsync(CancellationToken cancellationToken)
4647 return ;
4748 }
4849
49- var retryCount = 0 ;
50- while ( retryCount < 2 )
50+ for ( var retryCount = 0 ; retryCount < MaxRetryCount ; retryCount ++ )
5151 {
5252 var result = await this . _processWrapper . RunProcessAsync (
5353 "dotnet" ,
5454 [ "tool" , "update" , "Swashbuckle.AspNetCore.Cli" , "--ignore-failed-sources" , "--tool-path" , this . _swaggerDirectory , "--configfile" , Path . Combine ( this . _openApiToolsDirectoryPath , "nuget.config" ) , "--version" , SwaggerVersion ] ,
5555 cancellationToken ) ;
5656
57- if ( result . ExitCode != 0 && retryCount != 1 )
57+ var isLastRetry = retryCount == MaxRetryCount - 1 ;
58+ if ( result . ExitCode != 0 )
5859 {
60+ if ( isLastRetry )
61+ {
62+ throw new OpenApiTaskFailedException ( "Swashbuckle CLI could not be installed." ) ;
63+ }
64+
5965 this . _loggerWrapper . LogMessage ( result . StandardOutput , MessageImportance . High ) ;
6066 this . _loggerWrapper . LogWarning ( result . StandardError ) ;
6167 this . _loggerWrapper . LogWarning ( "Swashbuckle download failed. Retrying once more..." ) ;
6268
63- retryCount ++ ;
6469 continue ;
6570 }
6671
67- if ( retryCount == 1 && result . ExitCode != 0 )
68- {
69- throw new OpenApiTaskFailedException ( "Swashbuckle CLI could not be installed." ) ;
70- }
71-
7272 break ;
7373 }
7474 }
7575
7676 public async Task < string > GenerateOpenApiSpecAsync ( string swaggerExePath , string outputOpenApiSpecPath , string documentName , CancellationToken cancellationToken )
7777 {
7878 var envVars = new Dictionary < string , string ? > ( ) { { "DOTNET_ROLL_FORWARD" , "LatestMajor" } } ;
79- var result = await this . _processWrapper . RunProcessAsync ( swaggerExePath , [ "tofile" , "--output" , outputOpenApiSpecPath , "--yaml" , this . _openApiWebApiAssemblyPath , documentName ] , cancellationToken : cancellationToken , envVars : envVars ) ;
80- this . _loggerWrapper . LogMessage ( result . StandardOutput , MessageImportance . High ) ;
79+ var swaggerCancellationToken = CancellationTokenSource . CreateLinkedTokenSource ( cancellationToken ) ;
80+ swaggerCancellationToken . CancelAfter ( TimeSpan . FromMinutes ( 1 ) ) ;
8181
82- if ( result . ExitCode != 0 )
82+ var willRetry = true ;
83+ for ( var retryCount = 0 ; retryCount < MaxRetryCount ; retryCount ++ )
8384 {
84- this . _loggerWrapper . LogWarning ( result . StandardError ) ;
85- throw new OpenApiTaskFailedException ( $ "OpenApi file { outputOpenApiSpecPath } could not be created.") ;
85+ var result = await this . _processWrapper . RunProcessAsync ( swaggerExePath , [ "tofile" , "--output" , outputOpenApiSpecPath , "--yaml" , this . _openApiWebApiAssemblyPath , documentName ] , cancellationToken : swaggerCancellationToken . Token , envVars : envVars ) ;
86+
87+ var isLastRetry = retryCount == MaxRetryCount - 1 ;
88+ if ( result . ExitCode != 0 )
89+ {
90+ if ( isLastRetry )
91+ {
92+ throw new OpenApiTaskFailedException ( $ "OpenApi file for { outputOpenApiSpecPath } could not be generated.") ;
93+ }
94+
95+ this . _loggerWrapper . LogMessage ( result . StandardOutput , MessageImportance . High ) ;
96+ this . _loggerWrapper . LogWarning ( result . StandardError ) ;
97+ this . _loggerWrapper . LogWarning ( $ "OpenAPI spec generation failed for { outputOpenApiSpecPath } . Retrying again...") ;
98+ continue ;
99+ }
100+
101+ break ;
86102 }
87103
88104 return outputOpenApiSpecPath ;
0 commit comments