TommyN
TommyN

Reputation: 2381

Google App Engine - Cron or Task Queue?

I'm building a simple "play against a random opponent" back-end using Goole App Engine. So far I'm adding each user that wants to play into a "table" in the Datastore. As soon as there are more than 1 player in the Datastore, I can start to match them.

The Schedule Tasks with Cron looked promising for this work until I saw that the lowest resolution seems to be minutely. If there are plenty of players signing up I want them to be matched quickly and not have to wait a whole minute (worst case).

I thought about having the servlet that recives the "play against random opponent" request POST to a Task Queue that would do the match making, but I think this will lead to a lot of contention when reading from the Datastore and deleting the enteties from the "random" table after they have been matched?

Basically I want a single worker that will do the matching, and I want to signal this worker from time to time that now is a good time to try to match opponents.

Any suggestions on what would be the right course of action here?

Upvotes: 2

Views: 889

Answers (1)

Peter Knego
Peter Knego

Reputation: 80330

You can guarantee exclusive access via transactions:

  1. Receive a request to play via REST. Check (within a transaction) if there is any request in database.

  2. If there is, notify both users to start the play and delete request (transactionaly) from database.

  3. If there isn't, add it to the database and wait for the next request.

Update:

Alternativelly you can achieve what you need via pull queue. Same scenario as above, just instead of datastore you'd check if there is a task in the pull queue, retrieve if there is or create a new one if there isn't one.

Upvotes: 2

Related Questions