【问题标题】:Google App Engine Task Queue Fetch Statistics failGoogle App Engine 任务队列获取统计信息失败
【发布时间】:2012-03-21 09:56:04
【问题描述】:

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

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

无论如何,我认为更好的方法是使用队列的 fetch_statistics 方法。这样,脚本可以监控拉取队列中发生的事情,并知道队列中没有更多的项目。现在这显然可以通过队列的 REST api 获得,但是当我在内部使用它们时使用它似乎相当落后。

所以我正在调用 Queue.fetch_statistics(),但它会引发错误。我已经尝试将声明的错误放入 Google,但它没有返回任何内容。在 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)

任何人都可以对此有所了解吗?我在这里做了什么非常愚蠢的事情吗?

【问题讨论】:

  • AFAIK Queue 没有 fetch_statistics 方法
  • 确实如此,当然它在代码 sdk 中,但它没有记录。它是用于 REST API 服务的 JSON 化方法。在 taskqueue.py 第 1810 行 def fetch_statistics(self): """Get the current details about this queue. Returns: A QueueStatistics instance containing information about this queue. """ return QueueStatistics.fetch(self) 中,抛出的异常也与从方法返回的对象有关,而不是与方法本身不存在有关。在运行时可能会略有不同。
  • 使用未记录的 api,提供者在什么时候可以切断你并破坏你不是最明智的事情。
  • 它在code.google.com/appengine/docs/python/taskqueue 上唯一没有记录,而且很多东西没有包括在内或严格准确。 REST API 正在使用代码本身,因此不太可能被切断,我记得不久前在 PYTHON Group 上也提到过这个特性。反正我只需要它在 3 月剩下的时间里运行,所以它不像我依赖它作为核心长期应用程序功能的一部分。

标签: python google-app-engine


【解决方案1】:

以防它对其他人有用,这是一个示例函数,可让您开始从应用中获取队列信息。它只是一个示例,可以更好地处理错误,但它应该让您启动并运行。以前我们使用过 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

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

与此同时,我发布了一个功能请求,以便我们可以使用 Queue 对象执行此操作,如果您也想要它,请去加注星标。 http://goo.gl/W8Pk1

【讨论】:

    【解决方案2】:

    Task Queue Statistics API 现已记录并公开可用。错误不再发生。

    【讨论】:

      【解决方案3】:

      您遇到特定错误的直接原因似乎是代码中的错误; Queue.fetch_statistics() 调用 QueueStatistics.fetch() 调用 QueueStatistics._FetchMultipleQueues() 显然遇到 apiproxy_errors.ApplicationError 然后尝试调用 cls.__TranslateError() 但 QueueStatistics 类上没有这样的方法。

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

      【讨论】:

      • 谢谢吉多!没想到楼主这么回复!我只是认为它可能,因为 SDK 中的 Rest api 似乎在 JSONifying 响应之前使用了相同的函数。问题是,我决定尝试使用 REST API 来查看返回的结果。这似乎在生产运行时工作正常。 {u'kind': u'taskqueues#taskqueue', u'stats': {u'oldestTask': u'0', u'leasedLastMinute': u'4', u'leasedLastHour': u'4', u “总任务”:0}。虽然使用 appidentity 来为自己的队列在 rest api 上进行身份验证似乎有点奇怪,但它可以工作并且对我有用!
      猜你喜欢
      • 2011-06-22
      • 2011-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多