diff --git a/src/Runner.Common/ActionsRunServer.cs b/src/Runner.Common/ActionsRunServer.cs index 704a690e3ca..3ded580071d 100644 --- a/src/Runner.Common/ActionsRunServer.cs +++ b/src/Runner.Common/ActionsRunServer.cs @@ -20,12 +20,12 @@ public sealed class ActionsRunServer : RunnerService, IActionsRunServer { private bool _hasConnection; private VssConnection _connection; - private TaskAgentHttpClient _taskAgentClient; + private ActionsRunServerHttpClient _actionsRunServerClient; public async Task ConnectAsync(Uri serverUrl, VssCredentials credentials) { _connection = await EstablishVssConnection(serverUrl, credentials, TimeSpan.FromSeconds(100)); - _taskAgentClient = _connection.GetClient(); + _actionsRunServerClient = _connection.GetClient(); _hasConnection = true; } @@ -42,7 +42,7 @@ public Task GetJobMessageAsync(string id, CancellationTo CheckConnection(); var jobMessage = RetryRequest(async () => { - return await _taskAgentClient.GetJobMessageAsync(id, cancellationToken); + return await _actionsRunServerClient.GetJobMessageAsync(id, cancellationToken); }, cancellationToken); return jobMessage; diff --git a/src/Sdk/DTWebApi/WebApi/ActionsRunServerHttpClient.cs b/src/Sdk/DTWebApi/WebApi/ActionsRunServerHttpClient.cs new file mode 100644 index 00000000000..a72e8a28bbf --- /dev/null +++ b/src/Sdk/DTWebApi/WebApi/ActionsRunServerHttpClient.cs @@ -0,0 +1,95 @@ +using System; +using System.Collections.Generic; +using System.Globalization; +using System.IO; +using System.IO.Compression; +using System.Linq; +using System.Net; +using System.Net.Http; +using System.Net.Http.Headers; +using System.Threading; +using System.Threading.Tasks; +using GitHub.Services.Common; +using GitHub.Services.Common.Diagnostics; +using GitHub.Services.WebApi; +using Newtonsoft.Json; + +namespace GitHub.DistributedTask.WebApi +{ + [ResourceArea(TaskResourceIds.AreaId)] + public class ActionsRunServerHttpClient : TaskAgentHttpClient + { + private static readonly JsonSerializerSettings s_serializerSettings; + + static ActionsRunServerHttpClient() + { + s_serializerSettings = new VssJsonMediaTypeFormatter().SerializerSettings; + s_serializerSettings.DateParseHandling = DateParseHandling.None; + s_serializerSettings.FloatParseHandling = FloatParseHandling.Double; + } + + public ActionsRunServerHttpClient( + Uri baseUrl, + VssCredentials credentials) + : base(baseUrl, credentials) + { + } + + public ActionsRunServerHttpClient( + Uri baseUrl, + VssCredentials credentials, + VssHttpRequestSettings settings) + : base(baseUrl, credentials, settings) + { + } + + public ActionsRunServerHttpClient( + Uri baseUrl, + VssCredentials credentials, + params DelegatingHandler[] handlers) + : base(baseUrl, credentials, handlers) + { + } + + public ActionsRunServerHttpClient( + Uri baseUrl, + VssCredentials credentials, + VssHttpRequestSettings settings, + params DelegatingHandler[] handlers) + : base(baseUrl, credentials, settings, handlers) + { + } + + public ActionsRunServerHttpClient( + Uri baseUrl, + HttpMessageHandler pipeline, + Boolean disposeHandler) + : base(baseUrl, pipeline, disposeHandler) + { + } + + public Task GetJobMessageAsync( + string messageId, + object userState = null, + CancellationToken cancellationToken = default) + { + HttpMethod httpMethod = new HttpMethod("GET"); + Guid locationId = new Guid("25adab70-1379-4186-be8e-b643061ebe3a"); + object routeValues = new { messageId = messageId }; + + return SendAsync( + httpMethod, + locationId, + routeValues: routeValues, + version: new ApiResourceVersion(6.0, 1), + userState: userState, + cancellationToken: cancellationToken); + } + + protected override async Task ReadJsonContentAsync(HttpResponseMessage response, CancellationToken cancellationToken = default(CancellationToken)) + { + var json = await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false); + return JsonConvert.DeserializeObject(json, s_serializerSettings); + } + } +} diff --git a/src/Sdk/DTWebApi/WebApi/TaskAgentHttpClient.cs b/src/Sdk/DTWebApi/WebApi/TaskAgentHttpClient.cs index 4b08ebaa83a..c97fea0a4d1 100644 --- a/src/Sdk/DTWebApi/WebApi/TaskAgentHttpClient.cs +++ b/src/Sdk/DTWebApi/WebApi/TaskAgentHttpClient.cs @@ -141,24 +141,6 @@ public Task ReplaceAgentAsync( return ReplaceAgentAsync(poolId, agent.Id, agent, userState, cancellationToken); } - public Task GetJobMessageAsync( - string messageId, - object userState = null, - CancellationToken cancellationToken = default) - { - HttpMethod httpMethod = new HttpMethod("GET"); - Guid locationId = new Guid("25adab70-1379-4186-be8e-b643061ebe3a"); - object routeValues = new { messageId = messageId }; - - return SendAsync( - httpMethod, - locationId, - routeValues: routeValues, - version: new ApiResourceVersion(6.0, 1), - userState: userState, - cancellationToken: cancellationToken); - } - protected Task SendAsync( HttpMethod method, Guid locationId, diff --git a/src/Sdk/RSWebApi/RunServiceHttpClient.cs b/src/Sdk/RSWebApi/RunServiceHttpClient.cs index 4d2b74f8c4c..ba176ccf6b6 100644 --- a/src/Sdk/RSWebApi/RunServiceHttpClient.cs +++ b/src/Sdk/RSWebApi/RunServiceHttpClient.cs @@ -9,6 +9,7 @@ using GitHub.Services.Common; using GitHub.Services.OAuth; using GitHub.Services.WebApi; +using Newtonsoft.Json; using Sdk.RSWebApi.Contracts; using Sdk.WebApi.WebApi; @@ -16,6 +17,15 @@ namespace GitHub.Actions.RunService.WebApi { public class RunServiceHttpClient : RawHttpClientBase { + private static readonly JsonSerializerSettings s_serializerSettings; + + static RunServiceHttpClient() + { + s_serializerSettings = new VssJsonMediaTypeFormatter().SerializerSettings; + s_serializerSettings.DateParseHandling = DateParseHandling.None; + s_serializerSettings.FloatParseHandling = FloatParseHandling.Double; + } + public RunServiceHttpClient( Uri baseUrl, VssOAuthCredential credentials) @@ -174,5 +184,11 @@ public async Task RenewJobAsync( throw new Exception($"Failed to renew job: {result.Error}"); } } + + protected override async Task ReadJsonContentAsync(HttpResponseMessage response, CancellationToken cancellationToken = default(CancellationToken)) + { + var json = await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false); + return JsonConvert.DeserializeObject(json, s_serializerSettings); + } } }