2012-03-21 114 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

相關問題