Skip to content

Commit

Permalink
Improve retry handling based on feedback (#1447)
Browse files Browse the repository at this point in the history
  • Loading branch information
juliobbv authored Oct 29, 2021
1 parent 67ba8a7 commit b6dbf42
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/Runner.Common/JobServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ public sealed class JobServer : RunnerService, IJobServer
public async Task ConnectAsync(VssConnection jobConnection)
{
_connection = jobConnection;
int attemptCount = 5;
int totalAttempts = 5;
int attemptCount = totalAttempts;
var configurationStore = HostContext.GetService<IConfigurationStore>();
var runnerSettings = configurationStore.GetSettings();

Expand All @@ -56,18 +57,21 @@ public async Task ConnectAsync(VssConnection jobConnection)

if (runnerSettings.IsHostedServer)
{
await CheckNetworkEndpointsAsync();
await CheckNetworkEndpointsAsync(attemptCount);
}
}

await Task.Delay(100);
int attempt = totalAttempts - attemptCount;
TimeSpan backoff = BackoffTimerHelper.GetExponentialBackoff(attempt, TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(3.2), TimeSpan.FromMilliseconds(100));

await Task.Delay(backoff);
}

_taskClient = _connection.GetClient<TaskHttpClient>();
_hasConnection = true;
}

private async Task CheckNetworkEndpointsAsync()
private async Task CheckNetworkEndpointsAsync(int attemptsLeft)
{
try
{
Expand All @@ -79,8 +83,8 @@ private async Task CheckNetworkEndpointsAsync()

actionsClient.DefaultRequestHeaders.UserAgent.AddRange(HostContext.UserAgents);

// Call the _apis/health endpoint
var response = await actionsClient.GetAsync(new Uri(baseUri, "_apis/health"));
// Call the _apis/health endpoint, and include how many attempts are left as a URL query for easy tracking
var response = await actionsClient.GetAsync(new Uri(baseUri, $"_apis/health?_internalRunnerAttemptsLeft={attemptsLeft}"));
Trace.Info($"Actions health status code: {response.StatusCode}");
}
}
Expand All @@ -100,8 +104,8 @@ private async Task CheckNetworkEndpointsAsync()
{
gitHubClient.DefaultRequestHeaders.UserAgent.AddRange(HostContext.UserAgents);

// Call the api.github.com endpoint
var response = await gitHubClient.GetAsync("https://api.github.com");
// Call the api.github.com endpoint, and include how many attempts are left as a URL query for easy tracking
var response = await gitHubClient.GetAsync($"https://api.github.com?_internalRunnerAttemptsLeft={attemptsLeft}");
Trace.Info($"api.github.com status code: {response.StatusCode}");
}
}
Expand Down

0 comments on commit b6dbf42

Please sign in to comment.