Additional logging around process stream reading

This commit is contained in:
Jordan Brown
2021-08-10 15:22:01 -04:00
parent 17bae4909e
commit eebf3792cd
2 changed files with 21 additions and 8 deletions
+12 -5
View File
@@ -134,24 +134,31 @@ namespace Tgstation.Server.Host.System
{
if (combinedStringBuilder == null)
throw new InvalidOperationException("Output/Error stream reading was not enabled!");
await Task.WhenAll(standardOutputTask, standardErrorTask).WithToken(cancellationToken).ConfigureAwait(false);
await Task.WhenAll(
GetStandardOutput(cancellationToken),
GetErrorOutput(cancellationToken))
.ConfigureAwait(false);
return combinedStringBuilder.ToString().TrimStart(Environment.NewLine.ToCharArray());
}
/// <inheritdoc />
public Task<string> GetErrorOutput(CancellationToken cancellationToken)
public async Task<string> GetErrorOutput(CancellationToken cancellationToken)
{
if (standardErrorTask == null)
throw new InvalidOperationException("Error stream reading was not enabled!");
return standardErrorTask.WithToken(cancellationToken);
if (!standardErrorTask.IsCompleted)
logger.LogTrace("Waiting for PID {0} to close error stream...", Id);
return await standardErrorTask.WithToken(cancellationToken).ConfigureAwait(false);
}
/// <inheritdoc />
public Task<string> GetStandardOutput(CancellationToken cancellationToken)
public async Task<string> GetStandardOutput(CancellationToken cancellationToken)
{
if (standardOutputTask == null)
throw new InvalidOperationException("Output stream reading was not enabled!");
return standardOutputTask.WithToken(cancellationToken);
if (!standardOutputTask.IsCompleted)
logger.LogTrace("Waiting for PID {0} to close output stream...", Id);
return await standardOutputTask.WithToken(cancellationToken).ConfigureAwait(false);
}
/// <inheritdoc />
@@ -176,13 +176,17 @@ namespace Tgstation.Server.Host.System
{
combinedStringBuilder = new StringBuilder();
async Task<string> ConsumeReader(Func<TextReader> readerFunc)
async Task<string> ConsumeReader(Func<TextReader> readerFunc, bool isOutputStream)
{
var stringBuilder = new StringBuilder();
string text;
await processStartTcs.Task.ConfigureAwait(false);
var pid = handle.Id;
var streamType = isOutputStream ? "out" : "err";
logger.LogTrace("Starting std{0} read for PID {1}...", streamType, pid);
var reader = readerFunc();
while ((text = await reader.ReadLineAsync().ConfigureAwait(false)) != null)
{
@@ -192,18 +196,20 @@ namespace Tgstation.Server.Host.System
stringBuilder.Append(text);
}
logger.LogTrace("Finished std{0} read for PID {1}", streamType, pid);
return stringBuilder.ToString();
}
if (readOutput)
{
outputTask = ConsumeReader(() => handle.StandardOutput);
outputTask = ConsumeReader(() => handle.StandardOutput, true);
handle.StartInfo.RedirectStandardOutput = true;
}
if (readError)
{
errorTask = ConsumeReader(() => handle.StandardError);
errorTask = ConsumeReader(() => handle.StandardError, false);
handle.StartInfo.RedirectStandardError = true;
}
}