Browse Source

DockerRunner: include stderr in log on failure. (#1059)

philliphoff-arm64
Tom Deseyn 5 years ago
committed by GitHub
parent
commit
b1aaca8c5d
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 21
      src/Microsoft.Tye.Core/ProcessUtil.cs
  2. 5
      src/Microsoft.Tye.Hosting/DockerRunner.cs

21
src/Microsoft.Tye.Core/ProcessUtil.cs

@ -114,9 +114,12 @@ namespace Microsoft.Tye
process.Exited += (_, e) => process.Exited += (_, e) =>
{ {
// Even though the Exited event has been raised, WaitForExit() must still be called to ensure the output buffers lock (process)
// have been flushed before the process is considered completely done. {
process.WaitForExit(); // Even though the Exited event has been raised, WaitForExit() must still be called to ensure the output buffers
// have been flushed before the process is considered completely done.
process.WaitForExit();
}
if (throwOnError && process.ExitCode != 0) if (throwOnError && process.ExitCode != 0)
{ {
@ -128,11 +131,15 @@ namespace Microsoft.Tye
} }
}; };
process.Start(); // lock ensures we're reading output when WaitForExit is called in process.Exited event.
onStart?.Invoke(process.Id); lock (process)
{
process.Start();
onStart?.Invoke(process.Id);
process.BeginOutputReadLine(); process.BeginOutputReadLine();
process.BeginErrorReadLine(); process.BeginErrorReadLine();
}
var cancelledTcs = new TaskCompletionSource<object?>(); var cancelledTcs = new TaskCompletionSource<object?>();
await using var _ = cancellationToken.Register(() => cancelledTcs.TrySetResult(null)); await using var _ = cancellationToken.Register(() => cancelledTcs.TrySetResult(null));

5
src/Microsoft.Tye.Hosting/DockerRunner.cs

@ -311,16 +311,17 @@ namespace Microsoft.Tye.Hosting
status.DockerNetwork = dockerNetwork; status.DockerNetwork = dockerNetwork;
WriteReplicaToStore(replica); WriteReplicaToStore(replica);
var stderr = new StringBuilder();
var result = await application.ContainerEngine.RunAsync( var result = await application.ContainerEngine.RunAsync(
command, command,
throwOnError: false, throwOnError: false,
cancellationToken: cancellationToken, cancellationToken: cancellationToken,
outputDataReceived: data => service.Logs.OnNext($"[{replica}]: {data}"), outputDataReceived: data => service.Logs.OnNext($"[{replica}]: {data}"),
errorDataReceived: data => service.Logs.OnNext($"[{replica}]: {data}")); errorDataReceived: data => { service.Logs.OnNext($"[{replica}]: {data}"); stderr.AppendLine(data); });
if (result.ExitCode != 0) if (result.ExitCode != 0)
{ {
_logger.LogError("docker run failed for {ServiceName} with exit code {ExitCode}:" + result.StandardError, service.Description.Name, result.ExitCode); _logger.LogError("docker run failed for {ServiceName} with exit code {ExitCode}: " + stderr, service.Description.Name, result.ExitCode);
service.Replicas.TryRemove(replica, out var _); service.Replicas.TryRemove(replica, out var _);
service.ReplicaEvents.OnNext(new ReplicaEvent(ReplicaState.Removed, status)); service.ReplicaEvents.OnNext(new ReplicaEvent(ReplicaState.Removed, status));

Loading…
Cancel
Save