[hook] move delete es document in method and catch not found error

we should not crash if we try to delete an inexisting document

authorSamuel Trégouët <samuel.tregouet@logilab.fr>
changeset4fae8f54f665
branchdefault
phasedraft
hiddenyes
parent revision#4f9f0358830b [test] unittest on hook
child revision#2cebc589ac99 [entities] do not serialize containers of File
files modified by this revision
hooks.py
# 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 4fae8f54f665f524b699f0860a369c69e65f1ea4
# Parent 4f9f0358830b858bf36fc55de6be580f550ed77b
[hook] move delete es document in method and catch not found error

we should not crash if we try to delete an inexisting document

diff --git a/hooks.py b/hooks.py
@@ -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 cubes.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()
@@ -105,6 +111,6 @@
49                  kwargs['routing'] = entity.eid
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')