谷歌应用程序引擎任务队列获取统计信息失败



我有一个由后端服务的拉取队列,当队列为空时,我需要触发另一个脚本。

目前,我在从队列中租赁任务的方法中使用了一个非常粗糙的检测,因此,如果返回的任务列表为空,我们假设没有更多的任务可租赁,并触发下一步。然而,虽然这在大多数情况下都有效,但偶尔租赁请求似乎会返回一个空列表,即使有可用的任务。

无论如何,我认为更好的方法是使用Queue的fetch_statistics方法。这样,脚本就可以监视拉队列中发生的事情,并知道队列中已经没有其他项目了。现在,这显然可以通过RESTapi用于队列,但当我在内部使用这些时,使用它似乎相当落后。

所以我正在进行Queue.fetch_statistics()调用,但它抛出了一个错误。我试着把声明的错误输入谷歌,但没有任何结果。stackoverflow也是如此。

它总是抛出:

AttributeError: type object 'QueueStatistics' has no attribute '_QueueStatistics__TranslateError'

我的代码是:

    q = taskqueue.Queue('reporting-pull')
    try:
        logging.debug(q.fetch_statistics())
    except Exception, e:
        logging.exception(e)

有人能说明这件事吗?我是不是在这里做一些非常愚蠢的事情?

如果它对其他人有用,这里有一个示例函数可以让你开始从应用程序中获取队列信息。这只是一个例子,可以做更好的错误处理,但它应该让你启动并运行。以前我们使用过Taskqueue客户端,但我认为这有点过头了,因为我们无论如何都可以在代码中租赁和删除,所以我使用了应用程序标识,这很有效。

from google.appengine.api import taskqueue
from google.appengine.api import app_identity
from google.appengine.api import urlfetch
try:
    import json
except ImportError:
    import simplejson as json
import logging
def get_queue_info(queue_name, stats=False):
    '''
        Uses the Queue REST API to fetch queue info
        Args:
            queue_name: string - the name of the queue
            stats: boolean - get the stats info too
        RETURNS:
            DICT: from the JSON response or False on fail
    '''
    scope = 'https://www.googleapis.com/auth/taskqueue'
    authorization_token, _ = app_identity.get_access_token(scope)
    app_id = app_identity.get_application_id()
    #note the s~ denoting HRD its not mentioned in the docs as far as 
    #I can see, but it wont work without it
    uri = 'https://www.googleapis.com/taskqueue/v1beta1/projects/s~%s/taskqueues/%s?getStats=%s' % (app_id, queue_name, stats)
    #make the call to the API
    response = urlfetch.fetch(uri, method="GET", headers = {"Authorization": "OAuth " + authorization_token})
    if response.status_code == 200:
        result = json.loads(response.content)
    else:
        logging.error('could not get queue')
        logging.error(response.status_code)
        logging.error(response.content)
        return False

    return result

别忘了用应用程序标识的acl更新你的queue.yaml

-name: queue_name
 mode: pull
 acl:
 - user_email: myappid@appspot.gserviceaccount.com

我希望有人觉得这很有用。

与此同时,我发布了一个Feature请求,这样我们就可以对Queue对象执行此操作,如果你也想要的话,请去星形标记它。http://goo.gl/W8Pk1

任务队列统计API现已文档化并公开可用。错误不再发生。

出现特定错误的直接原因似乎是代码中的错误;Queue.fetch_statistics()调用QueueStatistics。fetch()调用队列统计信息_FetchMultipleQueues(),它显然遇到了apiproxy_errors.ApplicationError,然后尝试调用cls__TranslateError(),但QueueStatistics类上没有这样的方法。

我不知道ApplicationError的深层原因,但这可能意味着生产运行时还不支持该功能。

最新更新