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

Add SessionConflict return code #4726

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
7 changes: 6 additions & 1 deletion src/Agent.Listener/Agent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,12 @@ private async Task<int> RunAsync(AgentSettings settings, bool runOnce = false)
{
Trace.Info(nameof(RunAsync));
_listener = HostContext.GetService<IMessageListener>();
if (!await _listener.CreateSessionAsync(HostContext.AgentShutdownToken))
Constants.Agent.CreateSessionResult createSessionResult = await _listener.CreateSessionAsync(HostContext.AgentShutdownToken);
if (createSessionResult == Constants.Agent.CreateSessionResult.SessionConflict)
{
return Constants.Agent.ReturnCode.SessionConflict;
}
else if (createSessionResult == Constants.Agent.CreateSessionResult.Failure)
{
return Constants.Agent.ReturnCode.TerminatedError;
}
Expand Down
14 changes: 9 additions & 5 deletions src/Agent.Listener/MessageListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace Microsoft.VisualStudio.Services.Agent.Listener
[ServiceLocator(Default = typeof(MessageListener))]
public interface IMessageListener : IAgentService
{
Task<Boolean> CreateSessionAsync(CancellationToken token);
Task<Constants.Agent.CreateSessionResult> CreateSessionAsync(CancellationToken token);
Task DeleteSessionAsync();
Task<TaskAgentMessage> GetNextMessageAsync(CancellationToken token);
Task KeepAlive(CancellationToken token);
Expand Down Expand Up @@ -52,7 +52,7 @@ public override void Initialize(IHostContext hostContext)
_agentServer = HostContext.GetService<IAgentServer>();
}

public async Task<Boolean> CreateSessionAsync(CancellationToken token)
public async Task<Constants.Agent.CreateSessionResult> CreateSessionAsync(CancellationToken token)
{
Trace.Entering();

Expand Down Expand Up @@ -108,7 +108,7 @@ public async Task<Boolean> CreateSessionAsync(CancellationToken token)
encounteringError = false;
}

return true;
return Constants.Agent.CreateSessionResult.Success;
}
catch (OperationCanceledException) when (token.IsCancellationRequested)
{
Expand All @@ -133,7 +133,11 @@ public async Task<Boolean> CreateSessionAsync(CancellationToken token)
if (!IsSessionCreationExceptionRetriable(ex))
{
_term.WriteError(StringUtil.Loc("SessionCreateFailed", ex.Message));
return false;
if (ex is TaskAgentSessionConflictException)
{
return Constants.Agent.CreateSessionResult.SessionConflict;
}
return Constants.Agent.CreateSessionResult.Failure;
}

if (!encounteringError) //print the message only on the first error
Expand Down Expand Up @@ -211,7 +215,7 @@ public async Task<TaskAgentMessage> GetNextMessageAsync(CancellationToken token)
Trace.Error(ex);

// don't retry if SkipSessionRecover = true, DT service will delete agent session to stop agent from taking more jobs.
if (ex is TaskAgentSessionExpiredException && !_settings.SkipSessionRecover && await CreateSessionAsync(token))
if (ex is TaskAgentSessionExpiredException && !_settings.SkipSessionRecover && (await CreateSessionAsync(token) == Constants.Agent.CreateSessionResult.Success))
{
Trace.Info($"{nameof(TaskAgentSessionExpiredException)} received, recovered by recreate session.");
}
Expand Down
8 changes: 8 additions & 0 deletions src/Microsoft.VisualStudio.Services.Agent/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,13 +202,21 @@ public static class Flags
}
}

public enum CreateSessionResult
{
Success,
Failure,
SessionConflict
}

public static class ReturnCode
{
public const int Success = 0;
public const int TerminatedError = 1;
public const int RetryableError = 2;
public const int AgentUpdating = 3;
public const int RunOnceAgentUpdating = 4;
public const int SessionConflict = 5;
}

public static class AgentConfigurationProvider
Expand Down
16 changes: 8 additions & 8 deletions src/Test/L0/Listener/AgentL0.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public async void TestRunAsync()
_configurationManager.Setup(x => x.IsConfigured())
.Returns(true);
_messageListener.Setup(x => x.CreateSessionAsync(It.IsAny<CancellationToken>()))
.Returns(Task.FromResult<bool>(true));
.Returns(Task.FromResult<Constants.Agent.CreateSessionResult>(Constants.Agent.ReturnCode.Success));
_messageListener.Setup(x => x.GetNextMessageAsync(It.IsAny<CancellationToken>()))
.Returns(async () =>
{
Expand Down Expand Up @@ -208,7 +208,7 @@ public async void TestExecuteCommandForRunAsService(string[] args, bool configur
_configStore.Setup(x => x.IsServiceConfigured()).Returns(configureAsService);

_messageListener.Setup(x => x.CreateSessionAsync(It.IsAny<CancellationToken>()))
.Returns(Task.FromResult(false));
.Returns(Task.FromResult<Constants.Agent.CreateSessionResult>(Constants.Agent.CreateSessionResult.Failure));

agent.Initialize(hc);
await agent.ExecuteCommand(command);
Expand Down Expand Up @@ -245,7 +245,7 @@ public async void TestMachineProvisionerCLI()
.Returns(false);

_messageListener.Setup(x => x.CreateSessionAsync(It.IsAny<CancellationToken>()))
.Returns(Task.FromResult(false));
.Returns(Task.FromResult<Constants.Agent.CreateSessionResult>(Constants.Agent.CreateSessionResult.Failure));

agent.Initialize(hc);
await agent.ExecuteCommand(command);
Expand Down Expand Up @@ -282,7 +282,7 @@ public async void TestMachineProvisionerCLICompat()
.Returns(false);

_messageListener.Setup(x => x.CreateSessionAsync(It.IsAny<CancellationToken>()))
.Returns(Task.FromResult(false));
.Returns(Task.FromResult<Constants.Agent.CreateSessionResult>(Constants.Agent.CreateSessionResult.Failure));

agent.Initialize(hc);
await agent.ExecuteCommand(command);
Expand Down Expand Up @@ -330,7 +330,7 @@ public async void TestRunOnce()
_configurationManager.Setup(x => x.IsConfigured())
.Returns(true);
_messageListener.Setup(x => x.CreateSessionAsync(It.IsAny<CancellationToken>()))
.Returns(Task.FromResult<bool>(true));
.Returns(Task.FromResult<Constants.Agent.CreateSessionResult>(Constants.Agent.CreateSessionResult.Success));
_messageListener.Setup(x => x.GetNextMessageAsync(It.IsAny<CancellationToken>()))
.Returns(async () =>
{
Expand Down Expand Up @@ -435,7 +435,7 @@ public async void TestRunOnceOnlyTakeOneJobMessage()
_configurationManager.Setup(x => x.IsConfigured())
.Returns(true);
_messageListener.Setup(x => x.CreateSessionAsync(It.IsAny<CancellationToken>()))
.Returns(Task.FromResult<bool>(true));
.Returns(Task.FromResult<Constants.Agent.CreateSessionResult>(Constants.Agent.CreateSessionResult.Success));
_messageListener.Setup(x => x.GetNextMessageAsync(It.IsAny<CancellationToken>()))
.Returns(async () =>
{
Expand Down Expand Up @@ -537,7 +537,7 @@ public async void TestRunOnceHandleUpdateMessage()
_configurationManager.Setup(x => x.IsConfigured())
.Returns(true);
_messageListener.Setup(x => x.CreateSessionAsync(It.IsAny<CancellationToken>()))
.Returns(Task.FromResult<bool>(true));
.Returns(Task.FromResult<Constants.Agent.CreateSessionResult>(Constants.Agent.CreateSessionResult.Success));
_messageListener.Setup(x => x.GetNextMessageAsync(It.IsAny<CancellationToken>()))
.Returns(async () =>
{
Expand Down Expand Up @@ -766,7 +766,7 @@ public async void TestMetadataUpdate()
_configurationManager.Setup(x => x.IsConfigured())
.Returns(true);
_messageListener.Setup(x => x.CreateSessionAsync(It.IsAny<CancellationToken>()))
.Returns(Task.FromResult<bool>(true));
.Returns(Task.FromResult<Constants.Agent.CreateSessionResult>(Constants.Agent.CreateSessionResult.Success));
_messageListener.Setup(x => x.GetNextMessageAsync(It.IsAny<CancellationToken>()))
.Returns(async () =>
{
Expand Down
12 changes: 6 additions & 6 deletions src/Test/L0/Listener/MessageListenerL0.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@ public async void CreatesSession()
MessageListener listener = new MessageListener();
listener.Initialize(tc);

bool result = await listener.CreateSessionAsync(tokenSource.Token);
Constants.Agent.CreateSessionResult result = await listener.CreateSessionAsync(tokenSource.Token);
trace.Info("result: {0}", result);

// Assert.
Assert.True(result);
Assert.Equal(Constants.Agent.CreateSessionResult.Success, result);
_agentServer
.Verify(x => x.CreateAgentSessionAsync(
_settings.PoolId,
Expand Down Expand Up @@ -132,8 +132,8 @@ public async void DeleteSession()
MessageListener listener = new MessageListener();
listener.Initialize(tc);

bool result = await listener.CreateSessionAsync(tokenSource.Token);
Assert.True(result);
Constants.Agent.CreateSessionResult result = await listener.CreateSessionAsync(tokenSource.Token);
Assert.Equal(Constants.Agent.CreateSessionResult.Success, result);

_agentServer
.Setup(x => x.DeleteAgentSessionAsync(
Expand Down Expand Up @@ -179,8 +179,8 @@ public async void GetNextMessage()
MessageListener listener = new MessageListener();
listener.Initialize(tc);

bool result = await listener.CreateSessionAsync(tokenSource.Token);
Assert.True(result);
Constants.Agent.CreateSessionResult result = await listener.CreateSessionAsync(tokenSource.Token);
Assert.Equal(Constants.Agent.CreateSessionResult.Success, result);

var arMessages = new TaskAgentMessage[]
{
Expand Down