From fdd43818d15b0c9274132dd9680dec8e0485859f Mon Sep 17 00:00:00 2001 From: Roger Johansson Date: Fri, 12 May 2023 09:12:40 +0200 Subject: [PATCH 1/2] add logging to hosted service --- src/Proto.Cluster/ProtoActorLifecycleHost.cs | 9 +++++++++ src/Proto.Cluster/ServiceCollectionExtensions.cs | 2 ++ 2 files changed, 11 insertions(+) diff --git a/src/Proto.Cluster/ProtoActorLifecycleHost.cs b/src/Proto.Cluster/ProtoActorLifecycleHost.cs index ca7bae1a9d..ec09aed143 100644 --- a/src/Proto.Cluster/ProtoActorLifecycleHost.cs +++ b/src/Proto.Cluster/ProtoActorLifecycleHost.cs @@ -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 _logger; private readonly bool _runAsClient; private readonly IHostApplicationLifetime _lifetime; private bool _shutdownViaActorSystem; public ProtoActorLifecycleHost( ActorSystem actorSystem, + ILogger logger, IHostApplicationLifetime lifetime, bool runAsClient ) { _actorSystem = actorSystem; + _logger = logger; _runAsClient = runAsClient; _lifetime = lifetime; } @@ -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); } } @@ -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); } } diff --git a/src/Proto.Cluster/ServiceCollectionExtensions.cs b/src/Proto.Cluster/ServiceCollectionExtensions.cs index 8e1c2dff6b..a9b359aeed 100644 --- a/src/Proto.Cluster/ServiceCollectionExtensions.cs +++ b/src/Proto.Cluster/ServiceCollectionExtensions.cs @@ -63,6 +63,7 @@ public static IServiceCollection AddProtoCluster(this IServiceCollection self, A self.AddHostedService(p => new ProtoActorLifecycleHost( p.GetRequiredService(), + p.GetRequiredService>(), p.GetRequiredService(), boot.RunAsClient)); @@ -108,6 +109,7 @@ public static IServiceCollection AddProtoCluster(this IServiceCollection self, s self.AddHostedService(p => new ProtoActorLifecycleHost( p.GetRequiredService(), + p.GetRequiredService>(), p.GetRequiredService(), runAsClient)); From baf8632cd7ff6b7e5857c32e1b47e700684e9442 Mon Sep 17 00:00:00 2001 From: Roger Johansson Date: Fri, 12 May 2023 09:22:28 +0200 Subject: [PATCH 2/2] more shutdown logging --- src/Proto.Cluster/Cluster.cs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/Proto.Cluster/Cluster.cs b/src/Proto.Cluster/Cluster.cs index 5151bc236c..2a5f7140e2 100644 --- a/src/Proto.Cluster/Cluster.cs +++ b/src/Proto.Cluster/Cluster.cs @@ -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) {