Skip to content

Commit

Permalink
task: add clean up task for parent pids that are not deleted
Browse files Browse the repository at this point in the history
  • Loading branch information
carlinmack committed Nov 1, 2024
1 parent 2452779 commit d83ceaa
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
7 changes: 6 additions & 1 deletion invenio_rdm_records/services/components/pids.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from invenio_drafts_resources.services.records.uow import ParentRecordCommitOp
from invenio_records_resources.services.uow import TaskOp

from ..pids.tasks import register_or_update_pid
from ..pids.tasks import register_or_update_pid, cleanup_parent_pids


class PIDsComponent(ServiceComponent):
Expand Down Expand Up @@ -212,6 +212,11 @@ def delete_record(self, identity, data=None, record=None, uow=None):
self.service.pids.parent_pid_manager.discard_all(
parent_pids, soft_delete=True
)
else:
# We're sending a task in case there is a race condition with two
# versions being deleted at the same time to ensure that we have
# consistent database state
self.uow.register(TaskOp(cleanup_parent_pids, record["id"]))

# Async register/update tasks after transaction commit.
for scheme in parent_pids.keys():
Expand Down
11 changes: 11 additions & 0 deletions invenio_rdm_records/services/pids/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

"""RDM PIDs Service tasks."""

from copy import copy

from celery import shared_task
from invenio_access.permissions import system_identity

Expand All @@ -22,3 +24,12 @@ def register_or_update_pid(recid, scheme, parent=False):
scheme=scheme,
parent=parent,
)


@shared_task(ignore_result=True)
def cleanup_parent_pids(record):
"""Clean up parent PIDs which could not be deleted by the service."""
record_cls = current_rdm_records.records_service
parent_pids = copy(record.parent.get("pids", {}))
if record_cls.next_latest_published_record_by_parent(record.parent) is None:
record_cls.pids.parent_pid_manager.discard_all(parent_pids, soft_delete=True)

0 comments on commit d83ceaa

Please sign in to comment.