Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Runner.Common/BrokerServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public Task ForceRefreshConnection(VssCredentials credentials)

public bool ShouldRetryException(Exception ex)
{
if (ex is AccessDeniedException ade && ade.ErrorCode == 1)
if (ex is AccessDeniedException ade)
{
return false;
}
Expand Down
20 changes: 20 additions & 0 deletions src/Sdk/RSWebApi/Contracts/BrokerError.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Runtime.Serialization;

namespace GitHub.Actions.RunService.WebApi
{
[DataContract]
public class BrokerError
{
[DataMember(Name = "source", EmitDefaultValue = false)]
public string Source { get; set; }

[DataMember(Name = "errorKind", EmitDefaultValue = false)]
public string ErrorKind { get; set; }

[DataMember(Name = "statusCode", EmitDefaultValue = false)]
public int StatusCode { get; set; }

[DataMember(Name = "errorMessage", EmitDefaultValue = false)]
public string Message { get; set; }
}
}
10 changes: 10 additions & 0 deletions src/Sdk/RSWebApi/Contracts/BrokerErrorKind.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Runtime.Serialization;

namespace GitHub.Actions.RunService.WebApi
{
[DataContract]
public class BrokerErrorKind
{
public const string RunnerVersionTooOld = "RunnerVersionTooOld";
}
}
41 changes: 38 additions & 3 deletions src/Sdk/WebApi/WebApi/BrokerHttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,29 @@ public async Task<TaskAgentMessage> GetRunnerMessageAsync(
new HttpMethod("GET"),
requestUri: requestUri,
queryParameters: queryParams,
readErrorBody: true,
cancellationToken: cancellationToken);

if (result.IsSuccess)
{
return result.Value;
}

// the only time we throw a `Forbidden` exception from Listener /messages is when the runner is
// disable_update and is too old to poll
if (TryParseErrorBody(result.ErrorBody, out BrokerError brokerError))
{
switch (brokerError.ErrorKind)
{
case BrokerErrorKind.RunnerVersionTooOld:
throw new AccessDeniedException(brokerError.Message)
{
ErrorCode = 1
};
default:
break;
}
}

// temporary back compat
if (result.StatusCode == HttpStatusCode.Forbidden)
{
throw new AccessDeniedException($"{result.Error} Runner version v{runnerVersion} is deprecated and cannot receive messages.")
Expand All @@ -120,7 +134,7 @@ public async Task<TaskAgentMessage> GetRunnerMessageAsync(
};
}

throw new Exception($"Failed to get job message: {result.Error}");
throw new Exception($"Failed to get job message. Request to {requestUri} failed with status: {result.StatusCode}. Error message {result.Error}");
}

public async Task<TaskAgentSession> CreateSessionAsync(
Expand Down Expand Up @@ -172,5 +186,26 @@ public async Task DeleteSessionAsync(

throw new Exception($"Failed to delete broker session: {result.Error}");
}

private static bool TryParseErrorBody(string errorBody, out BrokerError error)
{
if (!string.IsNullOrEmpty(errorBody))
{
try
{
error = JsonUtility.FromString<BrokerError>(errorBody);
if (error?.Source == "actions-broker-listener")
{
return true;
}
}
catch (Exception)
{
}
}

error = null;
return false;
}
}
}