Skip to content

Instantly share code, notes, and snippets.

@nl5887
Last active January 4, 2016 19:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nl5887/8665492 to your computer and use it in GitHub Desktop.
Save nl5887/8665492 to your computer and use it in GitHub Desktop.
Joins ElasticSearch commit into pyramid transaction manager
import transaction
class ElasticSearchIndexManager(object):
def __init__(self, callable, args, kwargs):
self.callable = callable
self.args = args
self.kwargs = kwargs
# Use the default thread transaction manager.
self.transaction_manager = transaction.manager
def commit(self, transaction):
pass
def abort(self, transaction):
pass
def sortKey(self):
return id(self)
# No subtransaction support.
def abort_sub(self, transaction):
pass # pragma NO COVERAGE
commit_sub = abort_sub
def beforeCompletion(self, transaction):
pass # pragma NO COVERAGE
afterCompletion = beforeCompletion
def tpc_begin(self, transaction, subtransaction=False):
assert not subtransaction
def tpc_vote(self, transaction):
pass
def tpc_finish(self, transaction):
self.callable(*self.args, **self.kwargs)
tpc_abort = abort
class TransactionElasticSearch(pyelasticsearch.ElasticSearch):
def index(self, *a, **kw):
# only index on commit
index_ = super(TransactionElasticSearch, self).index
transaction.get().join(ElasticSearchIndexManager(index_, a, kw))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment