Skip to content

Commit

Permalink
add Systemd Lifetime
Browse files Browse the repository at this point in the history
  • Loading branch information
forsthug committed Jan 12, 2024
1 parent 14bf566 commit 8b650f8
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 11 deletions.
9 changes: 0 additions & 9 deletions src/fiskaltrust.Launcher/Commands/RunCommand.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.CommandLine;
using System.CommandLine.Invocation;
using fiskaltrust.Launcher.ProcessHost;
using fiskaltrust.Launcher.Services;
using Serilog;
Expand All @@ -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
Expand Down Expand Up @@ -66,9 +59,7 @@ public static class RunHandler
{
public static async Task<int> HandleAsync(CommonOptions commonOptions, CommonProperties commonProperties, RunOptions _, RunServices runServices)
{
Log.Debug("RunHandler");
var builder = WebApplication.CreateBuilder();
Log.Debug("WebApplication.CreateBuilde");

builder.Host
.UseSerilog()
Expand Down
50 changes: 48 additions & 2 deletions src/fiskaltrust.Launcher/Extensions/LifetimeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,21 @@ public static IHostBuilder UseCustomHostLifetime(this IHostBuilder builder)
else if (SystemdHelpers.IsSystemdService())
{
builder.UseSystemd();
builder.ConfigureServices(services => services.AddSingleton<ILifetime, Lifetime>());
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<ILifetime, CustomSystemDServiceLifetime>();
services.AddSingleton<IHostLifetime>(sp => sp.GetRequiredService<ILifetime>());
#pragma warning restore CA1416
});
}
else
{
Expand Down Expand Up @@ -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) { }
}
}
}

0 comments on commit 8b650f8

Please sign in to comment.