Skip to content

Commit

Permalink
holding: better delete_standard_holdings_having_no_items
Browse files Browse the repository at this point in the history
* Adds try, exception to delete of holding.

Co-Authored-by: Peter Weber <[email protected]>
  • Loading branch information
rerowep and rerowep committed Mar 10, 2022
1 parent c6e9f75 commit e9a477b
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions rero_ils/modules/holdings/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,21 @@
@shared_task(ignore_result=True)
def delete_standard_holdings_having_no_items():
"""Removes standard holdings records with no attached items."""
current_app.logger.debug(
"Starting delete_standard_holdings_having_no_items task ...")
es_query = HoldingsSearch() \
.filter('term', holdings_type='standard') \
.filter('term', items_count=0) \
.source('pid')

deleted = 0
errors = 0
for hit in es_query.scan():
record = Holding.get_record_by_pid(hit.pid)
record.delete(force=False, dbcommit=True, delindex=True)
deleted += 1

current_app.logger.debug("Ending delete_standard_holdings_having_no_items")
msg = f'Number of removed holdings: {es_query.count()}'
set_timestamp('holdings-deletion', deleted=deleted)
return msg
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}
set_timestamp('delete_standard_holdings_having_no_items', **counts)
return counts

0 comments on commit e9a477b

Please sign in to comment.