Skip to content

Commit

Permalink
Merge pull request #114 from fiskaltrust/97-improve-logging-on-plebia…
Browse files Browse the repository at this point in the history
…n-startup-in-case-of-a-crash

Added functionality to capture logs from Plebian and log them in Monarch
  • Loading branch information
volllly authored Oct 25, 2023
2 parents d0eae7e + cfd28da commit 93a7773
Show file tree
Hide file tree
Showing 10 changed files with 237 additions and 133 deletions.
28 changes: 15 additions & 13 deletions src/fiskaltrust.Launcher/Commands/DoctorCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public async Task<int> InvokeAsync(InvocationContext context)

await CheckAwait("Start monarch WebApplication", async () => await WithTimeout(async () => await monarchApp.StartAsync(_lifetime.ApplicationLifetime.ApplicationStopping), TimeSpan.FromSeconds(5)), throws: true);

var plebianConfiguration = new PlebianConfiguration
var plebeianConfiguration = new PlebeianConfiguration
{
PackageId = doctorId,
PackageType = Constants.PackageType.Queue
Expand All @@ -167,24 +167,24 @@ public async Task<int> InvokeAsync(InvocationContext context)
var packageConfiguration = new PackageConfiguration
{
Configuration = new(),
Id = plebianConfiguration.PackageId,
Id = plebeianConfiguration.PackageId,
Package = "none",
Url = Array.Empty<string>(),
Version = "1.0.0"
};

IProcessHostService? processHostService = Check("Start plebian processhostservice client", () => GrpcChannel.ForAddress($"http://localhost:{launcherConfiguration.LauncherPort}").CreateGrpcService<IProcessHostService>());
IProcessHostService? processHostService = Check("Start plebeian processhostservice client", () => GrpcChannel.ForAddress($"http://localhost:{launcherConfiguration.LauncherPort}").CreateGrpcService<IProcessHostService>());

var plebianBuilder = Host.CreateDefaultBuilder()
var plebeianBuilder = Host.CreateDefaultBuilder()
.UseSerilog(new LoggerConfiguration().CreateLogger())
.ConfigureServices(services =>
{
Check("Setup plebian services", () =>
Check("Setup plebeian services", () =>
{
services.Configure<HostOptions>(opts => opts.ShutdownTimeout = TimeSpan.FromSeconds(30));
services.AddSingleton(_ => launcherConfiguration);
services.AddSingleton(_ => packageConfiguration);
services.AddSingleton(_ => plebianConfiguration);
services.AddSingleton(_ => plebeianConfiguration);

var pluginLoader = new PluginLoader();
services.AddSingleton(_ => pluginLoader);
Expand All @@ -195,7 +195,7 @@ public async Task<int> InvokeAsync(InvocationContext context)
}

services.AddSingleton<HostingService>();
services.AddHostedService<ProcessHostPlebian>();
services.AddHostedService<ProcessHostPlebeian>();

services.AddSingleton<IClientFactory<IDESSCD>, DESSCDClientFactory>();
services.AddSingleton<IClientFactory<IPOS>, POSClientFactory>();
Expand All @@ -212,9 +212,9 @@ public async Task<int> InvokeAsync(InvocationContext context)
}, throws: true);
});

var plebianApp = Check("Build plebian Host", plebianBuilder.Build, throws: true)!;
var plebeianApp = Check("Build plebeian Host", plebeianBuilder.Build, throws: true)!;

await CheckAwait("Start plebian Host", async () => await WithTimeout(async () => await plebianApp.StartAsync(_lifetime.ApplicationLifetime.ApplicationStopping), TimeSpan.FromSeconds(5)));
await CheckAwait("Start plebeian Host", async () => await WithTimeout(async () => await plebeianApp.StartAsync(_lifetime.ApplicationLifetime.ApplicationStopping), TimeSpan.FromSeconds(5)));

await doctorProcessHostMonarch.IsStarted.Task;

Expand All @@ -225,8 +225,8 @@ await CheckAwait("Shutdown launcher gracefully", async () => await WithTimeout(a
await monarchApp.StopAsync();
await monarchApp.WaitForShutdownAsync();

await plebianApp.StopAsync();
await plebianApp.WaitForShutdownAsync();
await plebeianApp.StopAsync();
await plebeianApp.WaitForShutdownAsync();
}, TimeSpan.FromSeconds(5))
);
}
Expand Down Expand Up @@ -353,15 +353,17 @@ public Task Start(CancellationToken cancellationToken)
return Task.CompletedTask;
}

public void Started()
public void SetPlebeanStarted()
{
IsStarted.SetResult();
}

public Task Stopped()
public Task GetStopped()
{
return Task.CompletedTask;
}

public void SetStartupCompleted() { }
}

public class DoctorMiddlewareBootstrapper : IMiddlewareBootstrapper
Expand Down
22 changes: 13 additions & 9 deletions src/fiskaltrust.Launcher/Commands/HostCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class HostCommand : Command
{
public HostCommand() : base("host")
{
AddOption(new Option<string>("--plebian-configuration"));
AddOption(new Option<string>("--plebeian-configuration"));
AddOption(new Option<string>("--debugging"));
AddOption(new Option<string>("--launcher-configuration"));
AddOption(new Option<bool>("--no-process-host-service", getDefaultValue: () => false));
Expand All @@ -38,7 +38,7 @@ public HostCommand() : base("host")
public class HostCommandHandler : ICommandHandler
{
public string LauncherConfiguration { get; set; } = null!;
public string PlebianConfiguration { get; set; } = null!;
public string PlebeianConfiguration { get; set; } = null!;
public bool NoProcessHostService { get; set; }
public bool Debugging { get; set; }

Expand All @@ -63,19 +63,19 @@ public async Task<int> InvokeAsync(InvocationContext context)

var launcherConfiguration = Common.Configuration.LauncherConfiguration.Deserialize(System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(LauncherConfiguration)));

var plebianConfiguration = Configuration.PlebianConfiguration.Deserialize(System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(PlebianConfiguration)));
var plebeianConfiguration = Configuration.PlebeianConfiguration.Deserialize(System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(PlebeianConfiguration)));

var cashboxConfiguration = CashBoxConfigurationExt.Deserialize(await File.ReadAllTextAsync(launcherConfiguration.CashboxConfigurationFile!));

cashboxConfiguration.Decrypt(launcherConfiguration, await CommonCommandHandler.LoadCurve(launcherConfiguration.AccessToken!, launcherConfiguration.UseLegacyDataProtection!.Value));

var packageConfiguration = (plebianConfiguration.PackageType switch
var packageConfiguration = (plebeianConfiguration.PackageType switch
{
PackageType.Queue => cashboxConfiguration.ftQueues,
PackageType.SCU => cashboxConfiguration.ftSignaturCreationDevices,
PackageType.Helper => cashboxConfiguration.helpers,
var unknown => throw new Exception($"Unknown PackageType {unknown}")
}).First(p => p.Id == plebianConfiguration.PackageId);
}).First(p => p.Id == plebeianConfiguration.PackageId);

packageConfiguration.Configuration = ProcessPackageConfiguration(packageConfiguration.Configuration, launcherConfiguration, cashboxConfiguration);

Expand All @@ -96,10 +96,14 @@ public async Task<int> InvokeAsync(InvocationContext context)
.UseSerilog()
.ConfigureServices(services =>
{
services.Configure<HostOptions>(opts => opts.ShutdownTimeout = TimeSpan.FromSeconds(30));
services.Configure<HostOptions>(opts =>
{
opts.ShutdownTimeout = TimeSpan.FromSeconds(30);
opts.BackgroundServiceExceptionBehavior = BackgroundServiceExceptionBehavior.StopHost;
});
services.AddSingleton(_ => launcherConfiguration);
services.AddSingleton(_ => packageConfiguration);
services.AddSingleton(_ => plebianConfiguration);
services.AddSingleton(_ => plebeianConfiguration);

var pluginLoader = new PluginLoader();
services.AddSingleton(_ => pluginLoader);
Expand All @@ -110,7 +114,7 @@ public async Task<int> InvokeAsync(InvocationContext context)
}

services.AddSingleton<HostingService>();
services.AddHostedService<ProcessHostPlebian>();
services.AddHostedService<ProcessHostPlebeian>();

services.AddSingleton<IClientFactory<IDESSCD>, DESSCDClientFactory>();
services.AddSingleton<IClientFactory<IITSSCD>, ITSSCDClientFactory>();
Expand Down Expand Up @@ -162,7 +166,7 @@ public async Task<int> InvokeAsync(InvocationContext context)
catch (Exception e)
{
Log.Error(e, "An unhandled exception occured.");
return 1;
throw;
}
finally
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@

namespace fiskaltrust.Launcher.Configuration
{
public record PlebianConfiguration
public record PlebeianConfiguration
{
public PackageType PackageType { get; set; }
public Guid PackageId { get; set; }

public static PlebianConfiguration Deserialize(string text) => JsonSerializer.Deserialize(text, typeof(PlebianConfiguration), Helpers.Serialization.SerializerContext.Default) as PlebianConfiguration ?? throw new Exception($"Could not deserialize {nameof(PlebianConfiguration)}");
public static PlebeianConfiguration Deserialize(string text) => JsonSerializer.Deserialize(text, typeof(PlebeianConfiguration), Helpers.Serialization.SerializerContext.Default) as PlebeianConfiguration ?? throw new Exception($"Could not deserialize {nameof(PlebeianConfiguration)}");

public string Serialize() => JsonSerializer.Serialize(this, typeof(PlebianConfiguration), Helpers.Serialization.SerializerContext.Default);
public string Serialize() => JsonSerializer.Serialize(this, typeof(PlebeianConfiguration), Helpers.Serialization.SerializerContext.Default);
}
}
2 changes: 1 addition & 1 deletion src/fiskaltrust.Launcher/Helpers/SerializerContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@

namespace fiskaltrust.Launcher.Helpers.Serialization
{
[JsonSerializable(typeof(PlebianConfiguration))]
[JsonSerializable(typeof(PlebeianConfiguration))]
public partial class SerializerContext : JsonSerializerContext { }
}
10 changes: 8 additions & 2 deletions src/fiskaltrust.Launcher/ProcessHost/ProcessHostMonarcStartup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,23 @@ protected override async Task ExecuteAsync(CancellationToken cancellationToken)
await new TaskCompletionSource().Task;
}

foreach (var host in _hosts)
{
host.Value.SetStartupCompleted();
}

try
{
await Task.WhenAll(_hosts.Select(h => h.Value.Stopped()));
await Task.WhenAll(_hosts.Select(h => h.Value.GetStopped()));
}
catch
{
foreach (var failed in _hosts.Where(h => !h.Value.Stopped().IsCompletedSuccessfully).Select(h => (h.Key, h.Value.Stopped().Exception)))
foreach (var failed in _hosts.Where(h => !h.Value.GetStopped().IsCompletedSuccessfully).Select(h => (h.Key, h.Value.GetStopped().Exception)))
{
_logger.LogWarning(failed.Exception, "ProcessHost {Id} had crashed.", failed.Key);
}
}
_lifetime.ApplicationLifetime.StopApplication();
}

private async Task StartProcessHostMonarch(PackageConfiguration configuration, PackageType packageType, CancellationToken cancellationToken)
Expand Down
Loading

0 comments on commit 93a7773

Please sign in to comment.