private async Task DoForAllPumps(MessagePumpTypes waitForPumpTypes, Func<IMessagePump, Task> action) { var typesToProcessInBackground = (MessagePumpTypes)((int)waitForPumpTypes ^ -1); var messagePumpsToWaitFor = GetMessagePumps(waitForPumpTypes).ToArray(); var messagePumpsToHandleInBackground = GetMessagePumps(typesToProcessInBackground).ToArray(); await messagePumpsToWaitFor .Select(pump => Task.Run(async () => await action(pump))) .WhenAll(); #pragma warning disable 4014 Task.Run(async () => { // pause for a tiny bit here so that if people want messages on the bus immediately then their // _bus.Send/Whatever(...) call can get into the threadpool queue before we flood it with potentially // thousands of other message pump creation tasks. await Task.Delay(100); await messagePumpsToHandleInBackground .Select(pump => Task.Run(async () => await action(pump))) .WhenAll(); }); #pragma warning restore 4014 }
public Task Stop(MessagePumpTypes messagePumpTypes = MessagePumpTypes.All) { return(Task.Run(async() => { lock (_mutex) { if (!_isRunning) { return; } _isRunning = false; } _logger.Debug("Bus stopping..."); var sw = Stopwatch.StartNew(); try { var stoppingHandler = Stopping; stoppingHandler?.Invoke(this, EventArgs.Empty); await _messagePumpsManager.Stop(messagePumpTypes); } catch (AggregateException aex) { throw new BusException("Failed to stop bus", aex); } var stoppedHandler = Stopped; stoppedHandler?.Invoke(this, EventArgs.Empty); sw.Stop(); _logger.Info("Bus stopped in {Elapsed}.", sw.Elapsed); }).ConfigureAwaitFalse()); }
private async Task DoForAllPumps(MessagePumpTypes waitForPumpTypes, Func <IMessagePump, Task> action) { var typesToProcessInBackground = (MessagePumpTypes)((int)waitForPumpTypes ^ -1); var messagePumpsToWaitFor = GetMessagePumps(waitForPumpTypes).ToArray(); var messagePumpsToHandleInBackground = GetMessagePumps(typesToProcessInBackground).ToArray(); await messagePumpsToWaitFor .Select(pump => _taskFactory.StartNew(async() => await action(pump), TaskContext.ControlMessagePump).Unwrap()) .WhenAll(); #pragma warning disable 4014 Task.Run(async() => { // pause for a tiny bit here so that if people want messages on the bus immediately then their // _bus.Send/Whatever(...) call can get into the threadpool queue before we flood it with potentially // thousands of other message pump creation tasks. await Task.Delay(100); await messagePumpsToHandleInBackground .Select(pump => _taskFactory.StartNew(async() => await action(pump), TaskContext.ControlMessagePump).Unwrap()) .WhenAll(); }); #pragma warning restore 4014 }
private IEnumerable<IMessagePump> GetMessagePumps(MessagePumpTypes messagePumpTypes) { if (messagePumpTypes.HasFlag(MessagePumpTypes.Response)) yield return _responseMessagePump; if (messagePumpTypes.HasFlag(MessagePumpTypes.Request)) foreach (var pump in _requestMessagePumps) yield return pump; if (messagePumpTypes.HasFlag(MessagePumpTypes.Command)) foreach (var pump in _commandMessagePumps) yield return pump; if (messagePumpTypes.HasFlag(MessagePumpTypes.MulticastRequest)) foreach (var pump in _multicastRequestMessagePumps) yield return pump; if (messagePumpTypes.HasFlag(MessagePumpTypes.MulticastEvent)) foreach (var pump in _multicastEventMessagePumps) yield return pump; if (messagePumpTypes.HasFlag(MessagePumpTypes.CompetingEvent)) foreach (var pump in _competingEventMessagePumps) yield return pump; }
private async Task DoForAllPumps(MessagePumpTypes waitForPumpTypes, Func<IMessagePump, Task> action) { var typesToProcessInBackground = (MessagePumpTypes) ((int) waitForPumpTypes ^ -1); var messagePumpsToWaitFor = GetMessagePumps(waitForPumpTypes).ToArray(); var messagePumpsToHandleInBackground = GetMessagePumps(typesToProcessInBackground).ToArray(); var tasksToWaitFor = messagePumpsToWaitFor .Select(pump => Task.Run(() => action(pump)).ConfigureAwaitFalse()) .ToArray(); messagePumpsToHandleInBackground .Select(pump => Task.Run(() => action(pump)).ConfigureAwaitFalse()) .Done(); await tasksToWaitFor .WhenAll(); }
private async Task DoForAllPumps(MessagePumpTypes waitForPumpTypes, Func <IMessagePump, Task> action) { var typesToProcessInBackground = (MessagePumpTypes)((int)waitForPumpTypes ^ -1); var messagePumpsToWaitFor = GetMessagePumps(waitForPumpTypes).ToArray(); var messagePumpsToHandleInBackground = GetMessagePumps(typesToProcessInBackground).ToArray(); var tasksToWaitFor = messagePumpsToWaitFor .Select(pump => Task.Run(() => action(pump)).ConfigureAwaitFalse()) .ToArray(); messagePumpsToHandleInBackground .Select(pump => Task.Run(() => action(pump)).ConfigureAwaitFalse()) .Done(); await tasksToWaitFor .WhenAll(); }
private IEnumerable <IMessagePump> GetMessagePumps(MessagePumpTypes messagePumpTypes) { if (messagePumpTypes.HasFlag(MessagePumpTypes.Response)) { yield return(_responseMessagePump); } if (messagePumpTypes.HasFlag(MessagePumpTypes.Request)) { foreach (var pump in _requestMessagePumps) { yield return(pump); } } if (messagePumpTypes.HasFlag(MessagePumpTypes.Command)) { foreach (var pump in _commandMessagePumps) { yield return(pump); } } if (messagePumpTypes.HasFlag(MessagePumpTypes.MulticastRequest)) { foreach (var pump in _multicastRequestMessagePumps) { yield return(pump); } } if (messagePumpTypes.HasFlag(MessagePumpTypes.MulticastEvent)) { foreach (var pump in _multicastEventMessagePumps) { yield return(pump); } } if (messagePumpTypes.HasFlag(MessagePumpTypes.CompetingEvent)) { foreach (var pump in _competingEventMessagePumps) { yield return(pump); } } }
public async Task Start(MessagePumpTypes messagePumpTypes = MessagePumpTypes.Default) { lock (_mutex) { if (_isRunning) { return; } _isRunning = true; } _logger.Debug("Bus starting..."); try { var startingHandler = Starting; if (startingHandler != null) { startingHandler(this, EventArgs.Empty); } await _messagePumpsManager.Start(messagePumpTypes); } catch (AggregateException aex) { _logger.Error(aex, "Failed to start bus."); throw new BusException("Failed to start bus", aex); } var startedHandler = Started; if (startedHandler != null) { startedHandler(this, EventArgs.Empty); } _logger.Info("Bus started."); }
public async Task Stop(MessagePumpTypes messagePumpTypes = MessagePumpTypes.All) { lock (_mutex) { if (!_isRunning) { return; } _isRunning = false; } _logger.Debug("Bus stopping..."); try { var stoppingHandler = Stopping; if (stoppingHandler != null) { stoppingHandler(this, EventArgs.Empty); } await _messagePumpsManager.Stop(messagePumpTypes); } catch (AggregateException aex) { throw new BusException("Failed to stop bus", aex); } var stoppedHandler = Stopped; if (stoppedHandler != null) { stoppedHandler(this, EventArgs.Empty); } _logger.Info("Bus stopped."); }
public async Task Stop(MessagePumpTypes messagePumpTypes) { await DoForAllPumps(messagePumpTypes, pump => pump.Stop()); }