diff --git a/rero_ils/modules/holdings/tasks.py b/rero_ils/modules/holdings/tasks.py index cae06694e3..15ec378435 100644 --- a/rero_ils/modules/holdings/tasks.py +++ b/rero_ils/modules/holdings/tasks.py @@ -29,18 +29,22 @@ @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') + errors = 0 for hit in es_query.scan(): record = Holding.get_record_by_pid(hit.pid) - record.delete(force=False, dbcommit=True, delindex=True) - - current_app.logger.debug("Ending delete_standard_holdings_having_no_items") - msg = f'Number of removed holdings: {es_query.count()}' - set_timestamp('holdings-deletion', msg=msg) - 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