【问题标题】:How do I know when a SendGrid message fails from an async Azure Function?我如何知道异步 Azure 函数的 SendGrid 消息何时失败?
【发布时间】:2019-04-08 20:13:42
【问题描述】:

我有一个 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);
                }

            }
        }

【问题讨论】:

    标签: twilio azure-functions sendgrid azure-eventhub


    【解决方案1】:

    IAsyncCollector has a FlushAsync method 你可以打电话。 AddAsync 可以缓冲项目,但 FlushAsync 会强制它们通过(并在错误时引发异常)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-25
      • 1970-01-01
      • 2013-01-27
      • 1970-01-01
      • 1970-01-01
      • 2021-03-29
      • 2014-01-22
      • 1970-01-01
      相关资源
      最近更新 更多