# HG changeset patch
# User Samuel Trégouët <samuel.tregouet@logilab.fr>
# Date 1489145733 -3600
# Fri Mar 10 12:35:33 2017 +0100
# Node ID 8c10ddc019394c5514ea430717f5ef8440ddcb6e
# Parent de0f70b83ec1b273354beae4e2346f4e9acf1b29
[hook] move delete es document in method and catch not found error
we should not crash if we try to delete an inexisting document
# User Samuel Trégouët <samuel.tregouet@logilab.fr>
# Date 1489145733 -3600
# Fri Mar 10 12:35:33 2017 +0100
# Node ID 8c10ddc019394c5514ea430717f5ef8440ddcb6e
# Parent de0f70b83ec1b273354beae4e2346f4e9acf1b29
[hook] move delete es document in method and catch not found error
we should not crash if we try to delete an inexisting document
@@ -17,11 +17,11 @@
1 2 """cubicweb-elasticsearch specific hooks and operations""" 3 4 import logging 5 6 -from elasticsearch.exceptions import ConnectionError 7 +from elasticsearch.exceptions import ConnectionError, NotFoundError 8 from urllib3.exceptions import ProtocolError 9 10 from cubicweb.server import hook 11 from cubicweb.predicates import score_entity 12 from cubicweb_elasticsearch.es import indexable_types, fulltext_indexable_rql, CUSTOM_ATTRIBUTES
@@ -63,10 +63,20 @@
13 IndexEsOperation.get_instance(self._cw).add_data(entity) 14 15 16 class IndexEsOperation(hook.DataOperationMixIn, hook.Operation): 17 18 + @staticmethod 19 + def delete_doc(self, es, **kwargs): 20 + try: 21 + # TODO option for async ? 22 + es.delete(**kwargs) 23 + except (ConnectionError, ProtocolError): 24 + log.warning('Failed to delete in hook, could not connect to ES') 25 + except NotFoundError: 26 + log.info('Failed to delete es document in hook (%s)', repr(kwargs)) 27 + 28 def postcommit_event(self): 29 indexer = self.cnx.vreg['es'].select('indexer', self.cnx) 30 es = indexer.get_connection() 31 if es is None or not indexer.index_name: 32 log.error('no connection to ES (not configured) skip ES indexing')
@@ -74,15 +84,11 @@
33 for entity in self.get_data(): 34 kwargs = dict(index=indexer.index_name, 35 id=entity.eid, 36 doc_type=entity.cw_etype) 37 if self.cnx.deleted_in_transaction(entity.eid): 38 - try: 39 - # TODO option for async ? 40 - es.delete(**kwargs) 41 - except (ConnectionError, ProtocolError): 42 - log.debug('Failed to index in hook, could not connect to ES') 43 + self.delete_doc(es, **kwargs) 44 continue 45 rql = fulltext_indexable_rql(entity.cw_etype, 46 entity._cw.vreg.schema, 47 eid=entity.eid) 48 indexable_entity = self.cnx.execute(rql).one()
@@ -101,6 +107,6 @@
49 kwargs['doc_type'] = getattr(serializer, 'doc_type', json['cw_etype']) 50 try: 51 # TODO option for async ? 52 es.index(**kwargs) 53 except (ConnectionError, ProtocolError): 54 - log.debug('Failed to index in hook, could not connect to ES') 55 + log.warning('Failed to index in hook, could not connect to ES')