Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added functionality to capture logs from Plebian and log them in Monarch #114

Merged
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 59 additions & 17 deletions src/fiskaltrust.Launcher/ProcessHost/ProcessHostMonarch.cs
volllly marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using fiskaltrust.Launcher.Constants;
using fiskaltrust.Launcher.Helpers;
using fiskaltrust.storage.serialization.V0;
using Microsoft.Extensions.Logging;

namespace fiskaltrust.Launcher.ProcessHost
{
Expand All @@ -23,32 +24,53 @@ public class ProcessHostMonarch : IProcessHostMonarch
private readonly TaskCompletionSource _stopped;
private readonly PackageConfiguration _packageConfiguration;
private readonly ILogger<ProcessHostMonarch> _logger;
private readonly List<string> _plebianLogBuffer = new List<string>();

public ProcessHostMonarch(ILogger<ProcessHostMonarch> logger, LauncherConfiguration launcherConfiguration, PackageConfiguration packageConfiguration, PackageType packageType, LauncherExecutablePath launcherExecutablePath)
{
_packageConfiguration = packageConfiguration;
_logger = logger;

_process = new Process();
_process.StartInfo.UseShellExecute = false;
_process.StartInfo.FileName = launcherExecutablePath.Path;
_process.StartInfo.CreateNoWindow = false;

_process.StartInfo.Arguments = string.Join(" ", new string[] {
"host",
"--plebian-configuration", $"\"{Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(new PlebianConfiguration { PackageType = packageType, PackageId = packageConfiguration.Id }.Serialize()))}\"",
"--launcher-configuration", $"\"{Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(launcherConfiguration.Serialize()))}\"",
});

_process = new Process
{
StartInfo = new ProcessStartInfo
{
UseShellExecute = false,
FileName = launcherExecutablePath.Path,
CreateNoWindow = false,
Arguments = string.Join(" ", new string[] {
"host",
"--plebian-configuration", $"\"{Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(new PlebianConfiguration { PackageType = packageType, PackageId = packageConfiguration.Id }.Serialize()))}\"",
"--launcher-configuration", $"\"{Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(launcherConfiguration.Serialize()))}\"",
}),
RedirectStandardInput = true,
RedirectStandardError = true,
RedirectStandardOutput = true
},
EnableRaisingEvents = true
};

// if (Debugger.IsAttached)
// {
// _process.StartInfo.Arguments += " --debugging";
// }
_process.StartInfo.RedirectStandardInput = true;
_process.StartInfo.RedirectStandardError = true;
_process.StartInfo.RedirectStandardOutput = true;

_process.EnableRaisingEvents = true;
_process.OutputDataReceived += (sender, e) =>
{
if (e.Data != null)
{
_plebianLogBuffer.Add($"Plebian Output: {e.Data}");
pawelvds marked this conversation as resolved.
Show resolved Hide resolved
}
};

_process.ErrorDataReceived += (sender, e) =>
{
if (e.Data != null)
{
_plebianLogBuffer.Add($"Plebian Error: {e.Data}");
}
};

_stopped = new TaskCompletionSource();
_started = new TaskCompletionSource();
}
Expand All @@ -72,6 +94,23 @@ public Task Start(CancellationToken cancellationToken)
{
_logger.LogInformation("Host {package} {id} has shutdown.", _packageConfiguration.Package, _packageConfiguration.Id);

if (_process.ExitCode != 0)
{
foreach (var log in _plebianLogBuffer)
{
if (log.Contains("Error"))
{
_logger.LogError(log);
}
else
{
_logger.LogInformation(log);
}
}
pawelvds marked this conversation as resolved.
Show resolved Hide resolved
}

_plebianLogBuffer.Clear();
pawelvds marked this conversation as resolved.
Show resolved Hide resolved

await Task.Delay(1000);
if (!cancellationToken.IsCancellationRequested)
{
Expand All @@ -87,11 +126,14 @@ public Task Start(CancellationToken cancellationToken)
_process.BeginOutputReadLine();
_process.BeginErrorReadLine();
}
catch { }
catch
{
_logger.LogError("Error while initiating the output and error read lines.");
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Could not start ProcessHost process for {package} {id}.", _packageConfiguration.Package, _packageConfiguration.Id);
_logger.LogError(ex, "Could not restart ProcessHost process for {package} {id}.", _packageConfiguration.Package, _packageConfiguration.Id);
_started.TrySetResult();
_stopped.TrySetCanceled(cancellationToken);
}
Expand Down
Loading