How to prevent race conditions in Google App Engin

2019-08-01 06:38发布

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.

4条回答
我想做一个坏孩纸
2楼-- · 2019-08-01 07:18

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.

查看更多
女痞
3楼-- · 2019-08-01 07:25

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)
查看更多
等我变得足够好
4楼-- · 2019-08-01 07:26

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.

查看更多
一夜七次
5楼-- · 2019-08-01 07:33

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.

查看更多
登录 后发表回答