Skip to content

Commit

Permalink
Prevent index from being deleted when a exception occurs (#75)
Browse files Browse the repository at this point in the history
* Prevent index from being deleted when a exception occurs

* lint

* We dont need try except haha|

---------

Co-authored-by: Joey Jurjens <[email protected]>
  • Loading branch information
joeyjurjens and Joey Jurjens authored Nov 21, 2024
1 parent 1dd8994 commit 4b46d83
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
7 changes: 2 additions & 5 deletions oscar_elasticsearch/search/indexing/indexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,8 @@ def reindex(self):
index.reindex_objects(chunk)
"""
self.indexer.start()

try:
yield self
finally:
self.indexer.finish()
yield self
self.indexer.finish()

def reindex_objects(self, objects):
es_data = self.make_documents(objects)
Expand Down
19 changes: 19 additions & 0 deletions oscar_elasticsearch/search/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,25 @@ def test_popularity_based_on_order_lines(self):
quantity,
)

def test_exception_does_not_delete_index(self):
call_command("update_index_products")
sleep(3)

results, total_hits = self.product_index.search()
self.assertEqual(results.count(), 4)
self.assertEqual(total_hits, 4)

with self.assertRaises(Exception):
with ProductElasticsearchIndex().reindex() as index:
# Trigger an error by not passing products
index.reindex_objects(Category.objects.all())
sleep(3)

# It should still have the same amount of products.
results, total_hits = self.product_index.search()
self.assertEqual(results.count(), 4)
self.assertEqual(total_hits, 4)


class TestBrowsableItems(TestCase):

Expand Down

0 comments on commit 4b46d83

Please sign in to comment.