diff --git a/src/Agent.Listener/Agent.cs b/src/Agent.Listener/Agent.cs index 34bb691944..bc10f46e44 100644 --- a/src/Agent.Listener/Agent.cs +++ b/src/Agent.Listener/Agent.cs @@ -327,7 +327,12 @@ private async Task RunAsync(AgentSettings settings, bool runOnce = false) { Trace.Info(nameof(RunAsync)); _listener = HostContext.GetService(); - 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; } diff --git a/src/Agent.Listener/MessageListener.cs b/src/Agent.Listener/MessageListener.cs index eebca9d1f7..2b4dbfce0e 100644 --- a/src/Agent.Listener/MessageListener.cs +++ b/src/Agent.Listener/MessageListener.cs @@ -24,7 +24,7 @@ namespace Microsoft.VisualStudio.Services.Agent.Listener [ServiceLocator(Default = typeof(MessageListener))] public interface IMessageListener : IAgentService { - Task CreateSessionAsync(CancellationToken token); + Task CreateSessionAsync(CancellationToken token); Task DeleteSessionAsync(); Task GetNextMessageAsync(CancellationToken token); Task KeepAlive(CancellationToken token); @@ -52,7 +52,7 @@ public override void Initialize(IHostContext hostContext) _agentServer = HostContext.GetService(); } - public async Task CreateSessionAsync(CancellationToken token) + public async Task CreateSessionAsync(CancellationToken token) { Trace.Entering(); @@ -108,7 +108,7 @@ public async Task CreateSessionAsync(CancellationToken token) encounteringError = false; } - return true; + return Constants.Agent.CreateSessionResult.Success; } catch (OperationCanceledException) when (token.IsCancellationRequested) { @@ -133,7 +133,11 @@ public async Task 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 @@ -211,7 +215,7 @@ public async Task 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."); } diff --git a/src/Microsoft.VisualStudio.Services.Agent/Constants.cs b/src/Microsoft.VisualStudio.Services.Agent/Constants.cs index 2b0f630ae8..143ecd8e4f 100644 --- a/src/Microsoft.VisualStudio.Services.Agent/Constants.cs +++ b/src/Microsoft.VisualStudio.Services.Agent/Constants.cs @@ -202,6 +202,13 @@ public static class Flags } } + public enum CreateSessionResult + { + Success, + Failure, + SessionConflict + } + public static class ReturnCode { public const int Success = 0; @@ -209,6 +216,7 @@ public static class ReturnCode public const int RetryableError = 2; public const int AgentUpdating = 3; public const int RunOnceAgentUpdating = 4; + public const int SessionConflict = 5; } public static class AgentConfigurationProvider diff --git a/src/Test/L0/Listener/AgentL0.cs b/src/Test/L0/Listener/AgentL0.cs index 14250ab78d..a74c710fdf 100644 --- a/src/Test/L0/Listener/AgentL0.cs +++ b/src/Test/L0/Listener/AgentL0.cs @@ -107,7 +107,7 @@ public async void TestRunAsync() _configurationManager.Setup(x => x.IsConfigured()) .Returns(true); _messageListener.Setup(x => x.CreateSessionAsync(It.IsAny())) - .Returns(Task.FromResult(true)); + .Returns(Task.FromResult(Constants.Agent.ReturnCode.Success)); _messageListener.Setup(x => x.GetNextMessageAsync(It.IsAny())) .Returns(async () => { @@ -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())) - .Returns(Task.FromResult(false)); + .Returns(Task.FromResult(Constants.Agent.CreateSessionResult.Failure)); agent.Initialize(hc); await agent.ExecuteCommand(command); @@ -245,7 +245,7 @@ public async void TestMachineProvisionerCLI() .Returns(false); _messageListener.Setup(x => x.CreateSessionAsync(It.IsAny())) - .Returns(Task.FromResult(false)); + .Returns(Task.FromResult(Constants.Agent.CreateSessionResult.Failure)); agent.Initialize(hc); await agent.ExecuteCommand(command); @@ -282,7 +282,7 @@ public async void TestMachineProvisionerCLICompat() .Returns(false); _messageListener.Setup(x => x.CreateSessionAsync(It.IsAny())) - .Returns(Task.FromResult(false)); + .Returns(Task.FromResult(Constants.Agent.CreateSessionResult.Failure)); agent.Initialize(hc); await agent.ExecuteCommand(command); @@ -330,7 +330,7 @@ public async void TestRunOnce() _configurationManager.Setup(x => x.IsConfigured()) .Returns(true); _messageListener.Setup(x => x.CreateSessionAsync(It.IsAny())) - .Returns(Task.FromResult(true)); + .Returns(Task.FromResult(Constants.Agent.CreateSessionResult.Success)); _messageListener.Setup(x => x.GetNextMessageAsync(It.IsAny())) .Returns(async () => { @@ -435,7 +435,7 @@ public async void TestRunOnceOnlyTakeOneJobMessage() _configurationManager.Setup(x => x.IsConfigured()) .Returns(true); _messageListener.Setup(x => x.CreateSessionAsync(It.IsAny())) - .Returns(Task.FromResult(true)); + .Returns(Task.FromResult(Constants.Agent.CreateSessionResult.Success)); _messageListener.Setup(x => x.GetNextMessageAsync(It.IsAny())) .Returns(async () => { @@ -537,7 +537,7 @@ public async void TestRunOnceHandleUpdateMessage() _configurationManager.Setup(x => x.IsConfigured()) .Returns(true); _messageListener.Setup(x => x.CreateSessionAsync(It.IsAny())) - .Returns(Task.FromResult(true)); + .Returns(Task.FromResult(Constants.Agent.CreateSessionResult.Success)); _messageListener.Setup(x => x.GetNextMessageAsync(It.IsAny())) .Returns(async () => { @@ -766,7 +766,7 @@ public async void TestMetadataUpdate() _configurationManager.Setup(x => x.IsConfigured()) .Returns(true); _messageListener.Setup(x => x.CreateSessionAsync(It.IsAny())) - .Returns(Task.FromResult(true)); + .Returns(Task.FromResult(Constants.Agent.CreateSessionResult.Success)); _messageListener.Setup(x => x.GetNextMessageAsync(It.IsAny())) .Returns(async () => { diff --git a/src/Test/L0/Listener/MessageListenerL0.cs b/src/Test/L0/Listener/MessageListenerL0.cs index 45a24d7b7e..1674f268d9 100644 --- a/src/Test/L0/Listener/MessageListenerL0.cs +++ b/src/Test/L0/Listener/MessageListenerL0.cs @@ -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, @@ -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( @@ -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[] {