Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google App Engine Locking

just wondering if anyone of you has come across this. I'm playing around with the Python mail API on Google App Engine and I created an app that accepts a message body and address via POST, creates an entity in the datastore, then a cron job is run every minute, grabs 200 entities and sends out the emails, then deletes the entities.

I ran an experiment with 1500 emails, had 1500 entities created in the datastore and 1500 emails were sent out. I then look at my stats and see that approx. 45,000 recipients were used from the quota, how is that possible?

So my question is at which point does the "Recipients Emailed" quota actually count? At the point where I create a mail object or when I actually send() it? I was hoping for the second, but the quotas seem to show something different. I do pass the mail object around between crons and tasks, etc. Anybody has any info on this?

Thanks.

Update: Turns out I actually was sending out 45k emails with a queue of only 1500. It seems that one cron job runs until the previous one is finished and works out with the same entities. So the question changes to "how do I lock the entities and make sure nobody selects them before sending the emails"?

Thanks again!

like image 931
kovshenin Avatar asked May 24 '11 11:05

kovshenin


2 Answers

Use tasks to send the email.

Create a task that takes a key as an argument, retrieves the stored entity for that key, then sends the email.

When your handler receives the body and address, store that as you do now but then enqueue a task to do the send and pass the key of your datastore object to the task so it knows which object to send an email for.

You may find that the body and address are small enough that you can simply pass them as arguments to a task and have the task send the email without having to store anything directly in the datastore.

This also has the advantage that if you want to impose a limit on the number of emails sent within a given amount of time (quota) you can set up a task queue with that rate.

like image 198
Bryce Cutt Avatar answered Sep 23 '22 16:09

Bryce Cutt


Instantiating an email object certainly does not count against your "recipients emailed" quota. Like other App Engine services, you consume quota when you trigger an RPC, i.e. call send().

If you intended to email 1500 recipients and App Engine says you emailed 45,000, your code has a bug.

like image 21
Drew Sears Avatar answered Sep 21 '22 16:09

Drew Sears