How can continuous Azure Web Jobs be idempotent and send emails?

After reading tons of information on the Internet about Azure WebJobs, the documentation says that work should be idempotent, on the other hand, blogs say that they use WebJobs to do things like β€œcharge customers,” β€œsend email.”

This documentation states that running a continuous WebJob on multiple instances with a queue can result in a call more than once. Do people really ignore the fact that they can charge their customer twice, or send an email twice?

How can I make sure that I can run WebJob with a queue in a scaled web application, and messages are processed only once?

+6
source share
1 answer

I am doing this using a database, a row lock update request, and a TransactionScope object.

In the Order table, create a column to control the status of the action you take in your WebJob. those. EmailSent.

In the QueueTrigger function, start the transaction, then execute the UPDATE query in the sales order using the ROWLOCK set, which sets EmailSent = 1 with WHERE EmailSent = 0. If the return value is from SqlCommand = 0, then exit the function. Another WebJob has already sent an email. Otherwise, send an email and call Complete () on the TransactionScope object if it was sent successfully.

This should provide the desired idempotency.

Hope this helps.

+1
source

Source: https://habr.com/ru/post/989416/


All Articles