|
|
@@ -99,11 +99,21 @@ public sealed class AgentWorkerRuntime : IHostedService, IDisposable, IAgentWork |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
catch (Exception ex) |
|
|
|
|
|
|
|
|
catch (OperationCanceledException) |
|
|
|
|
|
{ |
|
|
|
|
|
// Time to shut down. |
|
|
|
|
|
break; |
|
|
|
|
|
} |
|
|
|
|
|
catch (Exception ex) when (!_shutdownCts.IsCancellationRequested) |
|
|
{ |
|
|
{ |
|
|
_logger.LogError(ex, "Error reading from channel."); |
|
|
_logger.LogError(ex, "Error reading from channel."); |
|
|
channel = RecreateChannel(channel); |
|
|
channel = RecreateChannel(channel); |
|
|
} |
|
|
} |
|
|
|
|
|
catch |
|
|
|
|
|
{ |
|
|
|
|
|
// Shutdown requested. |
|
|
|
|
|
break; |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@@ -113,34 +123,39 @@ public sealed class AgentWorkerRuntime : IHostedService, IDisposable, IAgentWork |
|
|
var outboundMessages = _outboundMessagesChannel.Reader; |
|
|
var outboundMessages = _outboundMessagesChannel.Reader; |
|
|
while (!_shutdownCts.IsCancellationRequested) |
|
|
while (!_shutdownCts.IsCancellationRequested) |
|
|
{ |
|
|
{ |
|
|
await outboundMessages.WaitToReadAsync().ConfigureAwait(false); |
|
|
|
|
|
|
|
|
|
|
|
// Read the next message if we don't already have an unsent message |
|
|
|
|
|
// waiting to be sent. |
|
|
|
|
|
if (!outboundMessages.TryRead(out var message)) |
|
|
|
|
|
|
|
|
try |
|
|
{ |
|
|
{ |
|
|
break; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
await outboundMessages.WaitToReadAsync().ConfigureAwait(false); |
|
|
|
|
|
|
|
|
while (!_shutdownCts.IsCancellationRequested) |
|
|
|
|
|
{ |
|
|
|
|
|
try |
|
|
|
|
|
|
|
|
// Read the next message if we don't already have an unsent message |
|
|
|
|
|
// waiting to be sent. |
|
|
|
|
|
if (!outboundMessages.TryRead(out var message)) |
|
|
{ |
|
|
{ |
|
|
await channel.RequestStream.WriteAsync(message, _shutdownCts.Token).ConfigureAwait(false); |
|
|
|
|
|
break; |
|
|
break; |
|
|
} |
|
|
} |
|
|
catch (Exception ex) when (!_shutdownCts.IsCancellationRequested) |
|
|
|
|
|
{ |
|
|
|
|
|
_logger.LogError(ex, "Error writing to channel."); |
|
|
|
|
|
channel = RecreateChannel(channel); |
|
|
|
|
|
continue; |
|
|
|
|
|
} |
|
|
|
|
|
catch |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
while (!_shutdownCts.IsCancellationRequested) |
|
|
{ |
|
|
{ |
|
|
// Shutdown requested. |
|
|
|
|
|
|
|
|
await channel.RequestStream.WriteAsync(message, _shutdownCts.Token).ConfigureAwait(false); |
|
|
break; |
|
|
break; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
catch (OperationCanceledException) |
|
|
|
|
|
{ |
|
|
|
|
|
// Time to shut down. |
|
|
|
|
|
break; |
|
|
|
|
|
} |
|
|
|
|
|
catch (Exception ex) when (!_shutdownCts.IsCancellationRequested) |
|
|
|
|
|
{ |
|
|
|
|
|
_logger.LogError(ex, "Error writing to channel."); |
|
|
|
|
|
channel = RecreateChannel(channel); |
|
|
|
|
|
continue; |
|
|
|
|
|
} |
|
|
|
|
|
catch |
|
|
|
|
|
{ |
|
|
|
|
|
// Shutdown requested. |
|
|
|
|
|
break; |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@@ -286,10 +301,6 @@ public sealed class AgentWorkerRuntime : IHostedService, IDisposable, IAgentWork |
|
|
public async Task StopAsync(CancellationToken cancellationToken) |
|
|
public async Task StopAsync(CancellationToken cancellationToken) |
|
|
{ |
|
|
{ |
|
|
_shutdownCts.Cancel(); |
|
|
_shutdownCts.Cancel(); |
|
|
lock (_channelLock) |
|
|
|
|
|
{ |
|
|
|
|
|
_channel?.Dispose(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
_outboundMessagesChannel.Writer.TryComplete(); |
|
|
_outboundMessagesChannel.Writer.TryComplete(); |
|
|
|
|
|
|
|
|
@@ -302,6 +313,10 @@ public sealed class AgentWorkerRuntime : IHostedService, IDisposable, IAgentWork |
|
|
{ |
|
|
{ |
|
|
await writeTask.ConfigureAwait(false); |
|
|
await writeTask.ConfigureAwait(false); |
|
|
} |
|
|
} |
|
|
|
|
|
lock (_channelLock) |
|
|
|
|
|
{ |
|
|
|
|
|
_channel?.Dispose(); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public ValueTask SendRequest(RpcRequest request) |
|
|
public ValueTask SendRequest(RpcRequest request) |
|
|
|