Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

After reading tons of information on the web about Azure WebJobs, documentation says a job should be idempotent, on the other hand, blogs say they use WebJobs to do actions such as "charging a customer", "sending an e-mail".

This documentation says that running a continuous WebJob on multiple instances with a queue could result in being called more than once. Do people really ignore the fact that they could charge their customer twice, or send an e-mail twice?

How can I make sure I can run a WebJob with a queue on a scaled web app and messages are processed only once?

like image 957
jsgoupil Avatar asked Jun 19 '15 15:06

jsgoupil


1 Answers

I do this using a database, an update query with a row lock and TransactionScope object.

In your Order table, create a column to manage the state of the action you are taking in your WebJob. i.e. EmailSent.

In the QueueTrigger function begin a transaction, then execute an UPDATE query on the customer order with ROWLOCK set, that sets EmailSent = 1 with a WHERE EmailSent = 0. If the return value from SqlCommand = 0 then exit the function. Another WebJob has already sent the email. Otherwise, send the email and call Complete() on the TransactionScope object if sent successfully.

That should provide the idempotency you want.

Hope that helps.

like image 171
Mark Brown Avatar answered Nov 15 '22 05:11

Mark Brown