2012-03-21 127 views
2

我有一个后端服务的拉队列,当队列为空时,我需要触发另一个脚本。Google App Engine任务队列获取统计失败

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

无论如何,更好的方式来做它我认为是使用Queue的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) 

任何人都可以揭示出这个任意光?我在这里做一些非常愚蠢的事情?

+0

AFAIK [队列](http://code.google.com/appengine/docs/python/taskqueue/queues.html)没有fetch_statistics方法 – 2012-03-21 11:48:13

+0

它确实在代码sdk中,但它确实它没有记录。它是为REST API服务使用JSON化的方法。在任务队列中。PY线1810 '高清fetch_statistics(个体经营): “”“获得关于这个队列中的当前信息 返回:关于这个队列 一个QueueStatistics实例包含信息 ‘’” 回报QueueStatistics.fetch(个体经营) ' 另外抛出的异常涉及从方法返回的对象,而不是方法本身不存在。运行时可能稍有不同。 – 2012-03-21 13:18:03

+0

与无证api一起工作,任何时候提供者可以切断你并破坏你的时刻并不是最明智的事情。 – 2012-03-21 14:17:24

回答

2

现在,任务队列统计信息API已记录并公开可用。错误不再发生。

1

你得到的具体错误的直接原因似乎是代码中的错误; Queue.fetch_statistics()调用QueueStatistics.fetch()调用显然遇到apiproxy_errors.ApplicationError的QueueStatistics._FetchMultipleQueues(),然后尝试调用cls .__ TranslateError(),但QueueStatistics类中没有这种方法。

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

+1

感谢Guido!我没想到会让顶尖男士回应!我只是认为它可能,因为看起来SDK中的Rest api似乎在响应的JSON化之前使用了相同的函数。事情是,我决定只是尝试使用REST API来查看返回的内容。这似乎在生产运行时正常工作。 {u'kind':u'taskqueues#taskqueue',u'stats':{u'oldestTask':u'0',u'leasedLastMinute':u'4',u'leasedLastHour':u'4', u'totalTask​​s':0}。虽然使用appidentity为自己的队列授权a休息API似乎有点奇怪,但它可以工作,并且会为我做! – 2012-03-22 22:01:45

3

只要它对其他人有用,下面是一个示例函数,让您开始从您的应用获取队列信息。它只是一个例子,可以做更好的错误处理,但它应该让你启动并运行。之前我们已经使用了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: [email protected] 

我希望有人认为这是有用的。

与此同时,我发布了一个功能请求,所以我们可以使用Queue对象来做到这一点,如果您也想要它,请去它的星号。 http://goo.gl/W8Pk1

+0

不知何故,您的功能请求被标记为“无效” – Ryan 2012-05-11 11:56:45

相关问题