Implemented daemon side stats streaming. Fixed server task cancellation being too quick. Improved console streaming
This commit is contained in:
@@ -48,65 +48,91 @@ public class ConsoleSubSystem : ServerSubSystem
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public async Task Attach(string containerId)
|
||||
public Task Attach(string containerId)
|
||||
{
|
||||
Stream = await DockerClient.Containers.AttachContainerAsync(containerId,
|
||||
true,
|
||||
new ContainerAttachParameters()
|
||||
{
|
||||
Stderr = true,
|
||||
Stdin = true,
|
||||
Stdout = true,
|
||||
Stream = true
|
||||
},
|
||||
Server.TaskCancellation
|
||||
);
|
||||
|
||||
// Reading
|
||||
Task.Run(async () =>
|
||||
{
|
||||
// This loop is here to reconnect to the container if for some reason the container
|
||||
// attach stream fails before the server tasks have been canceled i.e. the before the server
|
||||
// goes offline
|
||||
|
||||
while (!Server.TaskCancellation.IsCancellationRequested)
|
||||
{
|
||||
var buffer = new byte[1024];
|
||||
|
||||
try
|
||||
{
|
||||
var readResult = await Stream.ReadOutputAsync(
|
||||
buffer,
|
||||
0,
|
||||
buffer.Length,
|
||||
Stream = await DockerClient.Containers.AttachContainerAsync(containerId,
|
||||
true,
|
||||
new ContainerAttachParameters()
|
||||
{
|
||||
Stderr = true,
|
||||
Stdin = true,
|
||||
Stdout = true,
|
||||
Stream = true
|
||||
},
|
||||
Server.TaskCancellation
|
||||
);
|
||||
|
||||
if (readResult.EOF)
|
||||
break;
|
||||
var buffer = new byte[1024];
|
||||
|
||||
var resizedBuffer = new byte[readResult.Count];
|
||||
Array.Copy(buffer, resizedBuffer, readResult.Count);
|
||||
buffer = new byte[buffer.Length];
|
||||
try
|
||||
{
|
||||
// Read while server tasks are not canceled
|
||||
while (!Server.TaskCancellation.IsCancellationRequested)
|
||||
{
|
||||
var readResult = await Stream.ReadOutputAsync(
|
||||
buffer,
|
||||
0,
|
||||
buffer.Length,
|
||||
Server.TaskCancellation
|
||||
);
|
||||
|
||||
var decodedText = Encoding.UTF8.GetString(resizedBuffer);
|
||||
await WriteOutput(decodedText);
|
||||
if (readResult.EOF)
|
||||
break;
|
||||
|
||||
var resizedBuffer = new byte[readResult.Count];
|
||||
Array.Copy(buffer, resizedBuffer, readResult.Count);
|
||||
buffer = new byte[buffer.Length];
|
||||
|
||||
var decodedText = Encoding.UTF8.GetString(resizedBuffer);
|
||||
await WriteOutput(decodedText);
|
||||
}
|
||||
}
|
||||
catch (TaskCanceledException)
|
||||
{
|
||||
// Ignored
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
// Ignored
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Logger.LogWarning("An unhandled error occured while reading from container stream: {e}", e);
|
||||
}
|
||||
finally
|
||||
{
|
||||
Stream.Dispose();
|
||||
}
|
||||
}
|
||||
catch (TaskCanceledException)
|
||||
{
|
||||
// Ignored
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
// Ignored
|
||||
// ignored
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Logger.LogWarning("An unhandled error occured while reading from container stream: {e}", e);
|
||||
Logger.LogError("An error occured while attaching to container: {e}", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Reset stream so no further inputs will be piped to it
|
||||
Stream = null;
|
||||
|
||||
Logger.LogDebug("Disconnected from container stream");
|
||||
});
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public async Task WriteOutput(string output)
|
||||
|
||||
Reference in New Issue
Block a user