当主题订阅禁用/启用时,Azure服务总线消息传递计数不会增加或重置

时间:2018-05-22 15:04:17

标签: azure azure-functions azureservicebus azure-management-api azure-management

我有以下工作流程:

  1. 服务总线接收消息。
  2. Azure功能触发并尝试通过HTTP将此消息传递给某些服务。
  3. 如果传递失败 - 函数抛出异常(自定义)并通过以下代码禁用主题订阅:
  4. enter image description here

    1. 并行的另一个函数ping服务的特殊运行状况检查端点,如果它达到200,它会尝试启用预订并使流程再次运行。
    2. 这些步骤可以重现N次,导致健康检查将返回200,因此交付网址为2 - 4xx码。
    3. 在下次尝试启用订阅并发送消息后,我希望递送计数会增加,并且最终(在10次递送尝试之后)它将变为死信。 实际 - 等于1.

      enter image description here

      我认为,当我调用状态已更改的CreateOrUpdate时,它可能会重置。 如果是 - 管理订阅状态而不是Microsoft.Azure.Management包的另一种方法是什么,以便不重置邮件传递计数?

      更新:功能代码

      public static class ESBTESTSubscriptionTrigger
      {
          private static readonly HttpClient Client = new HttpClient();
      
          private static IDatabase redisCache;
      
          [FunctionName("ESBTESTSubscriptionTrigger")]
          [Singleton]
          public static async Task Run([ServiceBusTrigger("Notifications", "ESBTEST", AccessRights.Listen, Connection = "NotificationsBusConnectionString")]BrokeredMessage serviceBusMessage, TraceWriter log, [Inject]IKeyVaultSecretsManager keyVaultSecretsManager)
          {
              var logicAppUrl = await keyVaultSecretsManager.GetSecretAsync("NotificationsLogicAppUrl");
      
              if (redisCache == null)
              {
                  redisCache = RedisCacheConnectionManager.GetRedisCacheConnection(
                      keyVaultSecretsManager.GetSecretAsync("RedisCacheConnectionString").GetAwaiter().GetResult());
              }
      
              if (string.IsNullOrWhiteSpace(logicAppUrl))
              {
                  log.Error("Logic App URL should be provided in Application settings of function App.");
                  throw new ParameterIsMissingException("Logic App URL should be provided in Application settings of function App.");
              }
      
              var applicaitonId = serviceBusMessage.Properties["applicationId"].ToString();
              var eventName = serviceBusMessage.Properties.ContainsKey("Event-Name") ? serviceBusMessage.Properties["Event-Name"].ToString() : string.Empty;
              if (string.IsNullOrWhiteSpace(applicaitonId))
              {
                  log.Error("ApplicationId should be present in service bus message properties.");
                  throw new ParameterIsMissingException("Application id is missing in service bus message.");
              }
      
              Stream stream = serviceBusMessage.GetBody<Stream>();
              StreamReader reader = new StreamReader(stream);
              string s = reader.ReadToEnd();
      
              var content = new StringContent(s, Encoding.UTF8, "application/json");
              content.Headers.Add("ApplicationId", applicaitonId);
      
              HttpResponseMessage response;
              try
              {
                  response = await Client.PostAsync(logicAppUrl, content);
              }
              catch (HttpRequestException e)
              {
                  log.Error($"Logic App responded with {e.Message}");
                  throw new LogicAppBadRequestException($"Logic App responded with {e.Message}", e);
              }
      
              if (!response.IsSuccessStatusCode)
              {
                  log.Error($"Logic App responded with {response.StatusCode}");
      
                  var serviceBusSubscriptionsSwitcherUrl = await keyVaultSecretsManager.GetSecretAsync("ServiceBusTopicSubscriptionSwitcherUri");
                  var sbSubscriptionSwitcherResponse = await Client.SendAsync(
                                                           new HttpRequestMessage(HttpMethod.Post, serviceBusSubscriptionsSwitcherUrl)
                                                               {
                                                                   Content =
                                                                       new
                                                                           StringContent(
                                                                               $"{{\"Action\":\"Disable\",\"SubscriptionName\":\"{applicaitonId}\"}}",
                                                                               Encoding.UTF8,
                                                                               "application/json")
                                                               });
      
                  if (sbSubscriptionSwitcherResponse.IsSuccessStatusCode == false)
                  {
                      throw new FunctionNotAvailableException($"ServiceBusTopicSubscriptionSwitcher responded with {sbSubscriptionSwitcherResponse.StatusCode}");
                  }
      
                  throw new LogicAppBadRequestException($"Logic App responded with {response.StatusCode}");
              }
      
              if (!string.IsNullOrWhiteSpace(eventName))
              {
                  redisCache.KeyDelete($"{applicaitonId}{eventName}DeliveryErrorEmailSent");
              }
          }
      }
      

0 个答案:

没有答案