Skip to content
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

Implement Broker Redirects for Session and Messages #3103

Merged
merged 19 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Implement DeleteSession
  • Loading branch information
luketomlinson committed Jan 29, 2024
commit e5ef4313a12364a20173c09aa5959f3a71bc241d
7 changes: 4 additions & 3 deletions src/Runner.Common/BrokerServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public interface IBrokerServer : IRunnerService

Task<TaskAgentSession> CreateSessionAsync(TaskAgentSession session, CancellationToken cancellationToken);

Task DeleteSessionAsync(Guid sessionId, CancellationToken cancellationToken);
Task DeleteSessionAsync(CancellationToken cancellationToken);
Task<TaskAgentMessage> GetRunnerMessageAsync(Guid? sessionId, TaskAgentStatus status, string version, string os, string architecture, bool disableUpdate, CancellationToken token);
}

Expand Down Expand Up @@ -64,9 +64,10 @@ public Task<TaskAgentMessage> GetRunnerMessageAsync(Guid? sessionId, TaskAgentSt
return brokerSession;
}

public async Task DeleteSessionAsync(Guid sessionId, CancellationToken cancellationToken)
public async Task DeleteSessionAsync(CancellationToken cancellationToken)
{
await Task.CompletedTask; // not implemented yet
CheckConnection();
await _brokerHttpClient.DeleteSessionAsync(cancellationToken);
}
}
}
4 changes: 2 additions & 2 deletions src/Runner.Listener/BrokerMessageListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public async Task<Boolean> CreateSessionAsync(CancellationToken token)
// Settings
var configManager = HostContext.GetService<IConfigurationManager>();
_settings = configManager.LoadSettings();
var serverUrl = _settings.ServerUrl;
var serverUrl = _settings.ServerUrlV2;
Trace.Info(_settings);

// Create connection.
Expand Down Expand Up @@ -159,7 +159,7 @@ public async Task DeleteSessionAsync()
{
using (var ts = new CancellationTokenSource(TimeSpan.FromSeconds(30)))
{
await _brokerServer.DeleteSessionAsync(_session.SessionId, ts.Token);
await _brokerServer.DeleteSessionAsync(ts.Token);
}
}
else
Expand Down
13 changes: 10 additions & 3 deletions src/Runner.Listener/MessageListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public sealed class MessageListener : RunnerService, IMessageListener
private CancellationTokenSource _getMessagesTokenSource;
private VssCredentials _creds;

private bool _isBrokerSession = false;

public override void Initialize(IHostContext hostContext)
{
base.Initialize(hostContext);
Expand Down Expand Up @@ -105,9 +107,9 @@ public async Task<Boolean> CreateSessionAsync(CancellationToken token)
if (_session.BrokerMigrationMessage != null)
{
Trace.Info("Runner session is in migration mode: Creating Broker session with BrokerBaseUrl: {0}", _session.BrokerMigrationMessage.BrokerBaseUrl);
var brokerServer = HostContext.GetService<IBrokerServer>();
await brokerServer.ConnectAsync(_session.BrokerMigrationMessage.BrokerBaseUrl, _creds);
_session = await brokerServer.CreateSessionAsync(taskAgentSession, token);
await _brokerServer.ConnectAsync(_session.BrokerMigrationMessage.BrokerBaseUrl, _creds);
_session = await _brokerServer.CreateSessionAsync(taskAgentSession, token);
_isBrokerSession = true;
}

Trace.Info($"Session created.");
Expand Down Expand Up @@ -183,6 +185,11 @@ public async Task DeleteSessionAsync()
{
using (var ts = new CancellationTokenSource(TimeSpan.FromSeconds(30)))
{
if (_isBrokerSession)
{
await _brokerServer.DeleteSessionAsync(ts.Token);
return;
}
await _runnerServer.DeleteAgentSessionAsync(_settings.PoolId, _session.SessionId, ts.Token);
}
}
Expand Down
1 change: 1 addition & 0 deletions src/Runner.Listener/Runner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,7 @@ await runServer.GetJobMessageAsync(messageRef.RunnerRequestId,
{
try
{
Trace.Info("Deleting Runner Session...");
await _listener.DeleteSessionAsync();
}
catch (Exception ex) when (runOnce)
Expand Down
18 changes: 18 additions & 0 deletions src/Sdk/WebApi/WebApi/BrokerHttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,5 +149,23 @@ public async Task<TaskAgentSession> CreateSessionAsync(

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

public async Task DeleteSessionAsync(
CancellationToken cancellationToken = default)
{
var requestUri = new Uri(Client.BaseAddress, $"session");

var result = await SendAsync<object>(
new HttpMethod("DELETE"),
requestUri: requestUri,
cancellationToken: cancellationToken);

if (result.IsSuccess)
{
return;
}

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