public ScriptedEventProcessorHost( string eventHubPath, string consumerGroupName, string eventHubConnectionString, string storageConnectionString, string leaseContainerName, TransportAbstraction.IHost host, TransportAbstraction.ISender sender, EventHubsConnections connections, TaskhubParameters parameters, NetheriteOrchestrationServiceSettings settings, EventHubsTraceHelper logger, string workerId) { this.eventHubPath = eventHubPath; this.consumerGroupName = consumerGroupName; this.eventHubConnectionString = eventHubConnectionString; this.storageConnectionString = storageConnectionString; this.leaseContainerName = leaseContainerName; this.host = host; this.sender = sender; this.connections = connections; this.parameters = parameters; this.taskHubGuid = parameters.TaskhubGuid.ToByteArray(); this.settings = settings; this.logger = logger; this.workerId = workerId; }
async Task ITaskHub.CreateAsync() { await this.cloudBlobContainer.CreateIfNotExistsAsync().ConfigureAwait(false); if (await this.TryLoadExistingTaskhubAsync().ConfigureAwait(false) != null) { throw new InvalidOperationException("Cannot create TaskHub: TaskHub already exists"); } // ensure the task hubs exist, creating them if necessary var tasks = new List <Task>(); tasks.Add(EventHubsUtil.EnsureEventHubExistsAsync(this.settings.ResolvedTransportConnectionString, PartitionHubs[0], this.settings.PartitionCount)); foreach (string taskhub in ClientHubs) { tasks.Add(EventHubsUtil.EnsureEventHubExistsAsync(this.settings.ResolvedTransportConnectionString, taskhub, 32)); } await Task.WhenAll(tasks); // determine the start positions long[] startPositions = await EventHubsConnections.GetQueuePositionsAsync(this.settings.ResolvedTransportConnectionString, EventHubsTransport.PartitionHubs); var taskHubParameters = new TaskhubParameters() { TaskhubName = this.settings.HubName, TaskhubGuid = Guid.NewGuid(), CreationTimestamp = DateTime.UtcNow, PartitionHubs = EventHubsTransport.PartitionHubs, ClientHubs = EventHubsTransport.ClientHubs, PartitionConsumerGroup = EventHubsTransport.PartitionConsumerGroup, ClientConsumerGroup = EventHubsTransport.ClientConsumerGroup, StartPositions = startPositions }; // save the taskhub parameters in a blob var jsonText = JsonConvert.SerializeObject( taskHubParameters, Newtonsoft.Json.Formatting.Indented, new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.None }); await this.taskhubParameters.UploadTextAsync(jsonText).ConfigureAwait(false); }
async Task ITaskHub.StartAsync() { this.shutdownSource = new CancellationTokenSource(); // load the taskhub parameters var jsonText = await this.taskhubParameters.DownloadTextAsync().ConfigureAwait(false); this.parameters = JsonConvert.DeserializeObject <TaskhubParameters>(jsonText); this.taskhubGuid = this.parameters.TaskhubGuid.ToByteArray(); // check that we are the correct taskhub! if (this.parameters.TaskhubName != this.settings.HubName) { throw new InvalidOperationException($"The specified taskhub name does not match the task hub name in {this.taskhubParameters.Name}"); } this.host.NumberPartitions = (uint)this.parameters.StartPositions.Length; this.connections = new EventHubsConnections(this.settings.ResolvedTransportConnectionString, this.parameters.PartitionHubs, this.parameters.ClientHubs) { Host = host, TraceHelper = this.traceHelper, }; await this.connections.StartAsync(this.parameters.StartPositions.Length); this.client = this.host.AddClient(this.ClientId, this.parameters.TaskhubGuid, this); this.clientEventLoopTask = Task.Run(this.ClientEventLoop); if (PartitionHubs.Length > 1) { throw new NotSupportedException("Using multiple eventhubs for partitions is not yet supported."); } if (this.client == null) { throw new InvalidOperationException("client must be started before partition hosting is started."); } string partitionsHub = PartitionHubs[0]; switch (this.settings.PartitionManagement) { case PartitionManagementOptions.EventProcessorHost: { this.traceHelper.LogInformation("Registering EventProcessorHost with EventHubs"); this.eventProcessorHost = new EventProcessorHost( partitionsHub, EventHubsTransport.PartitionConsumerGroup, this.settings.ResolvedTransportConnectionString, this.settings.ResolvedStorageConnectionString, this.cloudBlobContainer.Name); var processorOptions = new EventProcessorOptions() { InitialOffsetProvider = (s) => EventPosition.FromSequenceNumber(this.parameters.StartPositions[int.Parse(s)] - 1), MaxBatchSize = 300, PrefetchCount = 500, }; await this.eventProcessorHost.RegisterEventProcessorFactoryAsync(this, processorOptions).ConfigureAwait(false); break; } case PartitionManagementOptions.Scripted: { this.traceHelper.LogInformation($"Starting scripted partition host"); this.scriptedEventProcessorHost = new ScriptedEventProcessorHost( partitionsHub, EventHubsTransport.PartitionConsumerGroup, this.settings.ResolvedTransportConnectionString, this.settings.ResolvedStorageConnectionString, this.cloudBlobContainer.Name, this.host, this, this.connections, this.parameters, this.settings, this.traceHelper, this.settings.WorkerId); var thread = new Thread(() => this.scriptedEventProcessorHost.StartEventProcessing(this.settings, this.partitionScript)); thread.Name = "ScriptedEventProcessorHost"; thread.Start(); break; } case PartitionManagementOptions.ClientOnly: { break; } } }