Skip to content

Commit

Permalink
Use task queue with rate limit to deal with email rate limit. Cheesy.
Browse files Browse the repository at this point in the history
  • Loading branch information
kushal committed Jan 13, 2011
1 parent 4923063 commit 8fddab8
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 17 deletions.
2 changes: 1 addition & 1 deletion app.yaml
Expand Up @@ -7,7 +7,7 @@ handlers:
- url: /_ah/mail/snippets@.*fssnippets\.appspotmail\.com
script: receive_email.py
login: admin

- url: .*
script: main.py
secure: always
Expand Down
46 changes: 31 additions & 15 deletions emails.py
Expand Up @@ -15,19 +15,31 @@
"""

class ReminderEmail(webapp.RequestHandler):
def __send_mail(self, recipient):
def get(self):
all_users = User.all().filter("enabled =", True).fetch(500)
for user in all_users:
# TODO: Check if one has already been submitted for this period.
taskqueue.add(url='/onereminder', params={'email': user.email})


class OneReminderEmail(webapp.RequestHandler):
def post(self):
mail.send_mail(sender="snippets <snippets@fssnippets.appspotmail.com>",
to=recipient,
to=self.request.get('email'),
subject="Snippet time!",
body=REMINDER)

def get(self):
post(self)

class DigestEmail(webapp.RequestHandler):
def get(self):
all_users = User.all().filter("enabled =", True).fetch(500)
for user in all_users:
# TODO: Check if one has already been submitted for this period.
self.__send_mail(user.email)
taskqueue.add(url='/onedigest', params={'email': user.email})


class DigestEmail(webapp.RequestHandler):
class OneDigestEmail(webapp.RequestHandler):
def __send_mail(self, recipient, body):
mail.send_mail(sender="snippets <snippets@fssnippets.appspotmail.com>",
to=recipient,
Expand All @@ -37,15 +49,19 @@ def __send_mail(self, recipient, body):
def __snippet_to_text(self, snippet):
divider = '-' * 30
return '%s\n%s\n%s' % (snippet.user.email, divider, snippet.text)

def get(self):
all_users = User.all().filter("enabled =", True).fetch(500)
d = date_for_new_snippet()
post(self)

def post(self):
user = user_from_email(self.request.get('email'))
d = date_for_retrieval()
all_snippets = Snippet.all().filter("date =", d).fetch(500)
for user in all_users:
following = compute_following(user, all_users)
body = '\n\n\n'.join([self.__snippet_to_text(s) for s in all_snippets if s.user.email in following])
if body:
self.__send_mail(user.email, body)
else:
logging.info(user.email + ' not following anybody.')
all_users = User.all().fetch(500)
following = compute_following(user, all_users)
logging.info(all_snippets)
body = '\n\n\n'.join([self.__snippet_to_text(s) for s in all_snippets if s.user.email in following])
if body:
self.__send_mail(user.email, body)
else:
logging.info(user.email + ' not following anybody.')
4 changes: 3 additions & 1 deletion main.py
Expand Up @@ -186,7 +186,9 @@ def main():
('/follow', FollowHandler),
('/unfollow', UnfollowHandler),
('/reminderemail', ReminderEmail),
('/digestemail', DigestEmail)],
('/digestemail', DigestEmail),
('/onereminder', OneReminderEmail),
('/onedigest', OneDigestEmail)],
debug=True)
util.run_wsgi_app(application)

Expand Down
3 changes: 3 additions & 0 deletions queue.yaml
@@ -0,0 +1,3 @@
queue:
- name: default
rate: 1/s

0 comments on commit 8fddab8

Please sign in to comment.