Skip to content

Instantly share code, notes, and snippets.

@thruflo
Created June 26, 2009 13:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thruflo/136476 to your computer and use it in GitHub Desktop.
Save thruflo/136476 to your computer and use it in GitHub Desktop.
def setup_trustmap_computations(self, params):
service = 'trustmap.compute'
userkey = params['userkey'] # db.Key
context = params['context'] # str
timestamp = params['timestamp'] # db.DateTimeProperty
# due to appengine's whims, or if we exceed the request deadline
# this method may be called multiple times with the same params
# if so, we resume where we last left off
# otherwise, we start by querying to get a list of users who we need
# to create helper tasks for
dukey = u'%s-%s-%s' % (str(userkey), context, timestamp)
du = DirtyUsers.get_or_insert(keyname=dukey)
if not du.is_saved():
users = get_affected_users(userkey, context)
du.users = users
def save(helper_tasks, du):
if du.users:
db.put(helper_tasks + [du])
else:
if helper_tasks:
db.put(helper_tasks)
if du.is_saved():
db.delete(du)
helper_tasks[:] = []
# we iterate through the list of users, creating helper tasks along the way
try:
helper_tasks = []
for item in users:
hash_ = trustmap_compute_hasher(item, context)
ht = HelperTask.get_or_insert(hash_, service=service, task=task, parent=du)
helper_tasks.append(ht)
processed_users.append(item)
try:
du.users.remove(item)
except ValueError:
pass
# when we get a signifiant batch, we store it along the way
if len(helper_tasks) > 19:
db.run_in_transaction(save, helper_tasks, du)
except DeadlineExceededError, e:
logging.info(e)
# then when either done, or killed due to DeadlineExceededError we
# either save where we're up to or clean up
finally:
db.run_in_transaction(save, helper_tasks, du)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment