async Task IEventProcessor.ProcessEventsAsync(PartitionContext context, IEnumerable <EventData> messages)
        {
            EventHubTriggerInput value = new EventHubTriggerInput
            {
                Events  = messages.ToArray(),
                Context = context
            };

            // Single dispatch
            if (_singleDispatch)
            {
                int len = value.Events.Length;

                Task[] dispatches = new Task[len];
                for (int i = 0; i < len; i++)
                {
                    TriggeredFunctionData input = new TriggeredFunctionData
                    {
                        ParentId     = null,
                        TriggerValue = value.GetSingleEventTriggerInput(i)
                    };
                    dispatches[i] = _executor.TryExecuteAsync(input, CancellationToken.None);
                }

                // Drain the whole batch before taking more work
                await Task.WhenAll(dispatches);
            }
            else
            {
                // Batch dispatch

                TriggeredFunctionData input = new TriggeredFunctionData
                {
                    ParentId     = null,
                    TriggerValue = value
                };

                FunctionResult result = await _executor.TryExecuteAsync(input, CancellationToken.None);
            }

            await context.CheckpointAsync();
        }
Ejemplo n.º 2
0
            public async Task ProcessEventsAsync(PartitionContext context, IEnumerable <EventData> messages)
            {
                EventHubTriggerInput value = new EventHubTriggerInput
                {
                    Events           = messages.ToArray(),
                    PartitionContext = context
                };

                // Single dispatch
                if (_parent._singleDispatch)
                {
                    int len = value.Events.Length;

                    List <Task> dispatches = new List <Task>();
                    for (int i = 0; i < len; i++)
                    {
                        if (_cts.IsCancellationRequested)
                        {
                            // If we stopped the listener, then we may lose the lease and be unable to checkpoint.
                            // So skip running the rest of the batch. The new listener will pick it up.
                            continue;
                        }
                        else
                        {
                            TriggeredFunctionData input = new TriggeredFunctionData
                            {
                                ParentId     = null,
                                TriggerValue = value.GetSingleEventTriggerInput(i)
                            };
                            Task task = _parent._executor.TryExecuteAsync(input, _cts.Token);
                            dispatches.Add(task);
                        }
                    }

                    // Drain the whole batch before taking more work
                    if (dispatches.Count > 0)
                    {
                        await Task.WhenAll(dispatches);
                    }
                }
                else
                {
                    // Batch dispatch
                    TriggeredFunctionData input = new TriggeredFunctionData
                    {
                        ParentId     = null,
                        TriggerValue = value
                    };

                    FunctionResult result = await _parent._executor.TryExecuteAsync(input, _cts.Token);
                }

                bool hasEvents = false;

                // Dispose all messages to help with memory pressure. If this is missed, the finalizer thread will still get them.
                foreach (var message in messages)
                {
                    hasEvents = true;
                    message.Dispose();
                }

                // Don't checkpoint if no events. This can reset the sequence counter to 0.
                if (hasEvents)
                {
                    await _checkpoint(context);
                }
            }
Ejemplo n.º 3
0
            public async Task ProcessEventsAsync(PartitionContext context, IEnumerable <EventData> messages)
            {
                EventHubTriggerInput value = new EventHubTriggerInput
                {
                    Events  = messages.ToArray(),
                    Context = context
                };

                // Single dispatch
                if (_parent._singleDispatch)
                {
                    int len = value.Events.Length;

                    List <Task> dispatches = new List <Task>();
                    for (int i = 0; i < len; i++)
                    {
                        if (_cts.IsCancellationRequested)
                        {
                            // If we stopped the listener, then we may lose the lease and be unable to checkpoint.
                            // So skip running the rest of the batch. The new listener will pick it up.
                            continue;
                        }
                        else
                        {
                            TriggeredFunctionData input = new TriggeredFunctionData
                            {
                                ParentId     = null,
                                TriggerValue = value.GetSingleEventTriggerInput(i)
                            };
                            Task task = this._parent._executor.TryExecuteAsync(input, _cts.Token);
                            dispatches.Add(task);
                        }
                    }

                    // Drain the whole batch before taking more work
                    if (dispatches.Count > 0)
                    {
                        await Task.WhenAll(dispatches);
                    }
                }
                else
                {
                    // Batch dispatch

                    TriggeredFunctionData input = new TriggeredFunctionData
                    {
                        ParentId     = null,
                        TriggerValue = value
                    };

                    FunctionResult result = await this._parent._executor.TryExecuteAsync(input, CancellationToken.None);
                }

                bool hasEvents = false;

                // Dispose all messages to help with memory pressure. If this is missed, the finalizer thread will still get them.
                foreach (var message in messages)
                {
                    hasEvents = true;
                    message.Dispose();
                }

                // Don't checkpoint if no events. This can reset the sequence counter to 0.
                if (hasEvents)
                {
                    // There are lots of reasons this could fail. That just means that events will get double-processed, which is inevitable
                    // with event hubs anyways.
                    // For example, it could fail if we lost the lease. That could happen if we failed to renew it due to CPU starvation or an inability
                    // to make the outbound network calls to renew.
                    await context.CheckpointAsync();
                }
            }