views:

46

answers:

4

I don't know whether race condition is the best word, but I didn't find a better word.

In some situations I send a mail to the administrators of my application. They can then log in, verify some data and then automatically send an email to all the users of the application. How do I prevent the situation where two or more administrators are logged in and both press the send-button? I only want the mail to be send once in that case.

A: 

The simplest way that comes to my mind is to create like 5 minutes lock-out period before sending another e-mail and informing administrator that something like that just happen, so (s)he can cancel sending his/her own.

Paweł Dyda
+2  A: 

Probably the best is that pressing the send-button doesn't send the email, it just adds a task to a task queue. The handler for that task then ensures that the message is sent only once (and it can retry sending the message on failure by re-queueing the task, or it can batch the email up to send to only a limited number of recipients at a time, however long that ends up taking).

If your app currently gives the admin feedback when the message fails to send, this means you need to give the admin some view of the task queue and its results.

Steve Jessop
thanks, the task queue seems like a good idea. (silly that I didn't think of it)
nvcleemp
+4  A: 

I'd say go with named tasks. Add a task to the queue whenever the button is clicked, but ensure the name in both cases will prevent duplicates (you could name it with today's date or a constant, for example).

So if the task is already in queue, you'll find out... you can then choose what action to take. Your handler could also record that this mail has already been actioned by an admin, and tell the other admins so when they log in.

Sudhir Jonathan
OK, thanks for the idea. Looks very useful.
nvcleemp
A: 

You could create a very basic workflow system to get around the problem. You could have the concept of a Task in your system and persist task information to database table. When your app mails the administrators it should add a task to this table. When an administrator logs on they should assign the task to themselves. As soon as an administrator assigns the task this is reflected in the database, preventing anyone else from assigning it.

The kind of things your Task might model are;

  • id
  • name
  • description
  • status (not assigned, assigned, completed etc)
  • user (user ID of person this task is assigned to)
  • severity (a nice to have)
Qwerky