0
votes

We have webjobs consisting of several methods in a single Functions.cs file. They have servicebus triggers on topic/queues. Hence, keep listening to topic/queue for brokeredMessage. As soon as the message arrives, we have a processing logic that does lot of stuff. But, we find sometimes, all the webjobs get reinitialized suddenly. I found few articles on the website which says webjobs do get initialized and it is usual.

But, not sure if that is the only way and can we prevent it from getting reinitialized as we call brokeredMessage.Complete as soon we get brokeredMessage since we do not want it to be keep processing again and again?

Also, we have few webjobs in one app service and few webjobs in other app service. And, we find all of the webjobs from both the app service get re initialized at the same time. Not sure, why?

1

1 Answers

0
votes

You should design your process to be able to deal with occasional disconnects and failures, since this is a "feature" or applications living in the cloud.

Use a transaction to manage the critical area of your code.

Pseudo/commented code below, and a link to the Microsoft documentation is here.

var msg = receiver.Receive();

using (scope = new TransactionScope())
{
    // Do whatever work is required
    // Starting with computation and business logic.

    // Finishing with any persistence or new message generation, 
    // giving your application the best change of success. 
    // Keep in mind that all BrokeredMessage operations are enrolled in 
    // the transaction. They will all succeed or fail. 
    // If you have multiple data stores to update, you can use brokered messages 
    // to send new individual messages to do the operation on each store,
    // giving eventual consistency. 

    msg.Complete(); // mark the message as done 
    scope.Complete(); // declare the transaction done
}