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

Hostedservice logging #1996

Merged
merged 2 commits into from
May 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 6 additions & 3 deletions src/Proto.Cluster/Cluster.cs
Original file line number Diff line number Diff line change
Expand Up @@ -287,14 +287,17 @@ public async Task ShutdownAsync(bool graceful = true, string reason = "")

// Inform all members of the cluster that this node intends to leave. Also, let the MemberList know that this
// node was the one that initiated the shutdown to prevent another shutdown from being called.
Logger.LogInformation("Setting GracefullyLeft gossip state for {Id}", System.Id);
MemberList.Stopping = true;
await Gossip.SetStateAsync(GossipKeys.GracefullyLeft, new Empty()).ConfigureAwait(false);

// Deregister from configured cluster provider.
await Provider.ShutdownAsync(graceful).ConfigureAwait(false);

Logger.LogInformation("Waiting for two gossip intervals to pass for {Id}", System.Id);
// In case provider shutdown is quick, let's wait at least 2 gossip intervals.
await Task.Delay((int)Config.GossipInterval.TotalMilliseconds * 2).ConfigureAwait(false);

Logger.LogInformation("Stopping cluster provider for {Id}", System.Id);
// Deregister from configured cluster provider.
await Provider.ShutdownAsync(graceful).ConfigureAwait(false);

if (_clusterKindObserver != null)
{
Expand Down
9 changes: 9 additions & 0 deletions src/Proto.Cluster/ProtoActorLifecycleHost.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace Proto.Cluster;

public class ProtoActorLifecycleHost : IHostedService
{
private readonly ActorSystem _actorSystem;
private readonly ILogger<ProtoActorLifecycleHost> _logger;
private readonly bool _runAsClient;
private readonly IHostApplicationLifetime _lifetime;
private bool _shutdownViaActorSystem;

public ProtoActorLifecycleHost(
ActorSystem actorSystem,
ILogger<ProtoActorLifecycleHost> logger,
IHostApplicationLifetime lifetime,
bool runAsClient
)
{
_actorSystem = actorSystem;
_logger = logger;
_runAsClient = runAsClient;
_lifetime = lifetime;
}
Expand All @@ -35,16 +39,19 @@ public async Task StartAsync(CancellationToken _)

if (_actorSystem.Cluster().Config.ExitOnShutdown)
{
_logger.LogWarning("[ProtoActorLifecycleHost]{SystemId} Exit on shutdown is enabled, shutting down host process", _actorSystem.Id);
_lifetime.StopApplication();
}
});

if (_runAsClient)
{
_logger.LogInformation("[ProtoActorLifecycleHost]{SystemId} Starting Proto.Actor cluster client", _actorSystem.Id);
await _actorSystem.Cluster().StartClientAsync().ConfigureAwait(false);
}
else
{
_logger.LogInformation("[ProtoActorLifecycleHost]{SystemId} Starting Proto.Actor cluster client", _actorSystem.Id);
await _actorSystem.Cluster().StartMemberAsync().ConfigureAwait(false);
}
}
Expand All @@ -53,10 +60,12 @@ public async Task StopAsync(CancellationToken cancellationToken)
{
if (_shutdownViaActorSystem)
{
_logger.LogInformation("[ProtoActorLifecycleHost]{SystemId} Stopping Proto.Actor cluster via actor system", _actorSystem.Id);
await _actorSystem.Cluster().ShutdownCompleted.ConfigureAwait(false);
}
else
{
_logger.LogInformation("[ProtoActorLifecycleHost]{SystemId} Stopping Proto.Actor cluster via host application lifetime (SIGTERM)", _actorSystem.Id);
await _actorSystem.Cluster().ShutdownAsync(true, "Host process is stopping").ConfigureAwait(false);
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/Proto.Cluster/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public static IServiceCollection AddProtoCluster(this IServiceCollection self, A
self.AddHostedService(p =>
new ProtoActorLifecycleHost(
p.GetRequiredService<ActorSystem>(),
p.GetRequiredService<ILogger<ProtoActorLifecycleHost>>(),
p.GetRequiredService<IHostApplicationLifetime>(),
boot.RunAsClient));

Expand Down Expand Up @@ -108,6 +109,7 @@ public static IServiceCollection AddProtoCluster(this IServiceCollection self, s
self.AddHostedService(p =>
new ProtoActorLifecycleHost(
p.GetRequiredService<ActorSystem>(),
p.GetRequiredService<ILogger<ProtoActorLifecycleHost>>(),
p.GetRequiredService<IHostApplicationLifetime>(),
runAsClient));

Expand Down