Allow runner to check service connection in background.#3542
Allow runner to check service connection in background.#3542TingluoHuang merged 3 commits intomainfrom
Conversation
| namespace GitHub.DistributedTask.WebApi | ||
| { | ||
| [DataContract] | ||
| public class ServiceConnectivityCheckInput |
There was a problem hiding this comment.
i will copy the contract to the service side.
src/Runner.Worker/JobExtension.cs
Outdated
| 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); |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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))) |
There was a problem hiding this comment.
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)) |
There was a problem hiding this comment.
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)) |
There was a problem hiding this comment.
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"; |
There was a problem hiding this comment.
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)) |
There was a problem hiding this comment.
this is the featureflag to control the test.
| } | ||
| } | ||
|
|
||
| var telemetryData = StringUtil.ConvertToJson(testResult, Formatting.None); |
There was a problem hiding this comment.
don't add indent to save space.
53a41fb to
896f513
Compare
f4385f5 to
f931e1a
Compare
0a55fc0 to
56372d2
Compare
The server can request runner to do connectivity check as need to debug network issue between the runner and the service.