Example #1
0
        /// <summary>
        /// Dequeues available WebHooks and sends them out to each WebHook recipient.
        /// </summary>
        protected virtual async Task DequeueAndSendWebHooks(CancellationToken cancellationToken)
        {
            bool isEmpty = false;

            while (true)
            {
                try
                {
                    do
                    {
                        if (cancellationToken.IsCancellationRequested)
                        {
                            return;
                        }

                        // Dequeue messages from Azure queue
                        CloudQueue _queue = await _storageManager.GetCloudQueueAsync(_options.ConnectionString, AzureWebHookSender.WebHookQueue);

                        IEnumerable <CloudQueueMessage> messages = await _storageManager.GetMessagesAsync(_queue, MaxDequeuedMessages, _options.MessageTimeout);

                        // Extract the work items
                        ICollection <WebHookWorkItem> workItems = messages.Select(m =>
                        {
                            WebHookWorkItem workItem             = JsonConvert.DeserializeObject <WebHookWorkItem>(m.AsString, _serializerSettings);
                            workItem.Properties[QueueMessageKey] = m;
                            return(workItem);
                        }).ToArray();

                        if (cancellationToken.IsCancellationRequested)
                        {
                            return;
                        }

                        // Submit work items to be sent to WebHook receivers
                        if (workItems.Count > 0)
                        {
                            await _sender.SendWebHookWorkItemsAsync(workItems);
                        }
                        isEmpty = workItems.Count == 0;
                    }while (!isEmpty);
                }
                catch (Exception ex)
                {
                    CloudQueue _queue = await _storageManager.GetCloudQueueAsync(_options.ConnectionString, AzureWebHookSender.WebHookQueue);

                    string msg = string.Format(AzureStorageResource.DequeueManager_ErrorDequeueing, _queue.Name, ex.Message);
                    _logger.LogError(msg, ex);
                }

                try
                {
                    await Task.Delay(_options.Frequency, cancellationToken);
                }
                catch (OperationCanceledException oex)
                {
                    _logger.LogError(oex.Message, oex);
                    return;
                }
            }
        }
Example #2
0
        /// <inheritdoc />
        public async Task SendWebHookWorkItemsAsync(IEnumerable <WebHookWorkItem> workItems)
        {
            if (workItems == null)
            {
                throw new ArgumentNullException("workItems");
            }

            // Serialize WebHook requests and convert to queue messages
            IEnumerable <CloudQueueMessage> messages = null;

            try
            {
                messages = workItems.Select(item =>
                {
                    string content            = JsonConvert.SerializeObject(item, _serializerSettings);
                    CloudQueueMessage message = new CloudQueueMessage(content);
                    return(message);
                }).ToArray();
            }
            catch (Exception ex)
            {
                string msg = string.Format(AzureStorageResource.AzureSender_SerializeFailure, ex.Message);
                _logger.LogError(msg, ex);
                throw new InvalidOperationException(msg);
            }

            // Insert queue messages into queue.
            CloudQueue queue = await _manager.GetCloudQueueAsync(_options.ConnectionString, WebHookQueue);

            await _manager.AddMessagesAsync(queue, messages);
        }