Skip to content

Commit

Permalink
fix: move PosixSignalRegistration to vostok host to save ref
Browse files Browse the repository at this point in the history
  • Loading branch information
m.tre committed Jun 17, 2024
1 parent 612b778 commit 54e1e21
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 24 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
29 changes: 29 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,29 @@ 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
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 Down
26 changes: 3 additions & 23 deletions Vostok.Hosting/VostokHost_Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Runtime.InteropServices;
using JetBrains.Annotations;
using Vostok.Hosting.Models;

Expand Down Expand Up @@ -49,27 +48,8 @@ public static VostokHost WithConsoleCancellation([NotNull] this VostokHost vosto
return vostokHost;
}

#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 static VostokHost WithSigtermCancellation([NotNull] this VostokHost vostokHost)
{
#if NET6_0_OR_GREATER
PosixSignalRegistration.Create(PosixSignal.SIGTERM, ctx =>
{
ctx.Cancel = true;
vostokHost.Stop(false);
});
#else
AppDomain.CurrentDomain.ProcessExit += (_, _) => vostokHost.Stop(false);
#endif
return vostokHost;
}
[Obsolete("Use VostokHost.RegisterSigtermCancellation instead of this")]
public static VostokHost WithSigtermCancellation([NotNull] this VostokHost vostokHost) =>
vostokHost.RegisterSigtermCancellation();
}
}

0 comments on commit 54e1e21

Please sign in to comment.