Created
June 26, 2009 13:46
-
-
Save thruflo/136476 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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