Skip to content

Commit

Permalink
Fix relay task (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
dasgarner authored Oct 17, 2023
1 parent cab94e6 commit 06b8d87
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions Worker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<string, int> NewStats()
Expand Down

0 comments on commit 06b8d87

Please sign in to comment.