Browse Source

Make multi-project sample more resiliant

Fixes: #100

Makes the sample/test more idiomatic and resiliant
pull/393/head
Ryan Nowak 7 years ago
parent
commit
5b9eb17c90
  1. 2
      test/E2ETest/testassets/projects/Directory.Build.props
  2. 26
      test/E2ETest/testassets/projects/multi-project/worker/QueueWorker.cs

2
test/E2ETest/testassets/projects/Directory.Build.props

@ -3,6 +3,6 @@
<PropertyGroup> <PropertyGroup>
<!-- Used for P2Ps in our samples. The tests know how to set this value so that references work. --> <!-- Used for P2Ps in our samples. The tests know how to set this value so that references work. -->
<TyeLibrariesPath>$(MSBuildThisFileDirectory)..\src\</TyeLibrariesPath> <TyeLibrariesPath>$(MSBuildThisFileDirectory)..\..\..\..\src\</TyeLibrariesPath>
</PropertyGroup> </PropertyGroup>
</Project> </Project>

26
test/E2ETest/testassets/projects/multi-project/worker/QueueWorker.cs

@ -15,7 +15,7 @@ using RabbitMQ.Client.Events;
namespace Worker namespace Worker
{ {
public class QueueWorker : IHostedService public class QueueWorker : BackgroundService
{ {
private readonly IConfiguration _configuration; private readonly IConfiguration _configuration;
private readonly ILogger<QueueWorker> _logger; private readonly ILogger<QueueWorker> _logger;
@ -26,7 +26,7 @@ namespace Worker
_configuration = configuration; _configuration = configuration;
} }
public async Task StartAsync(CancellationToken cancellationToken) protected async override Task ExecuteAsync(CancellationToken cancellationToken)
{ {
try try
{ {
@ -51,6 +51,10 @@ namespace Worker
autoAck: true, autoAck: true,
consumer: consumer); consumer: consumer);
} }
catch (OperationCanceledException)
{
throw;
}
catch (Exception ex) catch (Exception ex)
{ {
_logger.LogError(0, ex, "Failed to start listening to rabbit mq"); _logger.LogError(0, ex, "Failed to start listening to rabbit mq");
@ -60,8 +64,7 @@ namespace Worker
private async Task<IModel> ConnectAsync(CancellationToken cancellationToken) private async Task<IModel> ConnectAsync(CancellationToken cancellationToken)
{ {
ExceptionDispatchInfo? edi = null; while (true)
for (var i = 0; i < 5; i++)
{ {
try try
{ {
@ -78,24 +81,13 @@ namespace Worker
} }
catch (Exception ex) catch (Exception ex)
{ {
if (i == 4)
{
edi = ExceptionDispatchInfo.Capture(ex);
}
_logger.LogError(0, ex, "Failed to start listening to rabbit mq"); _logger.LogError(0, ex, "Failed to start listening to rabbit mq");
} }
// Rely on the Task.Delay to throw and exit the loop if we're still waiting for connection
// when shutdown happens.
await Task.Delay(5000, cancellationToken); await Task.Delay(5000, cancellationToken);
} }
edi!.Throw();
throw null; //unreachable
}
public Task StopAsync(CancellationToken cancellationToken)
{
return Task.CompletedTask;
} }
} }
} }

Loading…
Cancel
Save