Skip to content

Instantly share code, notes, and snippets.

@topless
Last active April 21, 2016 12:25
Show Gist options
  • Save topless/c6b3b15943cac9c19b2b02f4bcb44774 to your computer and use it in GitHub Desktop.
Save topless/c6b3b15943cac9c19b2b02f4bcb44774 to your computer and use it in GitHub Desktop.
Deferred app engine queue, to iterate though all entities.
@api_v1.resource('/run/patch', endpoint='api.run.patch')
class PatchRunAPI(restful.Resource):
def get(self):
deferred.defer(update_run_task)
return flask.jsonify({
'result': 'The queues started patching all run models!!',
'status': 'success',
})
def update_run_task(next_cursor=None):
run_dbs, next_cursor = util.get_dbs(model.Run.query(), cursor=next_cursor)
if run_dbs:
for run_db in run_dbs:
logging.warning(run_db)
logging.warning("Patched %d entities." % len(run_dbs))
if next_cursor['next'] is not None:
deferred.defer(update_run_task, next_cursor['next'])
return
@lipis
Copy link

lipis commented Apr 21, 2016

something like this..

def update_run_task(next_cursor=None):
  run_dbs, next_cursor = util.get_dbs(model.Run.query(), cursor=next_cursor)
  updated_dbs = []

  for run_db in run_dbs:
    # do something with run_db if needed
    updated_dbs.append(run_db)

  if updated_dbs:
    ndb.put_multi(updated_dbs)

  if next_cursor['next'] is not None:
    deferred.defer(update_run_task, next_cursor['next'])

  logging.warning("Patched %d entities." % len(run_dbs))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment