Skip to content

Commit

Permalink
Merge branch 'INFRA-9782-graceful-shutdown-sigterm'
Browse files Browse the repository at this point in the history
  • Loading branch information
m.tre committed Jun 25, 2024
2 parents 48f2534 + 5d77d32 commit 548f97b
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 9 deletions.
1 change: 1 addition & 0 deletions Vostok.Hosting/PublicAPI/net6.0/PublicAPI.Shipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,7 @@ Vostok.Hosting.VostokHost
Vostok.Hosting.VostokHost.StartAsync(Vostok.Hosting.Models.VostokApplicationState? stateToAwait = 5) -> System.Threading.Tasks.Task
Vostok.Hosting.VostokHost.StopAsync(bool ensureSuccess = true) -> System.Threading.Tasks.Task<Vostok.Hosting.Models.VostokApplicationRunResult>
Vostok.Hosting.VostokHost.VostokHost(Vostok.Hosting.VostokHostSettings settings) -> void
Vostok.Hosting.VostokHost.RegisterSigtermCancellation() -> Vostok.Hosting.VostokHost
Vostok.Hosting.VostokHost_Extensions
Vostok.Hosting.VostokHostingEnvironmentFactory
Vostok.Hosting.VostokHostingEnvironmentFactorySettings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,7 @@ Vostok.Hosting.VostokHost
Vostok.Hosting.VostokHost.StartAsync(Vostok.Hosting.Models.VostokApplicationState? stateToAwait = 5) -> System.Threading.Tasks.Task
Vostok.Hosting.VostokHost.StopAsync(bool ensureSuccess = true) -> System.Threading.Tasks.Task<Vostok.Hosting.Models.VostokApplicationRunResult>
Vostok.Hosting.VostokHost.VostokHost(Vostok.Hosting.VostokHostSettings settings) -> void
Vostok.Hosting.VostokHost.RegisterSigtermCancellation() -> Vostok.Hosting.VostokHost
Vostok.Hosting.VostokHost_Extensions
Vostok.Hosting.VostokHostingEnvironmentFactory
Vostok.Hosting.VostokHostingEnvironmentFactorySettings
Expand Down Expand Up @@ -665,4 +666,4 @@ Vostok.Hosting.VostokHostShutdown
Vostok.Hosting.VostokHostShutdown.Initiate() -> void
Vostok.Hosting.VostokHostShutdown.IsInitiated.get -> bool
Vostok.Hosting.VostokHostShutdown.Source.get -> System.Threading.CancellationTokenSource
Vostok.Hosting.VostokHostShutdown.VostokHostShutdown(System.Threading.CancellationTokenSource source) -> void
Vostok.Hosting.VostokHostShutdown.VostokHostShutdown(System.Threading.CancellationTokenSource source) -> void
2 changes: 1 addition & 1 deletion Vostok.Hosting/Vostok.Hosting.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<Configurations>Debug;Release</Configurations>
</PropertyGroup>
<PropertyGroup>
<VersionPrefix>0.3.69</VersionPrefix>
<VersionPrefix>0.3.70</VersionPrefix>
</PropertyGroup>
<PropertyGroup>
<Title>Vostok.Hosting</Title>
Expand Down
37 changes: 37 additions & 0 deletions Vostok.Hosting/VostokHost.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System;
using System.Diagnostics;
#if NET6_0_OR_GREATER
using System.Runtime.InteropServices;
#endif
using System.Threading;
using System.Threading.Tasks;
using JetBrains.Annotations;
Expand Down Expand Up @@ -51,6 +54,9 @@ public class VostokHost
private volatile Task<VostokApplicationRunResult> workerTask;
private volatile VostokHostingEnvironment environment;
private volatile ILog log;
#if NET6_0_OR_GREATER
private volatile PosixSignalRegistration sigtermRegistration;
#endif

public VostokHost([NotNull] VostokHostSettings settings)
{
Expand Down Expand Up @@ -158,6 +164,34 @@ public Task<VostokApplicationRunResult> StopAsync(bool ensureSuccess = true)

return resultTask;
}

#if NET6_0_OR_GREATER
/// <summary>
/// Listen <see cref="PosixSignal.SIGTERM"/> and shutdown VostokHost if received.
/// </summary>
#else
/// <summary>
/// Listen <see cref="AppDomain.ProcessExit"/> and shutdown VostokHost if SIGTERM received.
/// </summary>
#endif
public VostokHost RegisterSigtermCancellation()
{
#if NET6_0_OR_GREATER
// Saving PosixSignalRegistration reference to nameof(VostokHost) field, because of IDisposable inheritance,
// and GC will collect if not assigned. On Dispose and finalize handler will unregister.
if (sigtermRegistration == null)
{
sigtermRegistration = PosixSignalRegistration.Create(PosixSignal.SIGTERM, ctx =>
{
ctx.Cancel = true;
this.Stop(false);
});
}
#else
AppDomain.CurrentDomain.ProcessExit += (_, _) => this.Stop(false);
#endif
return this;
}

private async Task<VostokApplicationRunResult> RunInternalAsync()
{
Expand All @@ -170,6 +204,9 @@ private async Task<VostokApplicationRunResult> RunInternalAsync()

var dynamicThreadPool = ConfigureDynamicThreadPool();

#if NET6_0_OR_GREATER
using (sigtermRegistration)
#endif
using (environment)
using (new ApplicationDisposable(settings.Application, environment, log))
using (dynamicThreadPool)
Expand Down
10 changes: 3 additions & 7 deletions Vostok.Hosting/VostokHost_Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,9 @@ public static VostokHost WithConsoleCancellation([NotNull] this VostokHost vosto
}

/// <summary>
/// Listen <see cref="AppDomain.ProcessExit"/> and shutdown VostokHost if SIGTERM received.
/// Makes same as <see cref="VostokHost.RegisterSigtermCancellation"/>
/// </summary>
public static VostokHost WithSigtermCancellation([NotNull] this VostokHost vostokHost)
{
AppDomain.CurrentDomain.ProcessExit += (_, _) => vostokHost.Stop(false);

return vostokHost;
}
public static VostokHost WithSigtermCancellation([NotNull] this VostokHost vostokHost) =>
vostokHost.RegisterSigtermCancellation();
}
}

0 comments on commit 548f97b

Please sign in to comment.