0
votes

We have a job hosted in an azure website, the job reads entries from a topic subscription. Everything works fine when we only have one instance to host the website. Once we scale out to more than one instance we observe the message is processed as many times as instances we have. Each instance points to the same subscription. From what we read, once the item is read, it won't be available for any other process. The duplicated processing is happening inside the same instance, meaning that if we have two instances, the item is processed twice in one of the instances, it is not splitted.

What can be possible be wrong in the way we are doing things? This is how we proceed to configure the connection to the queue, if the subscription does not exists, it is created:

 var serviceBusConfig = new ServiceBusConfiguration
        {
            ConnectionString = transactionsBusConnectionString
        };
        config.UseServiceBus(serviceBusConfig);

        var allRule1 = new RuleDescription
        {
            Name = "All",
            Filter = new TrueFilter()
        };
        SetupSubscription(transactionsBusConnectionString,"topic1", "subscription1", allRule1);


   private static void SetupSubscription(string busConnectionString, string topicNameKey, string subscriptionNameKey, RuleDescription newRule)
    {
        var namespaceManager =
            NamespaceManager.CreateFromConnectionString(busConnectionString);
        var topicName = ConfigurationManager.AppSettings[topicNameKey];
        var subscriptionName = ConfigurationManager.AppSettings[subscriptionNameKey];
        if (!namespaceManager.SubscriptionExists(topicName, subscriptionName))
        {
            namespaceManager.CreateSubscription(topicName, subscriptionName);
        }
        var subscriptionClient = SubscriptionClient.CreateFromConnectionString(busConnectionString, topicName, subscriptionName);
        var rules = namespaceManager.GetRules(topicName, subscriptionName);

        foreach (var rule in rules)
        {
            subscriptionClient.RemoveRule(rule.Name);
        }
        subscriptionClient.AddRule(newRule);

        rules = namespaceManager.GetRules(topicName, subscriptionName);
        rules.ToString();
    }

Example of the code that process the topic item:

   public void SendInAppNotification(
        [ServiceBusTrigger("%eventsTopicName%", "%SubsInAppNotifications%"), ServiceBusAccount("OutputServiceBus")] Notification message)
    {
        this.valueCalculator.AddInAppNotification(message);
    }

This method is inside a Function static class, I'm using azure web job sdk.

Whenever the azure web site is scaled to more than one instance, all the instances share the same configuration.

2
What does your processing logic look like? You may not be locking the message, or you may not be completing the message properly.JTaub
Any update on your problem ?Thomas
The sdk takes care of completing the message, I still have the problem, but I've added a trace to the create subscription part to verify it it is created multiple times. Now, even in that case, it is using the same subscription name, but it is not a bad idea to pout that tracing.user1075679
The subscriptions are not created every time the process runs. So it won't be because of that.user1075679

2 Answers

0
votes

It sounds like you're creating a new subscription each time your new instance runs, rather than hooking into an existing one. Topics are designed to allow multiple subscribers to attach in that way as well - usually though each subscriber has a different purpose, so they each see a copy of the message.

I cant verify this from your code snippet but that's my guess - are the config files identical? You should add some trace output to see if your processes are calling CreateSubscription() each time they run.

0
votes

I think I can access the message id, I'm using azure web job sdk but I think I can find a way to get it. Let me check it and will let you know.