我有一个由后端服务的拉取队列,当队列为空时,我需要触发另一个脚本。
目前,我在从队列中租用任务的方法中使用了一个非常粗糙的检测方法,因此如果返回的任务列表为空,我们就会认为没有更多的任务可以租用并触发下一步。然而,虽然这在大多数情况下都是有效的,但偶尔租约请求似乎会返回一个空列表,即使有任务可用。
无论如何,我认为更好的方法是使用队列的fetch_statistics方法。这样,脚本就可以监控拉取队列中正在发生的事情,并知道队列中没有更多的项目了。现在,这显然可以通过用于队列的REST api获得,但是当我在内部使用它们时,使用它似乎是相当落后的。
所以我调用了Queue.fetch_statistics(),但它抛出了一个错误。我已尝试将声明的错误输入Google,但它没有返回任何信息。
它总是抛出:
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)有没有人能说明这一点?我在这里做了什么很愚蠢的事吗?
发布于 2012-05-14 15:23:46
任务队列统计API现在已记录在案,并已公开提供。不再出现错误。
发布于 2012-03-23 16:52:25
为了防止它对其他人有用,这里有一个示例函数,可以让你开始从你的应用程序中获取队列信息。这只是一个例子,可以做更好的错误处理,但它应该让你启动和运行。之前我们使用了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不要忘记使用应用程序标识的访问控制列表更新您的queue.yaml
-name: queue_name
mode: pull
acl:
- user_email: myappid@appspot.gserviceaccount.com我希望有人会觉得这很有用。
在此期间,我已经发布了一个功能请求,所以我们可以用队列对象来做这件事,如果你也想要的话,请去启动它。http://goo.gl/W8Pk1
发布于 2012-03-23 05:40:31
出现特定错误的直接原因似乎是代码中的错误;Queue.fetch_statistics()调用QueueStatistics.fetch()调用QueueStatistics._FetchMultipleQueues(),后者显然遇到了apiproxy_errors.ApplicationError,然后尝试调用cls.__TranslateError(),但是QueueStatistics类上没有这样的方法。
我不知道ApplicationError的更深层次的原因,但这可能意味着生产运行时还不支持该功能。
https://stackoverflow.com/questions/9802175
复制相似问题