diff --git a/src/fiskaltrust.Launcher/Commands/RunCommand.cs b/src/fiskaltrust.Launcher/Commands/RunCommand.cs index 700344e8..f21385f6 100644 --- a/src/fiskaltrust.Launcher/Commands/RunCommand.cs +++ b/src/fiskaltrust.Launcher/Commands/RunCommand.cs @@ -1,5 +1,4 @@ using System.CommandLine; -using System.CommandLine.Invocation; using fiskaltrust.Launcher.ProcessHost; using fiskaltrust.Launcher.Services; using Serilog; @@ -8,12 +7,6 @@ using fiskaltrust.Launcher.Extensions; using fiskaltrust.Launcher.Helpers; using Microsoft.AspNetCore.Server.Kestrel.Core; -using fiskaltrust.Launcher.Common.Configuration; -using fiskaltrust.storage.serialization.V0; -using System.Security.Cryptography; -using Microsoft.AspNetCore.DataProtection; -using Microsoft.AspNetCore.Hosting.Server; -using Microsoft.AspNetCore.Hosting.Server.Features; namespace fiskaltrust.Launcher.Commands @@ -66,9 +59,7 @@ public static class RunHandler { public static async Task HandleAsync(CommonOptions commonOptions, CommonProperties commonProperties, RunOptions _, RunServices runServices) { - Log.Debug("RunHandler"); var builder = WebApplication.CreateBuilder(); - Log.Debug("WebApplication.CreateBuilde"); builder.Host .UseSerilog() diff --git a/src/fiskaltrust.Launcher/Extensions/LifetimeExtensions.cs b/src/fiskaltrust.Launcher/Extensions/LifetimeExtensions.cs index 3d39a619..071706ff 100644 --- a/src/fiskaltrust.Launcher/Extensions/LifetimeExtensions.cs +++ b/src/fiskaltrust.Launcher/Extensions/LifetimeExtensions.cs @@ -32,8 +32,21 @@ public static IHostBuilder UseCustomHostLifetime(this IHostBuilder builder) else if (SystemdHelpers.IsSystemdService()) { builder.UseSystemd(); - builder.ConfigureServices(services => services.AddSingleton()); - return builder; + + return builder.ConfigureServices(services => + { + var lifetime = services.FirstOrDefault(s => s.ImplementationType == typeof(SystemdLifetime)); + + if (lifetime != null) + { + services.Remove(lifetime); + } + +#pragma warning disable CA1416 + services.AddSingleton(); + services.AddSingleton(sp => sp.GetRequiredService()); +#pragma warning restore CA1416 + }); } else { @@ -140,4 +153,37 @@ protected override void Dispose(bool disposing) base.Dispose(disposing); } } + [SupportedOSPlatform("linux")] + public class CustomSystemDServiceLifetime : SystemdLifetime, ILifetime + { + private readonly CancellationTokenSource _starting = new(); + private readonly ManualResetEventSlim _started = new(); + + public IHostApplicationLifetime ApplicationLifetime { get; init; } + + public CustomSystemDServiceLifetime( + IHostEnvironment environment, + IHostApplicationLifetime applicationLifetime, + ILoggerFactory loggerFactory, + ISystemdNotifier systemdNotifier) + : base(environment, applicationLifetime, systemdNotifier, loggerFactory) + { + ApplicationLifetime = applicationLifetime; + } + + public void ServiceStartupCompleted() + { + ApplicationLifetime.ApplicationStarted.Register(() => _started.Set()); + } + + public new async Task WaitForStartAsync(CancellationToken cancellationToken) + { + try + { + using var cts = CancellationTokenSource.CreateLinkedTokenSource(_starting.Token, cancellationToken); + await base.WaitForStartAsync(cts.Token); + } + catch (OperationCanceledException) when (_starting.IsCancellationRequested) { } + } + } } \ No newline at end of file