Beispiel #1
0
        private async Task SendBatchEvents(CancellationToken cancellationToken)
        {
            var batch = new List <Message>(batchSize);

            while (!cancellationToken.IsCancellationRequested)
            {
                while ((batch.Count < batchSize) && !cancellationToken.IsCancellationRequested)
                {
                    if (!queue.TryDequeue(out var result))
                    {
                        await Task.Delay(interval / 2);

                        continue;
                    }
                    var lag  = DateTime.UtcNow - result;
                    var body = JsonConvert.SerializeObject(new { result, lag });
                    logger.LogInformation("SendBatchEvents: {0} lag", lag);
                    batch.Add(new Message(Encoding.Unicode.GetBytes(body)));
                }
                if (batch.Count > 0)
                {
                    await moduleClient.SendEventBatchAsync(batch, cancellationToken);

                    logger.LogInformation("Sent {0} messages", batch.Count);
                    batch.Clear();
                }
            }
            logger.LogInformation("SendBatchEvents cancelled");
        }
        /// <summary>
        /// Send the batch of events.
        /// </summary>
        /// <param name="batch">the set of events to send</param>
        protected virtual async Task SendBatchAsync(IList <Message> batch)
        {
            if (batch == null || batch.Count == 0)
            {
                return;
            }

            ModuleClient client = await ModuleClientCache.Instance.GetOrCreateAsync().ConfigureAwait(false);

            if (string.IsNullOrEmpty(this.attribute.OutputName))
            {
                await client.SendEventBatchAsync(batch).ConfigureAwait(false);
            }
            else
            {
                await client.SendEventBatchAsync(this.attribute.OutputName, batch).ConfigureAwait(false);
            }
        }