Refactor naming for background tasks.

This commit is contained in:
Christian
2020-11-25 21:07:50 +01:00
parent 7738c5faa1
commit ec015e7620
6 changed files with 10 additions and 8 deletions
+1 -1
View File
@@ -355,7 +355,7 @@ namespace MQTTnet.Client
{ {
// This handler must be executed in a new thread because otherwise a dead lock may happen // This handler must be executed in a new thread because otherwise a dead lock may happen
// when trying to reconnect in that handler etc. // when trying to reconnect in that handler etc.
Task.Run(() => disconnectedHandler.HandleDisconnectedAsync(new MqttClientDisconnectedEventArgs(clientWasConnected, exception, authenticateResult, _disconnectReason))).Forget(_logger); Task.Run(() => disconnectedHandler.HandleDisconnectedAsync(new MqttClientDisconnectedEventArgs(clientWasConnected, exception, authenticateResult, _disconnectReason))).RunInBackground(_logger);
} }
} }
} }
@@ -78,7 +78,7 @@ namespace MQTTnet.Implementations
_socket.Bind(_localEndPoint); _socket.Bind(_localEndPoint);
_socket.Listen(_options.ConnectionBacklog); _socket.Listen(_options.ConnectionBacklog);
Task.Run(() => AcceptClientConnectionsAsync(cancellationToken), cancellationToken).Forget(_logger); Task.Run(() => AcceptClientConnectionsAsync(cancellationToken), cancellationToken).RunInBackground(_logger);
return true; return true;
} }
@@ -115,7 +115,7 @@ namespace MQTTnet.Implementations
continue; continue;
} }
Task.Run(() => TryHandleClientConnectionAsync(clientSocket), cancellationToken).Forget(_logger); Task.Run(() => TryHandleClientConnectionAsync(clientSocket), cancellationToken).RunInBackground(_logger);
} }
catch (OperationCanceledException) catch (OperationCanceledException)
{ {
+4 -2
View File
@@ -5,11 +5,13 @@ namespace MQTTnet.Internal
{ {
public static class TaskExtensions public static class TaskExtensions
{ {
public static void Forget(this Task task, IMqttNetScopedLogger logger) public static void RunInBackground(this Task task, IMqttNetScopedLogger logger = null)
{ {
task?.ContinueWith(t => task?.ContinueWith(t =>
{ {
logger.Error(t.Exception, "Unhandled exception."); // Consume the exception first so that we get no exception regarding the not observed exception.
var exception = t.Exception;
logger?.Error(exception, "Unhandled exception in background task.");
}, },
TaskContinuationOptions.OnlyOnFaulted); TaskContinuationOptions.OnlyOnFaulted);
} }
@@ -160,7 +160,7 @@ namespace MQTTnet.Server
await SendAsync(_channelAdapter.PacketFormatterAdapter.DataConverter.CreateConnAckPacket(_connectionValidatorContext), cancellationToken).ConfigureAwait(false); await SendAsync(_channelAdapter.PacketFormatterAdapter.DataConverter.CreateConnAckPacket(_connectionValidatorContext), cancellationToken).ConfigureAwait(false);
Task.Run(() => SendPendingPacketsAsync(cancellationToken), cancellationToken).Forget(_logger); Task.Run(() => SendPendingPacketsAsync(cancellationToken), cancellationToken).RunInBackground(_logger);
Session.IsCleanSession = false; Session.IsCleanSession = false;
@@ -50,7 +50,7 @@ namespace MQTTnet.Server
public void Start(CancellationToken cancellationToken) public void Start(CancellationToken cancellationToken)
{ {
Task.Run(() => TryProcessQueuedApplicationMessagesAsync(cancellationToken), cancellationToken).Forget(_logger); Task.Run(() => TryProcessQueuedApplicationMessagesAsync(cancellationToken), cancellationToken).RunInBackground(_logger);
} }
public async Task HandleClientConnectionAsync(IMqttChannelAdapter channelAdapter, CancellationToken cancellationToken) public async Task HandleClientConnectionAsync(IMqttChannelAdapter channelAdapter, CancellationToken cancellationToken)
@@ -29,7 +29,7 @@ namespace MQTTnet.Server
// support async/await. Async etc. is avoided here because the thread will usually check // support async/await. Async etc. is avoided here because the thread will usually check
// the connections every few milliseconds and thus the context changes (due to async) are // the connections every few milliseconds and thus the context changes (due to async) are
// only consuming resources. Also there is just 1 thread for the entire server which is fine at all! // only consuming resources. Also there is just 1 thread for the entire server which is fine at all!
Task.Factory.StartNew(_ => DoWork(cancellationToken), cancellationToken, TaskCreationOptions.LongRunning).Forget(_logger); Task.Factory.StartNew(_ => DoWork(cancellationToken), cancellationToken, TaskCreationOptions.LongRunning).RunInBackground(_logger);
} }
void DoWork(CancellationToken cancellationToken) void DoWork(CancellationToken cancellationToken)