From 06b8d87b5a33d8c547464375aab5d8fdf3394a85 Mon Sep 17 00:00:00 2001 From: Dan Garner Date: Tue, 17 Oct 2023 13:48:17 +0100 Subject: [PATCH] Fix relay task (#42) --- Worker.cs | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/Worker.cs b/Worker.cs index f2d21aa..af8efe8 100644 --- a/Worker.cs +++ b/Worker.cs @@ -81,7 +81,7 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken) // Do we relay? if (!string.IsNullOrEmpty(_settings.relayOn)) { - tasks.Add(Task.Factory.StartNew(() => { new NetMQRuntime().Run(stoppingToken, RelayAsync(stoppingToken)); }, stoppingToken, TaskCreationOptions.LongRunning, TaskScheduler.Default)); + tasks.Add(Task.Factory.StartNew(() => { new NetMQRuntime().Run(stoppingToken, Relay(stoppingToken)); }, stoppingToken, TaskCreationOptions.LongRunning, TaskScheduler.Default)); } // Await all @@ -256,25 +256,27 @@ async Task HeartbeatAsync(CancellationToken stoppingToken) } } - async Task RelayAsync(CancellationToken stoppingToken) + Task Relay(CancellationToken stoppingToken) { + _logger.LogInformation("Creating a relay socket"); + using var relaySocket = new RequestSocket(_settings.relayOn); while (!stoppingToken.IsCancellationRequested) { - await Task.Run(() => { - bool result = _relayQueue.TryTake(out string message, -1, stoppingToken); - if (result && !string.IsNullOrEmpty(message)) + bool result = _relayQueue.TryTake(out string message, -1, stoppingToken); + if (result && !string.IsNullOrEmpty(message)) + { + _logger.LogDebug("Relay message"); + bool sendResult = relaySocket.TrySendFrame(message); + if (!sendResult) { - _logger.LogDebug("Relay message"); - bool sendResult = relaySocket.TrySendFrame(message); - if (!sendResult) - { - _logger.LogError("Unable to relay message"); - } + _logger.LogError("Unable to relay message"); } - }, stoppingToken); + } } + + return Task.CompletedTask; } private static Dictionary NewStats()