diff --git a/src/Runner.Common/RunServer.cs b/src/Runner.Common/RunServer.cs index 32c9f063fc0..4f344c0b070 100644 --- a/src/Runner.Common/RunServer.cs +++ b/src/Runner.Common/RunServer.cs @@ -51,14 +51,8 @@ private void CheckConnection() public Task GetJobMessageAsync(string id, CancellationToken cancellationToken) { CheckConnection(); - var jobMessage = RetryRequest( + return RetryRequest( async () => await _runServiceHttpClient.GetJobMessageAsync(requestUri, id, cancellationToken), cancellationToken); - if (jobMessage == null) - { - throw new TaskOrchestrationJobNotFoundException(id); - } - - return jobMessage; } public Task CompleteJobAsync(Guid planId, Guid jobId, TaskResult result, Dictionary outputs, IList stepResults, CancellationToken cancellationToken) @@ -71,14 +65,8 @@ public Task CompleteJobAsync(Guid planId, Guid jobId, TaskResult result, Diction public Task RenewJobAsync(Guid planId, Guid jobId, CancellationToken cancellationToken) { CheckConnection(); - var renewJobResponse = RetryRequest( + return RetryRequest( async () => await _runServiceHttpClient.RenewJobAsync(requestUri, planId, jobId, cancellationToken), cancellationToken); - if (renewJobResponse == null) - { - throw new TaskOrchestrationJobNotFoundException(jobId.ToString()); - } - - return renewJobResponse; } } } diff --git a/src/Sdk/RSWebApi/RunServiceHttpClient.cs b/src/Sdk/RSWebApi/RunServiceHttpClient.cs index 2730e66657a..e1e416d274a 100644 --- a/src/Sdk/RSWebApi/RunServiceHttpClient.cs +++ b/src/Sdk/RSWebApi/RunServiceHttpClient.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Net; using System.Net.Http; using System.Threading; using System.Threading.Tasks; @@ -55,7 +56,7 @@ public RunServiceHttpClient( { } - public Task GetJobMessageAsync( + public async Task GetJobMessageAsync( Uri requestUri, string messageId, CancellationToken cancellationToken = default) @@ -69,14 +70,27 @@ public Task GetJobMessageAsync( requestUri = new Uri(requestUri, "acquirejob"); var requestContent = new ObjectContent(payload, new VssJsonMediaTypeFormatter(true)); - return SendAsync( + var result = await SendAsync( httpMethod, requestUri: requestUri, content: requestContent, cancellationToken: cancellationToken); + + if (result.IsSuccess) + { + return result.Value; + } + + switch (result.StatusCode) + { + case HttpStatusCode.NotFound: + throw new TaskOrchestrationJobNotFoundException($"Job message not found: {messageId}"); + default: + throw new Exception($"Failed to get job message: {result.Error}"); + } } - public Task CompleteJobAsync( + public async Task CompleteJobAsync( Uri requestUri, Guid planId, Guid jobId, @@ -98,14 +112,26 @@ public Task CompleteJobAsync( requestUri = new Uri(requestUri, "completejob"); var requestContent = new ObjectContent(payload, new VssJsonMediaTypeFormatter(true)); - return SendAsync( + var response = await SendAsync( httpMethod, requestUri, content: requestContent, cancellationToken: cancellationToken); + if (response.IsSuccessStatusCode) + { + return; + } + + switch (response.StatusCode) + { + case HttpStatusCode.NotFound: + throw new TaskOrchestrationJobNotFoundException($"Job not found: {jobId}"); + default: + throw new Exception($"Failed to complete job: {response.ReasonPhrase}"); + } } - public Task RenewJobAsync( + public async Task RenewJobAsync( Uri requestUri, Guid planId, Guid jobId, @@ -121,11 +147,24 @@ public Task RenewJobAsync( requestUri = new Uri(requestUri, "renewjob"); var requestContent = new ObjectContent(payload, new VssJsonMediaTypeFormatter(true)); - return SendAsync( + var result = await SendAsync( httpMethod, requestUri, content: requestContent, cancellationToken: cancellationToken); + + if (result.IsSuccess) + { + return result.Value; + } + + switch (result.StatusCode) + { + case HttpStatusCode.NotFound: + throw new TaskOrchestrationJobNotFoundException($"Job not found: {jobId}"); + default: + throw new Exception($"Failed to renew job: {result.Error}"); + } } } } diff --git a/src/Sdk/WebApi/WebApi/BrokerHttpClient.cs b/src/Sdk/WebApi/WebApi/BrokerHttpClient.cs index 8f9b22e7541..e0254186672 100644 --- a/src/Sdk/WebApi/WebApi/BrokerHttpClient.cs +++ b/src/Sdk/WebApi/WebApi/BrokerHttpClient.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Net; using System.Net.Http; using System.Threading; using System.Threading.Tasks; @@ -55,7 +56,7 @@ public BrokerHttpClient( { } - public Task GetRunnerMessageAsync( + public async Task GetRunnerMessageAsync( string runnerVersion, TaskAgentStatus? status, CancellationToken cancellationToken = default @@ -74,11 +75,18 @@ public Task GetRunnerMessageAsync( queryParams.Add("runnerVersion", runnerVersion); } - return SendAsync( + var result = await SendAsync( new HttpMethod("GET"), requestUri: requestUri, queryParameters: queryParams, cancellationToken: cancellationToken); + + if (result.IsSuccess) + { + return result.Value; + } + + throw new Exception($"Failed to get job message: {result.Error}"); } } } diff --git a/src/Sdk/WebApi/WebApi/RawHttpClientBase.cs b/src/Sdk/WebApi/WebApi/RawHttpClientBase.cs index 8054a81145a..1a6cf33cba9 100644 --- a/src/Sdk/WebApi/WebApi/RawHttpClientBase.cs +++ b/src/Sdk/WebApi/WebApi/RawHttpClientBase.cs @@ -101,7 +101,7 @@ protected async Task SendAsync( } } - protected Task SendAsync( + protected Task> SendAsync( HttpMethod method, Uri requestUri, HttpContent content = null, @@ -112,7 +112,7 @@ protected Task SendAsync( return SendAsync(method, null, requestUri, content, queryParameters, userState, cancellationToken); } - protected async Task SendAsync( + protected async Task> SendAsync( HttpMethod method, IEnumerable> additionalHeaders, Uri requestUri, @@ -128,7 +128,7 @@ protected async Task SendAsync( } } - protected async Task SendAsync( + protected async Task> SendAsync( HttpRequestMessage message, Object userState = null, CancellationToken cancellationToken = default(CancellationToken)) @@ -138,7 +138,16 @@ protected async Task SendAsync( //from deadlocking... using (HttpResponseMessage response = await this.SendAsync(message, userState, cancellationToken).ConfigureAwait(false)) { - return await ReadContentAsAsync(response, cancellationToken).ConfigureAwait(false); + if (response.IsSuccessStatusCode) + { + T data = await ReadContentAsAsync(response, cancellationToken).ConfigureAwait(false); + return RawHttpClientResult.Ok(data); + } + else + { + string errorMessage = $"Error: {response.ReasonPhrase}"; + return RawHttpClientResult.Fail(errorMessage, response.StatusCode); + } } } diff --git a/src/Sdk/WebApi/WebApi/RawHttpClientResult.cs b/src/Sdk/WebApi/WebApi/RawHttpClientResult.cs new file mode 100644 index 00000000000..fd3b311b832 --- /dev/null +++ b/src/Sdk/WebApi/WebApi/RawHttpClientResult.cs @@ -0,0 +1,35 @@ +using System.Net; + +namespace Sdk.WebApi.WebApi +{ + public class RawHttpClientResult + { + public bool IsSuccess { get; protected set; } + public string Error { get; protected set; } + public HttpStatusCode StatusCode { get; protected set; } + public bool IsFailure => !IsSuccess; + + protected RawHttpClientResult(bool isSuccess, string error, HttpStatusCode statusCode) + { + IsSuccess = isSuccess; + Error = error; + StatusCode = statusCode; + } + } + + public class RawHttpClientResult : RawHttpClientResult + { + public T Value { get; private set; } + + protected internal RawHttpClientResult(T value, bool isSuccess, string error, HttpStatusCode statusCode) + : base(isSuccess, error, statusCode) + { + Value = value; + } + + public static RawHttpClientResult Fail(string message, HttpStatusCode statusCode) => new RawHttpClientResult(default(T), false, message, statusCode); + public static RawHttpClientResult Ok(T value) => new RawHttpClientResult(value, true, string.Empty, HttpStatusCode.OK); + } + + +}