Как узнать, что сообщение SendGrid завершается неудачно из асинхронной функции Azure? - PullRequest
0 голосов
/ 08 апреля 2019

У меня есть функция azure, настроенная на чтение из концентратора событий для отправки оповещений через sendgrid или twilio.Я хочу знать, когда я отправляю сообщение SendGrid «await smsCollector.AddAsync (mobileMessage)», было ли оно отправлено успешно, т. Е. Электронное письмо не было недействительным.Возможно ли это с этой настройкой?

         public static class SendAlert
         {
        [FunctionName("v1_EventHub_SendAlert")]
        public static async Task Run(
            [EventHubTrigger("v1-email-hub", Connection = "EventHubConnection")] EventData[] events,
            [SendGrid] IAsyncCollector<SendGridMessage> messageCollector,
            [TwilioSms(From = "xXXXxxXxxx)] IAsyncCollector<CreateMessageOptions> smsCollector,
            [Inject] NotificationEventLogic eventLogic,
            [Inject] NotificationLogic notificationLogic,
            ILogger log)
        {
            foreach (EventData eventData in events)
            {
                string messageBody = Encoding.UTF8.GetString(eventData.Body.Array, eventData.Body.Offset, eventData.Body.Count);

                var notificationEvents = JsonConvert.DeserializeObject<List<NotificationEvent>>(messageBody);

                foreach (var ev in notificationEvents)
                {
                    var notification = await notificationLogic.GetNotificationAsync(ev.NotificationId);

                    if (ev.NotificationEventType == NotificationEventType.Email)
                    {
                        var message = CreateEmail(ev, notification);
                        await messageCollector.AddAsync(message);
                    }
                    else if (ev.NotificationEventType == NotificationEventType.SMS)
                    {
                        var mobileMessage = CreateSms(ev, notification);

                        await smsCollector.AddAsync(mobileMessage);
                    }

                    await eventLogic.CreateEventAsync(ev);
                }

            }
        }

1 Ответ

0 голосов
/ 09 апреля 2019

IAsyncCollector имеет метод FlushAsync , который вы можете вызвать.AddAsync может буферизовать элементы, но FlushAsync пропустит их (и выдаст исключение при ошибке).

...