Skip to content

Commit

Permalink
Merge pull request #443 from seilorjunior/patch-4
Browse files Browse the repository at this point in the history
The correction for the bug regarding to open products in the same API
  • Loading branch information
waelkdouh authored Jan 26, 2024
2 parents 1fe5e45 + ad54e3e commit 2848565
Showing 1 changed file with 38 additions and 6 deletions.
44 changes: 38 additions & 6 deletions tools/code/publisher/ProductApi.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
using common;
using common;
using Microsoft.Extensions.Logging;
using MoreLinq;
using MoreLinq.Extensions;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
using System.Linq;
using System.Text.Json.Nodes;
using System.Threading;
using System.Threading.Tasks;
using System;
using System.Net.Http;

namespace publisher;

Expand Down Expand Up @@ -109,7 +112,7 @@ private static async ValueTask Put(ProductName productName, IReadOnlyCollection<
{
var productUri = GetProductUri(productName, serviceUri);
var productApisUri = new ProductApisUri(productUri);

var existingApiNames = await listRestResources(productApisUri.Uri, cancellationToken)
.Select(apiJsonObject => apiJsonObject.GetStringProperty("name"))
.Select(name => new ApiName(name))
Expand All @@ -118,6 +121,7 @@ private static async ValueTask Put(ProductName productName, IReadOnlyCollection<
var apiNamesToPut = apiNames.Except(existingApiNames);
var apiNamesToRemove = existingApiNames.Except(apiNames);


await apiNamesToRemove.ForEachParallel(async apiName =>
{
logger.LogInformation("Removing API {apiName} in product {productName}...", apiName, productName);
Expand All @@ -127,7 +131,7 @@ await apiNamesToRemove.ForEachParallel(async apiName =>
await apiNamesToPut.ForEachParallel(async apiName =>
{
logger.LogInformation("Putting API {apiName} in product {productName}...", apiName, productName);
await Put(apiName, productUri, putRestResource, cancellationToken);
await Put(apiName, productUri, putRestResource,logger, cancellationToken);
}, cancellationToken);
}

Expand All @@ -145,16 +149,44 @@ private static async ValueTask Delete(ApiName apiName, ProductUri productUri, De
await deleteRestResource(productApiUri.Uri, cancellationToken);
}

private static async ValueTask Put(ApiName apiName, ProductUri productUri, PutRestResource putRestResource, CancellationToken cancellationToken)
private static async ValueTask Put(ApiName apiName, ProductUri productUri, PutRestResource putRestResource, ILogger logger, CancellationToken cancellationToken)
{
var productApisUri = new ProductApisUri(productUri);
var productApiUri = new ProductApiUri(apiName, productApisUri);

await putRestResource(productApiUri.Uri, new JsonObject(), cancellationToken);
int retryCount = 0;
bool retry = true;
while (retry)
{
try
{
await putRestResource(productApiUri.Uri, new JsonObject(), cancellationToken);
retry = false; // No exception occurred, so no need to retry
}
catch (HttpRequestException httpRequestException)
{
if (httpRequestException.Message.Contains("API cannot be added to more than one open products"))
{
retryCount++;
if (retryCount <= 3)
{
// Log the retry attempt
logger.LogWarning("Retrying API put operation for {apiName}. Retry attempt: {retryCount}", apiName, retryCount);
// Wait for a certain duration before retrying
await Task.Delay(TimeSpan.FromSeconds(Math.Pow(2, retryCount)), cancellationToken);
}
else
{
// Retry limit reached, throw the exception
throw;
}
}
}
}
}

public static async ValueTask ProcessArtifactsToPut(IReadOnlyCollection<FileInfo> files, JsonObject configurationJson, ServiceDirectory serviceDirectory, ServiceUri serviceUri, ListRestResources listRestResources, PutRestResource putRestResource, DeleteRestResource deleteRestResource, ILogger logger, CancellationToken cancellationToken)
{
await Put(files, configurationJson, serviceDirectory, serviceUri, listRestResources, putRestResource, deleteRestResource, logger, cancellationToken);
}
}
}

0 comments on commit 2848565

Please sign in to comment.