Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "purview-build",
"version": "0.2.2",
"version": "0.2.3",
"private": true
}
65 changes: 50 additions & 15 deletions src/Purview.Build/Modules/LintModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,7 @@ Task<CommandResult> Restore() =>
cancellationToken
);

var restoreResult = await Restore();
for (var attempt = 1; restoreResult.ExitCode != 0 && attempt < maxAttempts; attempt++)
{
context.Logger.LogWarning(
"dotnet tool restore failed (attempt {Attempt} of {MaxAttempts}). Retrying...",
attempt,
maxAttempts
);

await Task.Delay(TimeSpan.FromSeconds(2), cancellationToken);

restoreResult = await Restore();
}
if (restoreResult.ExitCode != 0)
return restoreResult;
var restoreResult = await RestoreWithRetryAsync(Restore, maxAttempts, context, cancellationToken);

// Restore worked, now run the linter
return await context.Shell.Command.ExecuteCommandLineTool(
Expand All @@ -59,4 +45,53 @@ Task<CommandResult> Restore() =>
cancellationToken: cancellationToken
);
}

static async Task<CommandResult> RestoreWithRetryAsync(
Func<Task<CommandResult>> restore,
int maxAttempts,
IModuleContext context,
CancellationToken cancellationToken
)
{
Exception? lastException = null;
CommandResult? lastResult = null;

for (var attempt = 1; attempt <= maxAttempts; attempt++)
{
try
{
var result = await restore();
if (result.ExitCode == 0)
return result;

lastException = null;
lastResult = result;
}
catch (Exception ex) when (ex is not OperationCanceledException)
{
lastException = ex;
lastResult = null;
}

if (attempt < maxAttempts)
{
context.Logger.LogWarning(
lastException,
"dotnet tool restore failed (attempt {Attempt} of {MaxAttempts}). Retrying...",
attempt,
maxAttempts
);

await Task.Delay(TimeSpan.FromSeconds(2), cancellationToken);
}
}

if (lastException is not null)
throw lastException;

if (lastResult is not null)
throw new InvalidOperationException($"dotnet tool restore failed with exit code {lastResult.ExitCode}.");

throw new InvalidOperationException("dotnet tool restore failed.");
}
}