Skip to content

Commit 3b1d59a

Browse files
authored
[IDP-1484] Add logging and retry for swagger OpenAPI spec generation (#55)
* [IDP-1484] Add logging and retry for swagger OpenAPI spec generation * remove comment * Remove other comments * Retry twice for a total of 3 executions * Adjusting retry * Address retry logic and use shorter cancellationtoken * break instead of using extra bool
1 parent d253628 commit 3b1d59a

File tree

2 files changed

+33
-14
lines changed

2 files changed

+33
-14
lines changed

src/Workleap.OpenApi.MSBuild/GenerateContractProcess.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using Microsoft.Build.Framework;
2+
13
namespace Workleap.OpenApi.MSBuild;
24

35
/// <summary>
@@ -70,5 +72,6 @@ private async Task InstallDependencies(
7072
}
7173

7274
await Task.WhenAll(installationTasks);
75+
this._loggerWrapper.LogMessage("Finished installing OpenAPI dependencies.", MessageImportance.High);
7376
}
7477
}

src/Workleap.OpenApi.MSBuild/SwaggerManager.cs

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ namespace Workleap.OpenApi.MSBuild;
66
internal 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

Comments
 (0)