Last week was a quite exciting week for me because of various discussion around Google App Engine, such as "Goodbye Goole App Engine" and "Why we're really happy with AppEngine". I also have some opinions about it, but I much prefer to write code than participating in this kind of debate.
Instead, I am going to share my solution to deal with one of those AppEngine specific issues - an occasional DeadlineExceededError. This solution may not apply to everybody, because I am just solving this issue for my own application - but I hope sharing this kind of idea helps the community as well as myself.
To deal with this issue, I came up with a following decorator (yes, I am writing my code in Python, and decorator is my favorite feature of this language).
@memoize
def catch_dee(ratio):
def decorator(original_func):
def decorated_func(rh, *args, **kwargs):
try:
if os.environ['SERVER_SOFTWARE'].startswith('Development/') and random.random() < ratio:
raise DeadlineExceededError
return original_func(rh, *args, **kwargs)
except DeadlineExceededError:
user_agent = rh.request.headers['User-Agent']
rh.response.set_status(500)
return rh.response.out.write('{"success":false, "retry":true, "errors":["deadline_exceeded_error"]}')
return decorated_func
return decorator
And this is a piece of code from my application (sorry for additional decorators unrelated to this post -- all my code looks like this).
gdispatch.route(lambda: ('/blob/delete', DeleteHandler))
class DeleteHandler(webapp.RequestHandler):
@mysession.require_login
@config.catch_dee(0.333)
@gdispatch.kwargs
def post(self, ids):
...
This decorator does two things.
(1) It catches the DeadlineExceededError exception, and reports this error in the format my application (iPhone application) understands.
(2) It randomly (33.3%) generates this exception when I run this application under SDK, which helps me to test my client-side of code, which performs some retries.
I hope this post helps somebody. Feedback is welcome especially if you notice something I am missing.
Cheer to Google App Engine developers!
You decorator maniac! ;) Inspiring post.
Posted by: Vanni | December 07, 2010 at 07:42 AM
I love your Cloudreader app and thank you for producing it. One thing though. I can't find a way to delete tags. Can you or someone direct me how to do this?
Thank you and have a great New Year!
Posted by: Kinder | January 04, 2011 at 08:21 AM
Enjoying your Cloudreader App. Have you thought of creating some filing system with the app so can transfer a group of pdfs to one folder rather than the one list for all.
Thanks
Jes
Posted by: Jes Warra | May 08, 2011 at 03:58 AM
Loving Cloudreader. Is it possible to change the displayed title of a transferred file? I have some PDF files that didn't take on the name I entered when transferred wirelessly.
Posted by: JKrumrine | May 10, 2011 at 12:32 PM
So for a REAL twitter poll, you slohud have a program that1. randomly samples from the followers of a person (probably in a stratified manner by sequence since followers from a given point in time may be similar to each other)2. because you have the user's credentials and everyone is a follower, you can Direct message the followers in the selected sample with individualized URLs for the survey.3. Tabulation could report on open rates and on completion rates. This would give MUCH richer data to the survey consumer.
Posted by: Ami | August 04, 2012 at 11:36 AM