From 5b9eb17c90b4c77bf1c3f189af0a6fecb9cd3d8d Mon Sep 17 00:00:00 2001 From: Ryan Nowak Date: Mon, 20 Apr 2020 13:55:28 -0700 Subject: [PATCH] Make multi-project sample more resiliant Fixes: #100 Makes the sample/test more idiomatic and resiliant --- .../testassets/projects/Directory.Build.props | 2 +- .../multi-project/worker/QueueWorker.cs | 26 +++++++------------ 2 files changed, 10 insertions(+), 18 deletions(-) diff --git a/test/E2ETest/testassets/projects/Directory.Build.props b/test/E2ETest/testassets/projects/Directory.Build.props index fa576708..b7f4c349 100644 --- a/test/E2ETest/testassets/projects/Directory.Build.props +++ b/test/E2ETest/testassets/projects/Directory.Build.props @@ -3,6 +3,6 @@ - $(MSBuildThisFileDirectory)..\src\ + $(MSBuildThisFileDirectory)..\..\..\..\src\ \ No newline at end of file diff --git a/test/E2ETest/testassets/projects/multi-project/worker/QueueWorker.cs b/test/E2ETest/testassets/projects/multi-project/worker/QueueWorker.cs index fec59556..3ae21e39 100644 --- a/test/E2ETest/testassets/projects/multi-project/worker/QueueWorker.cs +++ b/test/E2ETest/testassets/projects/multi-project/worker/QueueWorker.cs @@ -15,7 +15,7 @@ using RabbitMQ.Client.Events; namespace Worker { - public class QueueWorker : IHostedService + public class QueueWorker : BackgroundService { private readonly IConfiguration _configuration; private readonly ILogger _logger; @@ -26,7 +26,7 @@ namespace Worker _configuration = configuration; } - public async Task StartAsync(CancellationToken cancellationToken) + protected async override Task ExecuteAsync(CancellationToken cancellationToken) { try { @@ -51,6 +51,10 @@ namespace Worker autoAck: true, consumer: consumer); } + catch (OperationCanceledException) + { + throw; + } catch (Exception ex) { _logger.LogError(0, ex, "Failed to start listening to rabbit mq"); @@ -60,8 +64,7 @@ namespace Worker private async Task ConnectAsync(CancellationToken cancellationToken) { - ExceptionDispatchInfo? edi = null; - for (var i = 0; i < 5; i++) + while (true) { try { @@ -78,24 +81,13 @@ namespace Worker } catch (Exception ex) { - if (i == 4) - { - edi = ExceptionDispatchInfo.Capture(ex); - } - _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); } - - edi!.Throw(); - throw null; //unreachable - } - - public Task StopAsync(CancellationToken cancellationToken) - { - return Task.CompletedTask; } } }