Skip to content

Commit

Permalink
holdings: fix deletion of standard holdings
Browse files Browse the repository at this point in the history
Sometimes an holding record is already deleted from the database,
but the index is not up to date. In this case, we need to delete
it from index.

* Fixes `delete_standard_holdings_having_no_items` task.

Co-Authored-by: Lauren-D <[email protected]>
  • Loading branch information
lauren-d committed Oct 25, 2022
1 parent 348c4ed commit dcd9711
Showing 1 changed file with 23 additions and 12 deletions.
35 changes: 23 additions & 12 deletions rero_ils/modules/holdings/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from celery import shared_task
from flask import current_app

from .api import Holding, HoldingsSearch
from .api import Holding, HoldingsIndexer, HoldingsSearch
from ..utils import set_timestamp


Expand All @@ -33,17 +33,28 @@ def delete_standard_holdings_having_no_items():
.filter('term', holdings_type='standard') \
.filter('term', items_count=0) \
.source('pid')

search_results = [hit for hit in es_query.scan()]
count = len(search_results)
deleted = 0
errors = 0
for hit in [hit for hit in es_query.scan()]:
record = Holding.get_record(hit.meta.id)
try:
record.delete(force=False, dbcommit=True, delindex=True)
except Exception as err:
errors += 1
reasons = record.reasons_not_to_delete()
current_app.logger.error(
f'Can not delete standard holding: {hit.pid} {reasons} {err}')

counts = {'count': es_query.count(), 'errors': errors}
for hit in search_results:
if record := Holding.get_record(hit.meta.id):
try:
record.delete(force=False, dbcommit=True, delindex=True)
deleted += 1
except Exception as err:
errors += 1
reasons = record.reasons_not_to_delete()
current_app.logger.error(
f'Can not delete standard holding: '
f'{hit.pid} {reasons} {err}')
else:
# delete holding from index
HoldingsIndexer().client.delete(
id=hit.meta.id, index='holdings', doc_type='_doc')
deleted += 1

counts = {'count': count, 'deleted': deleted, 'errors': errors}
set_timestamp('delete_standard_holdings_having_no_items', **counts)
return counts

0 comments on commit dcd9711

Please sign in to comment.