删除实体失败的taskqueue - python - flask - google appengine

时间:2018-01-28 19:35:37

标签: python google-app-engine entities task-queue del

我不知道我在做什么。

这就是为什么我的google appengine taskqueue不起作用的原因。

目标是根据要求简单删除数百亿个实体......好吧..不是很多。我真的不想使用mapreduce,我不认为我也需要。仍然非常确定没有任务队列实现的请求会超时。因此是一个任务问题。

在我的情况下,失败的任务队列。像这样:

  

INFO 2018-01-28 18:48:21,129 module.py:788]默认值:“POST / del / text HTTP / 1.1”302 -   警告2018-01-28 18:48:21,129 taskqueue_stub.py:1981]任务task5无法执行。此任务将在3600.000秒内重试

这是我精彩的失败代码:

@sign_in_
@app.route('/del/<entity_kind>', methods=["GET", "POST"])
def DeleteStuff(entity_kind):

  allowed_deletion = {
    'text': models.Text,
  }

  cursor = None
  bookmark = request.form.get('bookmark', '')
  if bookmark:
    cursor = ndb.Cursor.from_websafe_string(bookmark)

  query = allowed_deletion[entity_kind].query()
  entries, next_cursor, more = query.fetch_page(
    1000, 
    keys_only=True,
    start_cursor=cursor)

  ndb.delete_multi(entries)

  bookmark = None
  if more:
    bookmark = next_cursor.to_websafe_string()

  taskqueue.add(
    url='/del/'+entity_kind,
    params={'bookmark': bookmark}
    )
  return "{0} deleted".format(entity_kind)

但是,我甚至无法通过其中的return语句来执行任务:

app.yaml中的

- url: /del/*
  script: app.app
  login: admin

“触发器”处理程序绑定到app.yaml:

@sign_in_
@app.route('/del/<entity_kind>', methods=["GET", "POST"])
def DeleteStuff(entity_kind):

  allowed_deletion = {
    'text': models.Text,
    'call': models.Call,
    'voicemail': models.Voicemail,
    'image': models.Image,
    'email': models.Email,
  }


  taskqueue.add(
    url='/deleting/'+entity_kind,
    #params={'bookmark': bookmark}
    )
  return "{0} to be deleted".format(entity_kind)

worker.yaml:

runtime: python27
api_version: 1
threadsafe: true
service: worker

handlers:
- url: /deleting/.*
  script: worker.app
  login: admin

worker.py:

from flask import Flask, render_template, request, redirect, url_for, abort, make_response
from google.appengine.ext import ndb
import datetime


from app import app
from app import models

@app.route('/deleting/<entity_kind>',methods=['POST'])
def DeletingStuff(entity_kind):

  print "entered task queue"

  allowed_deletion = {
    'text': models.Text,
    'call': models.Call,
    'voicemail': models.Voicemail,
    'image': models.Image,
    'email': models.Email,
  }
  return

应用结构

app (folder)
    - __init__.py
    - app.py
    - worker.py
    - worker.yaml
    - app.yaml

也许工人应该在自己的文件夹中?我不知道如何把它挂在烧瓶中......

任务队列中的print语句甚至没有触发。

所有我进入终端的内容都是说

  

INFO 2018-01-28 22:11:21,432 module.py:788]默认:“POST   /删除/图像HTTP / 1.1“302 - 警告2018-01-28 22:11:21,432   taskqueue_stub.py:1981]任务task25无法执行。这个任务会   在409.600秒重试

最后,一个有效的解决方案

@sign_in_
@app.route('/del/<entity_type>', methods=["GET", "POST"])
def DeleteStuff(entity_type):

  taskqueue.add(
    url    = "/execute_task",
    method = "POST",
    params = {
      "entity_type": entity_type,
    }
  ) # from 'from google.appengine.api import taskqueue'
  return Response("sending {} task to queue... Check logs".format(entity_type), mimetype='text/plain', status=200)#Response from 'from Flask import Response'


@sign_in_
@app.route('/execute_task', methods=["GET", "POST"])
def execute_task():
  allowed_deletion = {
    'a': models.SomemodelA,
    'b': models.SomemodelB,
    'c': models.SomemodelC,
    'd': models.SomemodelD,
  }
  entity_type = request.form.get("entity_type", '')
  bookmark = request.form.get('bookmark', '')

  cursor = None
  if bookmark:
    cursor = ndb.Cursor.from_websafe_string(bookmark)

  if entity_type:
    query = allowed_deletion[entity_type].query()
    entries, next_cursor, more = query.fetch_page(
      1000, 
      keys_only=True,
      start_cursor=cursor)

    ndb.delete_multi(entries)

    bookmark = None
    if more:
      bookmark = next_cursor.to_websafe_string()

      taskqueue.add(
        url='/execute_task',
        params={'entity_type': entity_type, 'bookmark': bookmark}
      )

  return Response("did it. You'll never see this message", mimetype='text/plain', status=200)

备注

@sign_in_也可以执行任务而不会抛出错误

Google应用引擎会针对任务队列的可变网址引发失败错误,换句话说,这会导致失败错误:

@app.route('/execute_task/<some_variable>', methods=["GET", "POST"])
def execute_task(some_variable):
    pass

这两个网址都列在app.yaml中,不需要worker.py和worker.yaml

- url: /del/*
  script: app.app
  login: admin
- url: /execute_task
  script: app.app
  login: admin

非常感谢GAEfan的帮助!! :) 另请注意:我能够从他在我的另一篇文章中给出的代码开始调试我的解决方案:simple google app engine taskqueue tutoral? flask / python / GAE

1 个答案:

答案 0 :(得分:0)

开始于:

@app.route('/del/<entity_kind>', methods = ['GET', 'POST'])

接下来,request.args用于查询字符串。您想要处理POST参数:

request.form.get('bookmark', '')

接下来,您的@sign_in_标记导致任务队列重定向到登录页面。 (请参阅错误日志中引用的302?)。删除它。尝试将此添加到您的app.yaml:

- url: /del/.*
  script: whereisapplication.app
  login: admin

这会将其置于您的Google登录之后,但任务队列仍然可以访问。

更新到您的更新:

来自任务的打印声明毫无意义;你永远不会看到它。尝试:

import logging
logging.info("{0} to be deleted".format(entity_kind))

或:

@app.route('/deleting/<entity_kind>/',methods=['GET', 'POST'])
@app.route('/deleting/<entity_kind>',methods=['GET', 'POST'])
def DeletingStuff(entity_kind):
    if request.method == 'GET':
        print "here"

并查看您是否可以在浏览器中访问该网址。注意第一条路线中的尾部斜杠:帮助诊断重定向问题。

您也可以完全跳过POST方法,并使用查询字符串:

/deleting/text?cursor=387123481246123469

cursor = request.args.get("cursor", None)