Skip to content

Commit

Permalink
Fix retries
Browse files Browse the repository at this point in the history
  • Loading branch information
sungam3r committed Apr 24, 2023
1 parent 8e0c59f commit 60e37ed
Showing 1 changed file with 24 additions and 10 deletions.
34 changes: 24 additions & 10 deletions src/GraphQL.SDLExporter/SDLWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,20 +188,25 @@ void PrintErrors(GraphQLResponse? response, string header)
const int MAX_RETRY = 10;
ColoredConsole.WriteInfo($"Starting to poll {serviceUrl} with max {MAX_RETRY} attempts.");

while (true)
GraphQLResponse? response = null;

while (response == null && retry <= MAX_RETRY)
{
cancellationToken.ThrowIfCancellationRequested();

ColoredConsole.WriteInfo($"Attempt #{retry} to {serviceUrl} begins");

try
{
return Options.IntrospectionQueryFile == null
response = Options.IntrospectionQueryFile == null
? ExecuteIntrospectionVariation(client, serviceUrl, Options.ConfigureIntrospectionQuery(IntrospectionQuery.ModernDraft), "modern/draft") ??
ExecuteIntrospectionVariation(client, serviceUrl, Options.ConfigureIntrospectionQuery(IntrospectionQuery.Modern), "modern") ??
ExecuteIntrospectionVariation(client, serviceUrl, Options.ConfigureIntrospectionQuery(IntrospectionQuery.ClassicDraft), "classic/draft") ??
ExecuteIntrospectionVariation(client, serviceUrl, Options.ConfigureIntrospectionQuery(IntrospectionQuery.Classic), "classic")
: ExecuteIntrospectionVariation(client, serviceUrl, Options.ConfigureIntrospectionQuery(File.ReadAllText(Options.IntrospectionQueryFile)), "custom:" + Options.IntrospectionQueryFile);

if (response != null)
return response;
}
catch (Exception e)
{
Expand All @@ -211,18 +216,27 @@ void PrintErrors(GraphQLResponse? response, string header)
return null;

ColoredConsole.WriteError($"Make sure that it is possible to start the process at {serviceUrl} and the required port is not used by another process. Perhaps the process has not yet started and cannot serve the request.");

if (retry == MAX_RETRY)
}
finally
{
if (response == null)
{
ColoredConsole.WriteError($"Failed to load data from {serviceUrl} for {MAX_RETRY} attempts");
return null; // that's enough
}
if (retry == MAX_RETRY)
{
ColoredConsole.WriteError($"Failed to load data from {serviceUrl} for {MAX_RETRY} attempts");
}
else
{
ColoredConsole.WriteInfo("Waiting 2 seconds and try again");
Thread.Sleep(2000);
}

++retry;
ColoredConsole.WriteInfo($"Waiting 2 seconds and try again ({retry} of {MAX_RETRY}).");
Thread.Sleep(2000);
++retry;
}
}
}

return response;
}

private string? ConvertIntrospectionResponseToSDL(GraphQLResponse response)
Expand Down

0 comments on commit 60e37ed

Please sign in to comment.