/// <inheritdoc/> public async Task StartAsync() { await _lock.WaitAsync(); try { if (_host != null) { _logger.Debug("Event processor host already running."); return; } _logger.Debug("Starting event processor host..."); var consumerGroup = _hub.ConsumerGroup; if (string.IsNullOrEmpty(consumerGroup)) { consumerGroup = "$default"; } _logger.Information("Using Consumer Group: \"{consumerGroup}\"", consumerGroup); if (_lease != null && _checkpoint != null) { _host = new EventHubs.Processor.EventProcessorHost( $"host-{Guid.NewGuid()}", _hub.EventHubPath, consumerGroup, GetEventHubConnectionString(), _checkpoint, _lease); } else if (_config.BlobStorageConnString != null) { _host = new EventHubs.Processor.EventProcessorHost( _hub.EventHubPath, consumerGroup, GetEventHubConnectionString(), _config.BlobStorageConnString, !string.IsNullOrEmpty(_config.LeaseContainerName) ? _config.LeaseContainerName : _hub.EventHubPath.ToSha1Hash()); } else { _logger.Error("No storage configured or checkpoint " + "manager/lease manager implementation injected."); throw new InvalidConfigurationException("Invalid checkpoint configuration."); } await _host.RegisterEventProcessorFactoryAsync( _factory, new EventProcessorOptions { InitialOffsetProvider = s => _config.InitialReadFromEnd ? EventPosition.FromEnqueuedTime(DateTime.UtcNow) : EventPosition.FromStart(), MaxBatchSize = _config.ReceiveBatchSize, ReceiveTimeout = _config.ReceiveTimeout, InvokeProcessorAfterReceiveTimeout = true }); _logger.Information("Event processor host started."); } catch (Exception ex) { _logger.Error(ex, "Error starting event processor host."); _host = null; throw ex; } finally { _lock.Release(); } }
/// <inheritdoc/> public async Task StartAsync() { if (_host != null) { return; } try { await _lock.WaitAsync(); if (_host != null) { return; } var consumerGroup = _config.ConsumerGroup; if (string.IsNullOrEmpty(consumerGroup)) { consumerGroup = "$default"; } if (_lease != null && _checkpoint != null) { _host = new EventHubs.Processor.EventProcessorHost( $"host-{Guid.NewGuid()}", _config.EventHubPath, consumerGroup, GetEventHubConnectionString(), _checkpoint, _lease); } else if (_config.BlobStorageConnString != null && _config.LeaseContainerName != null) { _host = new EventHubs.Processor.EventProcessorHost( _config.EventHubPath, consumerGroup, GetEventHubConnectionString(), _config.BlobStorageConnString, _config.LeaseContainerName); } else { _logger.Error("No checkpointing storage configured or checkpoint " + "manager/lease manager implementation injected."); throw new InvalidConfigurationException( "Invalid checkpoint configuration."); } await _host.RegisterEventProcessorFactoryAsync( _factory, new EventProcessorOptions { InitialOffsetProvider = s => EventPosition.FromEnqueuedTime(DateTime.UtcNow), MaxBatchSize = _config.ReceiveBatchSize, ReceiveTimeout = _config.ReceiveTimeout, InvokeProcessorAfterReceiveTimeout = true }); } catch (Exception ex) { _logger.Error("Error starting event processor host", () => ex); _host = null; throw ex; } finally { _lock.Release(); } }