Skip to content

Allow runner to check service connection in background.#3542

Merged
TingluoHuang merged 3 commits intomainfrom
users/tihuang/connectioncheck
Nov 12, 2024
Merged

Allow runner to check service connection in background.#3542
TingluoHuang merged 3 commits intomainfrom
users/tihuang/connectioncheck

Conversation

@TingluoHuang
Copy link
Copy Markdown
Member

The server can request runner to do connectivity check as need to debug network issue between the runner and the service.

@TingluoHuang TingluoHuang requested a review from a team as a code owner November 7, 2024 04:12
namespace GitHub.DistributedTask.WebApi
{
[DataContract]
public class ServiceConnectivityCheckInput
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i will copy the contract to the service side.

Comment on lines +833 to +838
var foo = new ServiceConnectivityCheckInput() { IntervalInSecond = 30, RequestTimeoutInSecond = 30 };
foo.Endpoints.Add("pipelines", "https://pipelinesghubeus20.actions.githubusercontent.com/_apis/connectiondata");
foo.Endpoints.Add("broker", "https://broker.actions.githubusercontent.com/health");
foo.Endpoints.Add("results", "https://results-receiver.actions.githubusercontent.com/health");
var fooJson = StringUtil.ConvertToJson(foo);
context.Global.Variables.Set(WellKnownDistributedTaskVariables.RunnerServiceConnectivityTest, fooJson);
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is an example input, i will remove this before merge the PR.

}

private async Task<string> CheckConnectivity(string endpointUrl)
private async Task<CheckResult> CheckConnectivity(string endpointUrl, string accessToken, int timeoutInSeconds, CancellationToken token)
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I make this more generic so i can reuse between the current 1 time connection check and the backend long-running connection check.

using (var timeoutTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(5)))
CheckResult result = new CheckResult() { EndpointUrl = endpointUrl };
var stopwatch = Stopwatch.StartNew();
using (var timeoutTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(timeoutInSeconds)))
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the existing use case will pass 5 as method input.

CheckResult result = new CheckResult() { EndpointUrl = endpointUrl };
var stopwatch = Stopwatch.StartNew();
using (var timeoutTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(timeoutInSeconds)))
using (var linkedTokenSource = CancellationTokenSource.CreateLinkedTokenSource(token, timeoutTokenSource.Token))
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the existing use code will pass CancellationToken.None

httpClient.DefaultRequestHeaders.UserAgent.AddRange(HostContext.UserAgents);
var response = await httpClient.GetAsync(endpointUrl, timeoutTokenSource.Token);
result = $"{endpointUrl}: {response.StatusCode}";
if (!string.IsNullOrEmpty(accessToken))
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

existing code will pass empty string.

catch (Exception ex) when (ex is OperationCanceledException && timeoutTokenSource.IsCancellationRequested)
{
Trace.Error($"Request timeout during connectivity check: {ex}");
result = $"{endpointUrl}: timeout";
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we will reconstruct the same result from the caller.

context.Global.Variables.Set(WellKnownDistributedTaskVariables.RunnerServiceConnectivityTest, fooJson);

var connectionTest = context.Global.Variables.Get(WellKnownDistributedTaskVariables.RunnerServiceConnectivityTest);
if (string.IsNullOrEmpty(connectionTest))
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the featureflag to control the test.

}
}

var telemetryData = StringUtil.ConvertToJson(testResult, Formatting.None);
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't add indent to save space.

@TingluoHuang TingluoHuang force-pushed the users/tihuang/connectioncheck branch from 53a41fb to 896f513 Compare November 7, 2024 04:20
rentziass
rentziass previously approved these changes Nov 7, 2024
@TingluoHuang TingluoHuang force-pushed the users/tihuang/connectioncheck branch from 0a55fc0 to 56372d2 Compare November 12, 2024 15:35
@TingluoHuang TingluoHuang merged commit d16fb2c into main Nov 12, 2024
@TingluoHuang TingluoHuang deleted the users/tihuang/connectioncheck branch November 12, 2024 18:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants