Beispiel #1
0
        private async Task ExecuteAsync(CancellationToken cancellationToken)
        {
            var storageAccount = CloudStorageAccount.Parse(_globalSettings.Notifications.ConnectionString);
            var queueClient    = storageAccount.CreateCloudQueueClient();

            _queue = queueClient.GetQueueReference("notifications");

            while (!cancellationToken.IsCancellationRequested)
            {
                var messages = await _queue.GetMessagesAsync(32, TimeSpan.FromMinutes(1),
                                                             null, null, cancellationToken);

                if (messages.Any())
                {
                    foreach (var message in messages)
                    {
                        var notification = JsonConvert.DeserializeObject <PushNotificationData <object> >(
                            message.AsString);
                        await HubHelpers.SendNotificationToHubAsync(notification, _hubContext, cancellationToken);

                        await _queue.DeleteMessageAsync(message);
                    }
                }
                else
                {
                    await Task.Delay(TimeSpan.FromSeconds(5), cancellationToken);
                }
            }
        }
Beispiel #2
0
        public async Task PostSend()
        {
            using (var reader = new StreamReader(Request.Body, Encoding.UTF8))
            {
                var notificationJson = await reader.ReadToEndAsync();

                if (!string.IsNullOrWhiteSpace(notificationJson))
                {
                    await HubHelpers.SendNotificationToHubAsync(notificationJson, _hubContext);
                }
            }
        }
        private async Task ExecuteAsync(CancellationToken cancellationToken)
        {
            var storageAccount = CloudStorageAccount.Parse(_globalSettings.Notifications.ConnectionString);
            var queueClient    = storageAccount.CreateCloudQueueClient();

            _queue = queueClient.GetQueueReference("notifications");

            while (!cancellationToken.IsCancellationRequested)
            {
                try
                {
                    var messages = await _queue.GetMessagesAsync(32, TimeSpan.FromMinutes(1),
                                                                 null, null, cancellationToken);

                    if (messages.Any())
                    {
                        foreach (var message in messages)
                        {
                            try
                            {
                                await HubHelpers.SendNotificationToHubAsync(
                                    message.AsString, _hubContext, cancellationToken);

                                await _queue.DeleteMessageAsync(message);
                            }
                            catch (Exception e)
                            {
                                _logger.LogError("Error processing dequeued message: " +
                                                 $"{message.Id} x{message.DequeueCount}.", e);
                                if (message.DequeueCount > 2)
                                {
                                    await _queue.DeleteMessageAsync(message);
                                }
                            }
                        }
                    }
                    else
                    {
                        await Task.Delay(TimeSpan.FromSeconds(5), cancellationToken);
                    }
                }
                catch (Exception e)
                {
                    _logger.LogError("Error processing messages.", e);
                }
            }

            _logger.LogWarning("Done processing.");
        }
        private async Task ExecuteAsync(CancellationToken cancellationToken)
        {
            _queueClient = new QueueClient(_globalSettings.Notifications.ConnectionString, "notifications");
            while (!cancellationToken.IsCancellationRequested)
            {
                try
                {
                    var messages = await _queueClient.ReceiveMessagesAsync(32);

                    if (messages.Value?.Any() ?? false)
                    {
                        foreach (var message in messages.Value)
                        {
                            try
                            {
                                await HubHelpers.SendNotificationToHubAsync(
                                    message.DecodeMessageText(), _hubContext, cancellationToken);

                                await _queueClient.DeleteMessageAsync(message.MessageId, message.PopReceipt);
                            }
                            catch (Exception e)
                            {
                                _logger.LogError("Error processing dequeued message: " +
                                                 $"{message.MessageId} x{message.DequeueCount}. {e.Message}", e);
                                if (message.DequeueCount > 2)
                                {
                                    await _queueClient.DeleteMessageAsync(message.MessageId, message.PopReceipt);
                                }
                            }
                        }
                    }
                    else
                    {
                        await Task.Delay(TimeSpan.FromSeconds(5), cancellationToken);
                    }
                }
                catch (Exception e)
                {
                    _logger.LogError("Error processing messages.", e);
                }
            }

            _logger.LogWarning("Done processing.");
        }
 public async Task PostNotification([FromBody] PushNotificationData <object> model)
 {
     await HubHelpers.SendNotificationToHubAsync(model, _hubContext);
 }