private async Task ProcessAsync() { try { TaskRunner.RunInBackground(() => _appLifecycleManager.StartAsync()).IgnoreAwait(Log, "Exception on app lifecycle manager initialization"); while (true) { var transportConnectionResult = await _incomingConnections.TryReadAsync().ConfigureAwait(false); if (transportConnectionResult.HasValue) { var transportConnection = transportConnectionResult.Value; _activeConnections.TryAdd(transportConnection.Id, transportConnection); TaskRunner .RunInBackground(ProcessConnectionAsync, transportConnection) .ContinueWithSynchronously((Action <Task, object>)OnConnectionProcessed, transportConnection) .IgnoreAwait(Log); } else { Log.Trace("Transport connection listening completed"); break; } } } catch (Exception ex) { var activeConnections = _activeConnections.Values.ToArray(); _activeConnections.Clear(); foreach (var activeConnection in activeConnections) { activeConnection.TryTerminate(ex); } throw; } finally { var activeConnections = _activeConnections.Values.ToArray(); _activeConnections.Clear(); if (activeConnections.Length > 0) { Log.Info("Terminating {0} active connections", activeConnections.Length); foreach (var activeConnection in activeConnections) { Log.Trace("Terminating connection {0}", activeConnection); activeConnection.TryTerminate(); } await Task.WhenAll(activeConnections.Select(x => x.Completion.IgnoreExceptions())).ConfigureAwait(false); Log.Info("Terminated {0} active connections", activeConnections.Length); } } }
protected override async Task <Task> StartCoreAsync() { Log.Info("Starting broker in directory {0}", _workingDir); await Task .WhenAll( _connectionListener.StartAsync(), _brokerProcessor.StartAsync(), _appLifecycleManager.StartAsync()) .ConfigureAwait(false); Log.Info("Broker started in directory {0}", _workingDir); return(ProcessAsync()); }