Skip to content

Commit

Permalink
Make multi-project sample more resiliant
Browse files Browse the repository at this point in the history
Fixes: dotnet#100

Makes the sample/test more idiomatic and resiliant
  • Loading branch information
rynowak authored and kishanAnem committed May 15, 2020
1 parent 80f0416 commit 2b515c3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 18 deletions.
2 changes: 1 addition & 1 deletion test/E2ETest/testassets/projects/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@

<PropertyGroup>
<!-- 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>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

namespace Worker
{
public class QueueWorker : IHostedService
public class QueueWorker : BackgroundService
{
private readonly IConfiguration _configuration;
private readonly ILogger<QueueWorker> _logger;
Expand All @@ -26,7 +26,7 @@ public QueueWorker(ILogger<QueueWorker> logger, IConfiguration configuration)
_configuration = configuration;
}

public async Task StartAsync(CancellationToken cancellationToken)
protected async override Task ExecuteAsync(CancellationToken cancellationToken)
{
try
{
Expand All @@ -51,6 +51,10 @@ public async Task StartAsync(CancellationToken cancellationToken)
autoAck: true,
consumer: consumer);
}
catch (OperationCanceledException)
{
throw;
}
catch (Exception ex)
{
_logger.LogError(0, ex, "Failed to start listening to rabbit mq");
Expand All @@ -60,8 +64,7 @@ public async Task StartAsync(CancellationToken cancellationToken)

private async Task<IModel> ConnectAsync(CancellationToken cancellationToken)
{
ExceptionDispatchInfo? edi = null;
for (var i = 0; i < 5; i++)
while (true)
{
try
{
Expand All @@ -78,24 +81,13 @@ private async Task<IModel> ConnectAsync(CancellationToken cancellationToken)
}
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;
}
}
}

0 comments on commit 2b515c3

Please sign in to comment.