Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure AkkaHostedService failures are always visible #494

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions src/Akka.Hosting.Tests/StartFailureSpec.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System;
using System.Threading.Tasks;
using Akka.Actor;
using FluentAssertions;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Xunit;
using Xunit.Abstractions;
using static FluentAssertions.FluentActions;

namespace Akka.Hosting.Tests;

public class StartFailureSpec
{
private readonly ITestOutputHelper _output;

public StartFailureSpec(ITestOutputHelper output)
{
_output = output;
}

[Fact]
public async Task ShouldThrowWhenActorSystemFailedToStart()
{
// arrange
var host = new HostBuilder()
.ConfigureLogging(builder =>
{
builder.ClearProviders();
builder.AddProvider(new XUnitLoggerProvider(_output, LogLevel.Debug));
})
.ConfigureServices(services =>
{
services.AddAkka("MySys", (builder, provider) =>
{
builder.AddStartup((_, _) => throw new TestException("BOOM"));
});
})
.Build();

await Awaiting(async () => await host.StartAsync()).Should()
.ThrowExactlyAsync<TestException>().WithMessage("BOOM");
}

private class TestException: Exception
{
public TestException(string? message) : base(message)
{
}
}
}
10 changes: 8 additions & 2 deletions src/Akka.Hosting/AkkaHostedService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Runtime.ExceptionServices;
using System.Threading;
using System.Threading.Tasks;
using Akka.Actor;
Expand Down Expand Up @@ -74,8 +75,13 @@ async Task TerminationHook()
Logger.Log(LogLevel.Critical, ex, "Unable to start AkkaHostedService - shutting down application");

// resolve https://github.com/akkadotnet/Akka.Hosting/issues/470 - never allow failures to be silent
Console.WriteLine($"Unable to start AkkaHostedService due to [{ex}] - shutting down application");
HostApplicationLifetime?.StopApplication();
Console.WriteLine($"Unable to start AkkaHostedService - shutting down application.\nCause: {ex}");

// Best effort to perform a clean stop
var capturedException = ExceptionDispatchInfo.Capture(ex);
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(3));
await StopAsync(cts.Token);
capturedException.Throw();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

}
}

Expand Down
Loading