Skip to content

Commit

Permalink
Move enum
Browse files Browse the repository at this point in the history
  • Loading branch information
eeSquared committed Mar 27, 2024
1 parent 17fcae2 commit 12f05a9
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 48 deletions.
7 changes: 0 additions & 7 deletions src/Runner.Common/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,6 @@ public static class Flags
}
}

public enum CreateSessionResult
{
Success,
Failure,
SessionConflict
}

public static class ReturnCode
{
public const int Success = 0;
Expand Down
12 changes: 6 additions & 6 deletions src/Runner.Listener/BrokerMessageListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public override void Initialize(IHostContext hostContext)
_brokerServer = HostContext.GetService<IBrokerServer>();
}

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

Expand Down Expand Up @@ -99,7 +99,7 @@ public override void Initialize(IHostContext hostContext)
encounteringError = false;
}

return Constants.Runner.CreateSessionResult.Success;
return CreateSessionResult.Success;
}
catch (OperationCanceledException) when (token.IsCancellationRequested)
{
Expand All @@ -123,7 +123,7 @@ public override void Initialize(IHostContext hostContext)
if (string.Equals(vssOAuthEx.Error, "invalid_client", StringComparison.OrdinalIgnoreCase))
{
_term.WriteError("Failed to create a session. The runner registration has been deleted from the server, please re-configure. Runner registrations are automatically deleted for runners that have not connected to the service recently.");
return Constants.Runner.CreateSessionResult.Failure;
return CreateSessionResult.Failure;
}

// Check whether we get 401 because the runner registration already removed by the service.
Expand All @@ -134,7 +134,7 @@ public override void Initialize(IHostContext hostContext)
if (string.Equals(authError, "invalid_client", StringComparison.OrdinalIgnoreCase))
{
_term.WriteError("Failed to create a session. The runner registration has been deleted from the server, please re-configure. Runner registrations are automatically deleted for runners that have not connected to the service recently.");
return Constants.Runner.CreateSessionResult.Failure;
return CreateSessionResult.Failure;
}
}

Expand All @@ -143,9 +143,9 @@ public override void Initialize(IHostContext hostContext)
_term.WriteError($"Failed to create session. {ex.Message}");
if (ex is TaskAgentSessionConflictException)
{
return Constants.Runner.CreateSessionResult.SessionConflict;
return CreateSessionResult.SessionConflict;
}
return Constants.Runner.CreateSessionResult.Failure;
return CreateSessionResult.Failure;
}

if (!encounteringError) //print the message only on the first error
Expand Down
23 changes: 15 additions & 8 deletions src/Runner.Listener/MessageListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,17 @@

namespace GitHub.Runner.Listener
{
public enum CreateSessionResult
{
Success,
Failure,
SessionConflict
}

[ServiceLocator(Default = typeof(MessageListener))]
public interface IMessageListener : IRunnerService
{
Task<Constants.Runner.CreateSessionResult> CreateSessionAsync(CancellationToken token);
Task<CreateSessionResult> CreateSessionAsync(CancellationToken token);
Task DeleteSessionAsync();
Task<TaskAgentMessage> GetNextMessageAsync(CancellationToken token);
Task DeleteMessageAsync(TaskAgentMessage message);
Expand Down Expand Up @@ -59,7 +66,7 @@ public override void Initialize(IHostContext hostContext)
_brokerServer = hostContext.GetService<IBrokerServer>();
}

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

Expand Down Expand Up @@ -123,7 +130,7 @@ public override void Initialize(IHostContext hostContext)
encounteringError = false;
}

return Constants.Runner.CreateSessionResult.Success;
return CreateSessionResult.Success;
}
catch (OperationCanceledException) when (token.IsCancellationRequested)
{
Expand All @@ -147,7 +154,7 @@ public override void Initialize(IHostContext hostContext)
if (string.Equals(vssOAuthEx.Error, "invalid_client", StringComparison.OrdinalIgnoreCase))
{
_term.WriteError("Failed to create a session. The runner registration has been deleted from the server, please re-configure. Runner registrations are automatically deleted for runners that have not connected to the service recently.");
return Constants.Runner.CreateSessionResult.Failure;
return CreateSessionResult.Failure;
}

// Check whether we get 401 because the runner registration already removed by the service.
Expand All @@ -158,7 +165,7 @@ public override void Initialize(IHostContext hostContext)
if (string.Equals(authError, "invalid_client", StringComparison.OrdinalIgnoreCase))
{
_term.WriteError("Failed to create a session. The runner registration has been deleted from the server, please re-configure. Runner registrations are automatically deleted for runners that have not connected to the service recently.");
return Constants.Runner.CreateSessionResult.Failure;
return CreateSessionResult.Failure;
}
}

Expand All @@ -167,9 +174,9 @@ public override void Initialize(IHostContext hostContext)
_term.WriteError($"Failed to create session. {ex.Message}");
if (ex is TaskAgentSessionConflictException)
{
return Constants.Runner.CreateSessionResult.SessionConflict;
return CreateSessionResult.SessionConflict;
}
return Constants.Runner.CreateSessionResult.Failure;
return CreateSessionResult.Failure;
}

if (!encounteringError) //print the message only on the first error
Expand Down Expand Up @@ -307,7 +314,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) == Constants.Runner.CreateSessionResult.Success))
if (ex is TaskAgentSessionExpiredException && !_settings.SkipSessionRecover && (await CreateSessionAsync(token) == CreateSessionResult.Success))
{
Trace.Info($"{nameof(TaskAgentSessionExpiredException)} received, recovered by recreate session.");
}
Expand Down
6 changes: 3 additions & 3 deletions src/Runner.Listener/Runner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -359,12 +359,12 @@ private async Task<int> RunAsync(RunnerSettings settings, bool runOnce = false)
{
Trace.Info(nameof(RunAsync));
_listener = GetMesageListener(settings);
Constants.Runner.CreateSessionResult createSessionResult = await _listener.CreateSessionAsync(HostContext.RunnerShutdownToken);
if (createSessionResult == Constants.Runner.CreateSessionResult.SessionConflict)
CreateSessionResult createSessionResult = await _listener.CreateSessionAsync(HostContext.RunnerShutdownToken);
if (createSessionResult == CreateSessionResult.SessionConflict)
{
return Constants.Runner.ReturnCode.SessionConflict;
}
else if (createSessionResult == Constants.Runner.CreateSessionResult.Failure)
else if (createSessionResult == CreateSessionResult.Failure)
{
return Constants.Runner.ReturnCode.TerminatedError;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Test/L0/Listener/BrokerMessageListenerL0.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ public async void CreatesSession()
BrokerMessageListener listener = new();
listener.Initialize(tc);

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

// Assert.
Assert.Equal(Constants.Runner.CreateSessionResult.Success, result);
Assert.Equal(CreateSessionResult.Success, result);
_brokerServer
.Verify(x => x.CreateSessionAsync(
It.Is<TaskAgentSession>(y => y != null),
Expand Down
32 changes: 16 additions & 16 deletions src/Test/L0/Listener/MessageListenerL0.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@ public async void CreatesSession()
MessageListener listener = new();
listener.Initialize(tc);

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

// Assert.
Assert.Equal(Constants.Runner.CreateSessionResult.Success, result);
Assert.Equal(CreateSessionResult.Success, result);
_runnerServer
.Verify(x => x.CreateAgentSessionAsync(
_settings.PoolId,
Expand Down Expand Up @@ -135,11 +135,11 @@ public async void CreatesSessionWithBrokerMigration()
MessageListener listener = new();
listener.Initialize(tc);

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

// Assert.
Assert.Equal(Constants.Runner.CreateSessionResult.Success, result);
Assert.Equal(CreateSessionResult.Success, result);

_runnerServer
.Verify(x => x.CreateAgentSessionAsync(
Expand Down Expand Up @@ -185,8 +185,8 @@ public async void DeleteSession()
MessageListener listener = new();
listener.Initialize(tc);

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

_runnerServer
.Setup(x => x.DeleteAgentSessionAsync(
Expand Down Expand Up @@ -245,10 +245,10 @@ public async void DeleteSessionWithBrokerMigration()
MessageListener listener = new();
listener.Initialize(tc);

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

Assert.Equal(Constants.Runner.CreateSessionResult.Success, result);
Assert.Equal(CreateSessionResult.Success, result);

_runnerServer
.Verify(x => x.CreateAgentSessionAsync(
Expand Down Expand Up @@ -309,8 +309,8 @@ public async void GetNextMessage()
MessageListener listener = new();
listener.Initialize(tc);

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

var arMessages = new TaskAgentMessage[]
{
Expand Down Expand Up @@ -390,8 +390,8 @@ public async void GetNextMessageWithBrokerMigration()
MessageListener listener = new();
listener.Initialize(tc);

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

var brokerMigrationMesage = new BrokerMigrationMessage(new Uri("https://actions.broker.com"));

Expand Down Expand Up @@ -497,11 +497,11 @@ public async void CreateSessionWithOriginalCredential()
MessageListener listener = new();
listener.Initialize(tc);

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

// Assert.
Assert.Equal(Constants.Runner.CreateSessionResult.Success, result);
Assert.Equal(CreateSessionResult.Success, result);
_runnerServer
.Verify(x => x.CreateAgentSessionAsync(
_settings.PoolId,
Expand Down Expand Up @@ -541,8 +541,8 @@ public async void SkipDeleteSession_WhenGetNextMessageGetTaskAgentAccessTokenExp
MessageListener listener = new();
listener.Initialize(tc);

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

_runnerServer
.Setup(x => x.GetAgentMessageAsync(
Expand Down
12 changes: 6 additions & 6 deletions src/Test/L0/Listener/RunnerL0.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public async void TestRunAsync()
_configurationManager.Setup(x => x.IsConfigured())
.Returns(true);
_messageListener.Setup(x => x.CreateSessionAsync(It.IsAny<CancellationToken>()))
.Returns(Task.FromResult<Constants.Runner.CreateSessionResult>(Constants.Runner.CreateSessionResult.Success));
.Returns(Task.FromResult<CreateSessionResult>(CreateSessionResult.Success));
_messageListener.Setup(x => x.GetNextMessageAsync(It.IsAny<CancellationToken>()))
.Returns(async () =>
{
Expand Down Expand Up @@ -184,7 +184,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<Constants.Runner.CreateSessionResult>(Constants.Runner.CreateSessionResult.Failure));
.Returns(Task.FromResult<CreateSessionResult>(CreateSessionResult.Failure));

var runner = new Runner.Listener.Runner();
runner.Initialize(hc);
Expand Down Expand Up @@ -217,7 +217,7 @@ public async void TestMachineProvisionerCLI()
.Returns(false);

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

var runner = new Runner.Listener.Runner();
runner.Initialize(hc);
Expand Down Expand Up @@ -263,7 +263,7 @@ public async void TestRunOnce()
_configurationManager.Setup(x => x.IsConfigured())
.Returns(true);
_messageListener.Setup(x => x.CreateSessionAsync(It.IsAny<CancellationToken>()))
.Returns(Task.FromResult<Constants.Runner.CreateSessionResult>(Constants.Runner.CreateSessionResult.Success));
.Returns(Task.FromResult<CreateSessionResult>(CreateSessionResult.Success));
_messageListener.Setup(x => x.GetNextMessageAsync(It.IsAny<CancellationToken>()))
.Returns(async () =>
{
Expand Down Expand Up @@ -363,7 +363,7 @@ public async void TestRunOnceOnlyTakeOneJobMessage()
_configurationManager.Setup(x => x.IsConfigured())
.Returns(true);
_messageListener.Setup(x => x.CreateSessionAsync(It.IsAny<CancellationToken>()))
.Returns(Task.FromResult<Constants.Runner.CreateSessionResult>(Constants.Runner.CreateSessionResult.Success));
.Returns(Task.FromResult<CreateSessionResult>(CreateSessionResult.Success));
_messageListener.Setup(x => x.GetNextMessageAsync(It.IsAny<CancellationToken>()))
.Returns(async () =>
{
Expand Down Expand Up @@ -458,7 +458,7 @@ public async void TestRunOnceHandleUpdateMessage()
_configurationManager.Setup(x => x.IsConfigured())
.Returns(true);
_messageListener.Setup(x => x.CreateSessionAsync(It.IsAny<CancellationToken>()))
.Returns(Task.FromResult<Constants.Runner.CreateSessionResult>(Constants.Runner.CreateSessionResult.Success));
.Returns(Task.FromResult<CreateSessionResult>(CreateSessionResult.Success));
_messageListener.Setup(x => x.GetNextMessageAsync(It.IsAny<CancellationToken>()))
.Returns(async () =>
{
Expand Down

0 comments on commit 12f05a9

Please sign in to comment.