Make sure the indexing lock is released,

even when an error occurs.

Signed-off-by: Roberto Rosario <roberto.rosario.gonzalez@gmail.com>
This commit is contained in:
Roberto Rosario
2017-07-01 14:49:32 -04:00
parent c2854d094b
commit e1a0ffd25c

View File

@@ -191,78 +191,78 @@ class IndexTemplateNode(MPTTModel):
# Start transaction after the lock in case the locking backend uses # Start transaction after the lock in case the locking backend uses
# the database. # the database.
with transaction.atomic(): try:
logger.debug('IndexTemplateNode; Indexing document: %s', document) with transaction.atomic():
logger.debug(
'Removing document "%s" from all index instance nodes',
document
)
for index_template_node in self.index_instance_nodes.all():
index_template_node.remove_document(
document=document, acquire_lock=False
)
if not self.parent:
logger.debug( logger.debug(
'IndexTemplateNode; parent: creating empty root index ' 'IndexTemplateNode; Indexing document: %s', document
'instance node'
) )
index_instance_node, created = self.index_instance_nodes.get_or_create()
for child in self.get_children(): logger.debug(
child.index_document( 'Removing document "%s" from all index instance nodes',
document=document, acquire_lock=False, document
index_instance_node_parent=index_instance_node )
for index_template_node in self.index_instance_nodes.all():
index_template_node.remove_document(
document=document, acquire_lock=False
) )
if acquire_lock: if not self.parent:
lock.release() logger.debug(
'IndexTemplateNode; parent: creating empty root index '
'instance node'
)
index_instance_node, created = self.index_instance_nodes.get_or_create()
elif self.enabled: for child in self.get_children():
logger.debug('IndexTemplateNode; non parent: evaluating') child.index_document(
logger.debug('My parent template is: %s', self.parent) document=document, acquire_lock=False,
logger.debug( index_instance_node_parent=index_instance_node
'My parent instance node is: %s',
index_instance_node_parent
)
logger.debug(
'IndexTemplateNode; Evaluating template: %s', self.expression
)
try:
context = Context({'document': document})
template = Template(self.expression)
result = template.render(context=context)
except Exception as exception:
logger.debug('Evaluating error: %s', exception)
error_message = _(
'Error indexing document: %(document)s; expression: '
'%(expression)s; %(exception)s'
) % {
'document': document,
'expression': self.expression,
'exception': exception
}
logger.debug(error_message)
else:
logger.debug('Evaluation result: %s', result)
if result:
index_instance_node, created = self.index_instance_nodes.get_or_create(
parent=index_instance_node_parent,
value=result
) )
if self.link_documents: elif self.enabled:
index_instance_node.documents.add(document) logger.debug('IndexTemplateNode; non parent: evaluating')
logger.debug('My parent template is: %s', self.parent)
logger.debug(
'My parent instance node is: %s',
index_instance_node_parent
)
logger.debug(
'IndexTemplateNode; Evaluating template: %s', self.expression
)
for child in self.get_children(): try:
child.index_document( context = Context({'document': document})
document=document, acquire_lock=False, template = Template(self.expression)
index_instance_node_parent=index_instance_node result = template.render(context=context)
except Exception as exception:
logger.debug('Evaluating error: %s', exception)
error_message = _(
'Error indexing document: %(document)s; expression: '
'%(expression)s; %(exception)s'
) % {
'document': document,
'expression': self.expression,
'exception': exception
}
logger.debug(error_message)
else:
logger.debug('Evaluation result: %s', result)
if result:
index_instance_node, created = self.index_instance_nodes.get_or_create(
parent=index_instance_node_parent,
value=result
) )
finally: if self.link_documents:
if acquire_lock: index_instance_node.documents.add(document)
lock.release()
for child in self.get_children():
child.index_document(
document=document, acquire_lock=False,
index_instance_node_parent=index_instance_node
)
finally:
if acquire_lock:
lock.release()
class Meta: class Meta:
verbose_name = _('Index node template') verbose_name = _('Index node template')