Skip to content

Instantly share code, notes, and snippets.

@sahid
Created January 13, 2011 16:58
Show Gist options
  • Save sahid/778179 to your computer and use it in GitHub Desktop.
Save sahid/778179 to your computer and use it in GitHub Desktop.
a safe datastore model for google appengine
import logging
import time
from google.appengine.runtime import apiproxy_errors
from google.appengine.ext import db
DATASTORE_NB_RETRY=5
DATASTORE_TIME_RETRY=.15
DATASTORE_DEFAULT_DEADLINE=5
DATASTORE_DEFAULT_CFG=db.create_config(
deadline=DATASTORE_DEFAULT_DEADLINE)
def _cfg_kwargs(**kw):
if not kw.has_key('config'):
kw['config'] = CFG_DEADLINE
return kw
class SafeModel(db.Model):
def put(self, **kw):
kw = _cfg_kwargs(**kw)
for retry in range(DATASTORE_NB_RETRY):
try:
return super(db.Model, self).put(**kw)
except(db.Timeout,
db.TransactionFailedError,
apiproxy_errors.ApplicationError,
apiproxy_errors.DeadlineExceededError), e:
logging.warn("Error during the put process, "
"retry %d in %.2fs", retry, DATASTORE_TIME_RETRY)
logging.debug(e.message)
time.sleep(DATASTORE_TIME_RETRY)
logging.exception(e)
def delete(self, **kw):
kw = _cfg_kwargs(**kw)
for retry in range(DATASTORE_NB_RETRY):
try:
return super(db.Model, self).delete(**kw)
except(db.Timeout,
apiproxy_errors.ApplicationError,
apiproxy_errors.DeadlineExceededError), e:
logging.warn("Error during the delete process, "
"retry %d in %.2fs", retry, DATASTORE_TIME_RETRY)
logging.debug(e.message)
time.sleep(DATASTORE_TIME_RETRY)
logging.exception(e)
@classmethod
def get_by_key_name(cls, key_names, parent=None, **kw):
kw = _cfg_kwargs(**kw)
for retry in range(DATASTORE_NB_RETRY):
try:
return super(db.Model, cls).get_by_key_name(key_names, parent=parent, **kw)
except(db.Timeout,
apiproxy_errors.ApplicationError,
apiproxy_errors.DeadlineExceededError), e:
logging.warn("Error during the get_by_key_name"
"process, retry %d in %.2fs", retry, DATASTORE_TIME_RETRY)
logging.debug(e.message)
time.sleep(DATASTORE_TIME_RETRY)
logging.exception(e)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment