-
Notifications
You must be signed in to change notification settings - Fork 959
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow runner to check service connection in background. #3542
base: main
Are you sure you want to change the base?
Conversation
namespace GitHub.DistributedTask.WebApi | ||
{ | ||
[DataContract] | ||
public class ServiceConnectivityCheckInput |
There was a problem hiding this comment.
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.
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.
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.
@@ -717,33 +739,58 @@ public async Task FinalizeJob(IExecutionContext jobContext, Pipelines.AgentJobRe | |||
} | |||
} | |||
|
|||
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.
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))) |
There was a problem hiding this comment.
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)) |
There was a problem hiding this comment.
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
{ | ||
try | ||
{ | ||
using (var httpClientHandler = HostContext.CreateHttpClientHandler()) | ||
using (var httpClient = new HttpClient(httpClientHandler)) | ||
{ | ||
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.
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"; |
There was a problem hiding this comment.
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)) |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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.
53a41fb
to
896f513
Compare
f4385f5
to
f931e1a
Compare
The server can request runner to do connectivity check as need to debug network issue between the runner and the service.