もちろん、AppEngineデータストアにはダウンタイムがあります。ただし、データストアエラーに直面してもより堅牢な「フェイルセーフ」プットが必要です(以下の動機を参照)。データストアが利用できない場合、タスクキューは書き込みを延期するための明白な場所のようです。ただし、他の解決策はわかりません(urlfetchを介してデータをサードパーティに送信する以外)。
動機:データストアに配置する必要のあるエンティティがあります。ユーザーにエラーメッセージを表示するだけでは不十分です。たとえば、簡単に元に戻すことができない何らかの副作用が発生した可能性があります(おそらくサードパーティのサイトとの相互作用)。
私は(私が思うに)合理的な「フェイルセーフ」プットを提供する単純なラッパーを思いついた(以下を参照)。これに問題がありますか、またはより堅牢な実装のアイデアがありますか?(注:NickJohnsonとSaxonDruceによる回答に投稿された提案のおかげで、この投稿はコードにいくつかの改良を加えて編集されました。)
import logging
from google.appengine.api.labs.taskqueue import taskqueue
from google.appengine.datastore import entity_pb
from google.appengine.ext import db
from google.appengine.runtime.apiproxy_errors import CapabilityDisabledError
def put_failsafe(e, db_put_deadline=20, retry_countdown=60, queue_name='default'):
"""Tries to e.put(). On success, 1 is returned. If this raises a db.Error
or CapabilityDisabledError, then a task will be enqueued to try to put the
entity (the task will execute after retry_countdown seconds) and 2 will be
returned. If the task cannot be enqueued, then 0 will be returned. Thus a
falsey value is only returned on complete failure.
Note that since the taskqueue payloads are limited to 10kB, if the protobuf
representing e is larger than 10kB then the put will be unable to be
deferred to the taskqueue.
If a put is deferred to the taskqueue, then it won't necessarily be
completed as soon as the datastore is back up. Thus it is possible that
e.put() will occur *after* other, later puts when 1 is returned.
Ensure e's model is imported in the code which defines the task which tries
to re-put e (so that e can be deserialized).
"""
try:
e.put(rpc=db.create_rpc(deadline=db_put_deadline))
return 1
except (db.Error, CapabilityDisabledError), ex1:
try:
taskqueue.add(queue_name=queue_name,
countdown=retry_countdown,
url='/task/retry_put',
payload=db.model_to_protobuf(e).Encode())
logging.info('failed to put to db now, but deferred put to the taskqueue e=%s ex=%s' % (e, ex1))
return 2
except (taskqueue.Error, CapabilityDisabledError), ex2:
return 0
タスクのリクエストハンドラ:
from google.appengine.ext import db, webapp
# IMPORTANT: This task deserializes entity protobufs. To ensure that this is
# successful, you must import any db.Model that may need to be
# deserialized here (otherwise this task may raise a KindError).
class RetryPut(webapp.RequestHandler):
def post(self):
e = db.model_from_protobuf(entity_pb.EntityProto(self.request.body))
e.put() # failure will raise an exception => the task to be retried
私はこれをすべてのプットに使用することを期待していません-ほとんどの場合、エラーメッセージを表示することは問題ありません。すべてのプットに使用したくなりますが、変更が後で表示されることをユーザーに伝えると(データストアがバックアップされて延期されるまで古いデータを表示し続けると、ユーザーが混乱する可能性があると思います)puts execute)。