mirror of
https://github.com/actions/runner.git
synced 2025-12-12 05:37:01 +00:00
GitHub Actions Runner
This commit is contained in:
61
src/Runner.Common/LocationServer.cs
Normal file
61
src/Runner.Common/LocationServer.cs
Normal file
@@ -0,0 +1,61 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using GitHub.Services.WebApi;
|
||||
using GitHub.Services.Location.Client;
|
||||
using GitHub.Services.Location;
|
||||
|
||||
namespace GitHub.Runner.Common
|
||||
{
|
||||
[ServiceLocator(Default = typeof(LocationServer))]
|
||||
public interface ILocationServer : IRunnerService
|
||||
{
|
||||
Task ConnectAsync(VssConnection jobConnection);
|
||||
|
||||
Task<ConnectionData> GetConnectionDataAsync();
|
||||
}
|
||||
|
||||
public sealed class LocationServer : RunnerService, ILocationServer
|
||||
{
|
||||
private bool _hasConnection;
|
||||
private VssConnection _connection;
|
||||
private LocationHttpClient _locationClient;
|
||||
|
||||
public async Task ConnectAsync(VssConnection jobConnection)
|
||||
{
|
||||
_connection = jobConnection;
|
||||
int attemptCount = 5;
|
||||
while (!_connection.HasAuthenticated && attemptCount-- > 0)
|
||||
{
|
||||
try
|
||||
{
|
||||
await _connection.ConnectAsync();
|
||||
break;
|
||||
}
|
||||
catch (Exception ex) when (attemptCount > 0)
|
||||
{
|
||||
Trace.Info($"Catch exception during connect. {attemptCount} attempt left.");
|
||||
Trace.Error(ex);
|
||||
}
|
||||
|
||||
await Task.Delay(100);
|
||||
}
|
||||
|
||||
_locationClient = _connection.GetClient<LocationHttpClient>();
|
||||
_hasConnection = true;
|
||||
}
|
||||
|
||||
private void CheckConnection()
|
||||
{
|
||||
if (!_hasConnection)
|
||||
{
|
||||
throw new InvalidOperationException("SetConnection");
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ConnectionData> GetConnectionDataAsync()
|
||||
{
|
||||
CheckConnection();
|
||||
return await _locationClient.GetConnectionDataAsync(ConnectOptions.None, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user