In order to send not more than 100 e-mail messages per day, I create the following logic in my application:
- all e-mail messages to be sent are stored in Datastore;
- there is a cron job, which runs every 15 minutes;
- this job is to send e-mail messages from the queue;
- prior to reading messages from the Datastore queue, the job reads the value from the memcache (
is_todays_quota_exceeded
); - if it is not, try to send messages. If successful, update queue status for this message. If it fails with
apiproxy_errors.OverQuotaError
, writeis_todays_quota_exceeded
equal to1
.
The problem I have is that I should store memcache value till the end of GAE day (i.e. till the quota is replenished). How can I calculate that in seconds?
Daily quotas are replenished daily at midnight Pacific time.
Upd. I've tried the following:
now = datetime.datetime.now()
current_time = datetime.datetime(year=now.year, month=now.month, day=now.day, hour=now.hour, minute=now.minute, second=now.second)
end_of_today = datetime.datetime(year=now.year, month=now.month, day=now.day, hour=23, minute=59, second=59)
diff = end_of_today - current_time
logging.info(diff.total_seconds())
But it fails at the last line - 'datetime.timedelta' object has no attribute 'total_seconds'
. I use Python 2.5, looks like total_seconds
was implemented later.
Upd2. The following helps to calculate number of seconds till end of today:
now = datetime.datetime.utcnow()
diff = (23*60*60 + 59*60 + 59*60) - (now.hour*60*60 - now.minute*60 - now.second*60)
logging.info(diff)